SyntaxWarning in pretext/lib/pretext.py

11 views
Skip to first unread message

Jeremy Sylvestre

unread,
Sep 10, 2025, 6:06:24 PMSep 10
to PreTeXt development
pretext/lib/pretext.py:2753: SyntaxWarning: invalid escape sequence '\['
  qtext = re.sub("\[\[validation:(\w+)\]\]", "", qtext)
pretext/lib/pretext.py:2754: SyntaxWarning: invalid escape sequence '\['
  qtext = re.sub("\[\[feedback:(\w+)\]\]", "", qtext)
pretext/lib/pretext.py:2757: SyntaxWarning: invalid escape sequence '\['
  ansids = re.findall("\[\[input:(\w+)\]\]", qtext)

git blame says these lines were introduced 3 weeks ago in commit 3894e59fe. Appears to be related to recent STACK development.

I'm on python 3.13.7 over here.

Rob Beezer

unread,
Sep 11, 2025, 1:30:09 AMSep 11
to prete...@googlegroups.com
Can you say what you were doing when this happened?

I don't imagine you were attempting to have a STACK problem in your source?

Do you see the warning with the sample article?

Only a warning? In other words, not fatal?

Rob

Jeremy Sylvestre

unread,
Sep 11, 2025, 10:16:04 AMSep 11
to prete...@googlegroups.com
Rob: see my responses below.

On Wed, 10 Sept 2025 at 23:30, 'Rob Beezer' via PreTeXt development <prete...@googlegroups.com> wrote:
Can you say what you were doing when this happened?


Just a regular build using the pretext/pretext script, along the lines of

/path/to/pretext/pretext \
 --verbose \
 --component all \
 --format html \
 --publisher publication.xml \
 --directory builddir \
  book.ptx

I don't imagine you were attempting to have a STACK problem in your source?

No STACK problems.
 

Do you see the warning with the sample article?


Yes. The warning appears to occur on import of pretext/lib/pretext: I get the same warnings just from doing

>>> from lib import pretext

in a python interactive session (run from the pretext/pretext directory).
 
Only a warning?  In other words, not fatal?

No, not fatal. Build proceeds successfully after the warnings. Not sure what would happen if I did a build that requires calling the _stack_process_response routine from which the warnings originate. Does the sample article contain STACK problems? Is there something in particular I should look for in the output from building the sample article?

 

Rob

On September 11, 2025 12:06:11 AM GMT+02:00, Jeremy Sylvestre <jsyl...@ualberta.ca> wrote:
>pretext/lib/pretext.py:2753: SyntaxWarning: invalid escape sequence '\['
>  qtext = re.sub("\[\[validation:(\w+)\]\]", "", qtext)
>pretext/lib/pretext.py:2754: SyntaxWarning: invalid escape sequence '\['
>  qtext = re.sub("\[\[feedback:(\w+)\]\]", "", qtext)
>pretext/lib/pretext.py:2757: SyntaxWarning: invalid escape sequence '\['
>  ansids = re.findall("\[\[input:(\w+)\]\]", qtext)
>
>git blame says these lines were introduced 3 weeks ago in commit 3894e59fe.
>Appears to be related to recent STACK development.
>
>I'm on python 3.13.7 over here.
>

--
You received this message because you are subscribed to the Google Groups "PreTeXt development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-dev...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pretext-dev/MTAwMDA0Ni5iZWV6ZXI.1757568606%40pnsh.

Haris Skiadas

unread,
Sep 11, 2025, 10:24:07 AMSep 11
to prete...@googlegroups.com
I’m guessing there are supposed to be r’s in front of those regex strings, to mark them as raw strings?

Sent from my iPad

On Sep 11, 2025, at 10:16 AM, Jeremy Sylvestre <jsyl...@ualberta.ca> wrote:



Rob Beezer

unread,
Sep 11, 2025, 10:50:03 AMSep 11
to prete...@googlegroups.com
Thanks, Jeremy, for the additional info. (I don't have my tools with me, and had not seen this warning before. And Georg might be unavailable right now.)

I think there is a stack problem in the sample article, which is commented out, or it is on a commit that could be reverted temporarily on a branch. It was authored properly, but does not render quite as expected, iirc. I do think it would stress the relevant code.

Rob

Charilaos Skiadas

unread,
Sep 11, 2025, 11:14:18 AMSep 11
to prete...@googlegroups.com
It will probably work for the sample stack for the same reason that it is a warning: It works but it’s not quite correct Python. 
(at the risk of explaining string escaping to an audience that knows a lot more about it than I do) In the absence of an r in from of the quote, Python will do its normal string escaping to "\[\[validation:(\w+)\]\]”  before using it in the regular expression, and it encounters \[ which is simply not a valid escape sequence. It will warn and then silently ignore the problem and treat \ and [ as two separate characters and use them in the resulting regular expression, ending in the regex looking to match a literal [ as it should. Same for the other escaped characters in that line that all are invalid string escape sequences, but looks like we only get warnings for the first occurrence. So it works but Python (and VSCode with pylance) rightly warns about it. Either escaping all those backslashes with a second backslash each or writing the strings as raw strings with and r in front like r"\[\[validation:(\w+)\]\]” should eliminate the warnings and keep the behavior the same.

Charilaos Skiadas
Department of Mathematics
Hanover College


Andrew Scholer

unread,
Sep 11, 2025, 11:22:56 AMSep 11
to prete...@googlegroups.com
I'll tack on:
  • Same behavior from pretext script.  However, unless you disable caching of python bytecode, you will likely only see the warning once when pretext.py is compiled. Then you won't see it again until you touch pretext.py or blow out the __pycache__.
  • See this with any book regardless of whether it has stack problems (e.g. hello world)
Andrew Scholer


Reply all
Reply to author
Forward
0 new messages