Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxWarning: invalid escape sequence when using Python 3.12 #4823

Closed
goodrone opened this issue Dec 6, 2023 · 3 comments · Fixed by #4832
Closed

SyntaxWarning: invalid escape sequence when using Python 3.12 #4823

goodrone opened this issue Dec 6, 2023 · 3 comments · Fixed by #4832

Comments

@goodrone
Copy link

goodrone commented Dec 6, 2023

Python 3.12 improves handling of incorrect escape sequences, see https://docs.python.org/3/whatsnew/3.12.html#other-language-changes

This is a typical scenario in regular expressions. With Python 3.12, there are currently 5 places that use invalid escape sequences:

  1. examples/tracing/task_switch.py:9: SyntaxWarning: invalid escape sequence '\.'
      b.attach_kprobe(event_re="^finish_task_switch$|^finish_task_switch\.isra\.\d$",
    
  2. src/python/bcc/__init__.py:758: SyntaxWarning: invalid escape sequence '\.'
      elif re.match(b'^.*\.cold(\.\d+)?$', fn):
    
  3. tests/python/test_tools_smoke.py:67: SyntaxWarning: invalid escape sequence '\s'
      reg = re.compile("^%s\s" % mod)
    
  4. tests/python/test_trace2.py:34: SyntaxWarning: invalid escape sequence '\.'
      b.attach_kprobe(event_re=b"^finish_task_switch$|^finish_task_switch\.isra\.\d$",
    
  5. tools/exitsnoop.py:207: SyntaxWarning: invalid escape sequence '\.'
      if re.match('^3\.10\..*el7.*$', platform.release()): # Centos/Red Hat
    

In all places I think it will be enough to use Python raw strings, r'...'.

Here is the command I used to look up all of them:

for f in $(git ls-files | grep -E '\.py$'); do python3 -m py_compile $f; done
@yonghong-song
Copy link
Collaborator

@goodrone could you send a pull request to fix this?

hhoffstaette added a commit to hhoffstaette/bcc that referenced this issue Dec 8, 2023
gentoo-bot pushed a commit to gentoo/gentoo that referenced this issue Dec 8, 2023
Bug: iovisor/bcc#4823
Bug: iovisor/bcc#4830
Reported-by: Holger Hoffstätte <[email protected]>
Signed-off-by: Sam James <[email protected]>
yonghong-song pushed a commit that referenced this issue Dec 8, 2023
@goodrone
Copy link
Author

Thank you @hhoffstaette for preparing a pull request https://github.com/iovisor/bcc/pull/4832/files. Unfortunately, I see that it has two issues:

  • The meaning of the regular expressions has changed. In places where \. was used, matching a dot, now there is just ., which matches anything. I think we need to restore the original semantics of the regexes.
  • A nit: in a couple of places, the quotes were changed from double quotes to single quotes which doesn't match the surrounding code.

I hope to be able to contribute an improved fix when I have time.

@hhoffstaette
Copy link
Contributor

It would be great if the test suite actually checked for this, otherwise the regexes are essentially in an unknown state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants