← index #318Issue #9346
Duplicate · high · value 1.905
QUERY · ISSUE

textwrap module not usable with MicroPython ure

openby egranataopened 2018-11-03updated 2022-09-20

I have a port of MicroPython which includes the ure module. I tried to bring in the textwrap module from this project, but ran into a couple of issues:

    wordsep_re = re.compile(
        r'(\s+|'                                  # any whitespace
        r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|'   # hyphenated words
        r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')   # em-dash

This regular expression does not compile cleanly as ure does not support the (?= notation

_whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)
_leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)

These two also fail to compile, in this case because the MULTILINE flag is missing

3 comments
jimmo · 2022-09-19

Looks like this module was written with the unix-ffi version of the re module in mind (which uses PCRE behind the scenes).

Now that we've made textwrap part of the python-stdlib section, it needs to be updated to no longer require these regexp features.

jimmo · 2022-09-20

@brianlinuxing

What do you actually need this module for? I'm not seeing any obvious reasons why a microcontroller needs to do text wrapping.

More specifically, if we were to make a much simpler subset implementation rather than making all the features of the CPython version work, what exact features would you require?

jimmo · 2022-09-20

@brianlinuxing

If you don't use the dedent function or don't require break_on_hyphens to be set to True for the wrap, then all you have to do is remove the lines that use the re features that aren't supported and this will work fine.

CANDIDATE · ISSUE

PicoW, MicroPython and textwrap, does it work fully? Or is it the re module?

closedby brianlinuxingopened 2022-09-17updated 2022-09-19
bug

Hello,

Is the textwrap module in the library usable on MicroPython 1.19.1 nightly?

This error comes up when loading up textwrap.py of Micropython-3.4.2.post1

Line 83:

Traceback (most recent call last):
File "<stdin>", line 22, in <module>
File "<stdin>", line 83, in TextWrapper
ValueError: error in regex

I wonder if there is a problem with the re package? Shown as “ r'(\s+|' # any whitespace” on line 83 as can be seen from the screenshots.

My question, is there an obvious debug strategy to this or short-term fix?

I found a previous open issue from 2018:

https://github.com/micropython/micropython-lib/issues/318

I would welcome helpful suggestions as to how to resolve this (a module I am using needs textwrap to work).

Thanks, Brian

[Running in Thonny 4.0.1 and MicroPython v1.19.1 on 2022-09-14; Raspberry Pi Pico W with RP2040]
textwrap_v1

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied