iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.36k stars 3.86k forks source link

SyntaxWarning: invalid escape sequence when using Python 3.12 #4823

Closed goodrone closed 10 months ago

goodrone commented 10 months ago

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 commented 10 months ago

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

goodrone commented 9 months ago

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

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

hhoffstaette commented 9 months ago

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