#4158 new defect

SyntaxWarning: invalid escape sequence

Reported by: hacklschorsch Owned by:
Priority: normal Milestone: undecided
Component: unknown Version: n/a
Keywords: Cc:
Launchpad Bug:

Description

I see a lot of "SyntaxWarning?: invalid escape sequence" warnings about us having things in strings that recent Python doesn't like.

For example, in the Debian 13 package CI runner, we find that *installing* the built package will emit lots of these warnings:

https://app.circleci.com/pipelines/github/LeastAuthority/tahoe-lafs/1303/workflows/0ee30e67-de10-4d03-8c93-6a51000393ad/jobs/15982/parallel-runs/0/steps/0-105

Selecting previously unselected package tahoe-lafs.
(Reading database ... 23602 files and directories currently installed.)
Preparing to unpack .../tahoe-lafs_1.20.0-1_all.deb ...
Unpacking tahoe-lafs (1.20.0-1) ...
Setting up tahoe-lafs (1.20.0-1) ...
/usr/lib/python3/dist-packages/allmydata/hashtree.py:73: SyntaxWarning: invalid escape sequence '\ '
  """
/usr/lib/python3/dist-packages/allmydata/hashtree.py:182: SyntaxWarning: invalid escape sequence '\ '
  """
/usr/lib/python3/dist-packages/allmydata/interfaces.py:409: SyntaxWarning: invalid escape sequence '\-'
  """This block of data contains integrity-checking information (hashes
/usr/lib/python3/dist-packages/allmydata/node.py:93: SyntaxWarning: invalid escape sequence '\.'
  ADDR_RE = re.compile("^([1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*)(:([1-9][0-9]*))?$")
/usr/lib/python3/dist-packages/allmydata/test/cli/test_backup.py:39: SyntaxWarning: invalid escape sequence '\d'
  "(\d)+ files skipped, "
/usr/lib/python3/dist-packages/allmydata/test/cli/test_backup.py:40: SyntaxWarning: invalid escape sequence '\d'
  "(\d+) directories created \((\d+) reused\), "
/usr/lib/python3/dist-packages/allmydata/test/cli/test_backup.py:41: SyntaxWarning: invalid escape sequence '\d'
  "(\d+) directories skipped", out)
/usr/lib/python3/dist-packages/allmydata/test/cli/test_cli.py:767: SyntaxWarning: invalid escape sequence '\('
  in_pending_msg_regex = "ran out of shares: complete= pending=Share\(.+\) overdue= unused= need 3"
/usr/lib/python3/dist-packages/allmydata/test/test_runner.py:257: SyntaxWarning: invalid escape sequence '\S'
  self.failIf(re.search("[\S][^\.!?]$", line), (line,))
/usr/lib/python3/dist-packages/allmydata/test/web/test_web.py:1971: SyntaxWarning: invalid escape sequence '\s'
  CSS_STYLE=re.compile(b'toolbar\s{.+text-align:\scenter.+toolbar-item.+display:\sinline',re.DOTALL)
/usr/lib/python3/dist-packages/allmydata/util/i2p_provider.py:192: SyntaxWarning: invalid escape sequence '\:'
  escaped_sam_port = sam_port.replace(':', '\:')
/usr/lib/python3/dist-packages/allmydata/web/status.py:1559: SyntaxWarning: invalid escape sequence '\d'
  u"_(\d\d)_(\d)_percentile",
/usr/lib/python3/dist-packages/allmydata/web/status.py:1560: SyntaxWarning: invalid escape sequence '\g'
  u'{quantile="0.\g<1>\g<2>"}',
/usr/lib/python3/dist-packages/allmydata/hashtree.py:73: SyntaxWarning: invalid escape sequence '\ '
  """
/usr/lib/python3/dist-packages/allmydata/hashtree.py:182: SyntaxWarning: invalid escape sequence '\ '
  """
/usr/lib/python3/dist-packages/allmydata/interfaces.py:409: SyntaxWarning: invalid escape sequence '\-'
  """This block of data contains integrity-checking information (hashes
/usr/lib/python3/dist-packages/allmydata/node.py:93: SyntaxWarning: invalid escape sequence '\.'
  ADDR_RE = re.compile("^([1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*)(:([1-9][0-9]*))?$")
/usr/lib/python3/dist-packages/allmydata/test/cli/test_backup.py:39: SyntaxWarning: invalid escape sequence '\d'
  "(\d)+ files skipped, "
/usr/lib/python3/dist-packages/allmydata/test/cli/test_backup.py:40: SyntaxWarning: invalid escape sequence '\d'
  "(\d+) directories created \((\d+) reused\), "
/usr/lib/python3/dist-packages/allmydata/test/cli/test_backup.py:41: SyntaxWarning: invalid escape sequence '\d'
  "(\d+) directories skipped", out)
/usr/lib/python3/dist-packages/allmydata/test/cli/test_cli.py:767: SyntaxWarning: invalid escape sequence '\('
  in_pending_msg_regex = "ran out of shares: complete= pending=Share\(.+\) overdue= unused= need 3"
/usr/lib/python3/dist-packages/allmydata/test/test_runner.py:257: SyntaxWarning: invalid escape sequence '\S'
  self.failIf(re.search("[\S][^\.!?]$", line), (line,))
/usr/lib/python3/dist-packages/allmydata/test/web/test_web.py:1971: SyntaxWarning: invalid escape sequence '\s'
  CSS_STYLE=re.compile(b'toolbar\s{.+text-align:\scenter.+toolbar-item.+display:\sinline',re.DOTALL)
/usr/lib/python3/dist-packages/allmydata/util/i2p_provider.py:192: SyntaxWarning: invalid escape sequence '\:'
  escaped_sam_port = sam_port.replace(':', '\:')
/usr/lib/python3/dist-packages/allmydata/web/status.py:1559: SyntaxWarning: invalid escape sequence '\d'
  u"_(\d\d)_(\d)_percentile",
/usr/lib/python3/dist-packages/allmydata/web/status.py:1560: SyntaxWarning: invalid escape sequence '\g'
  u'{quantile="0.\g<1>\g<2>"}',

Change History (2)

comment:1 Changed at 2025-01-13T21:22:23Z by hacklschorsch

Some context from https://stackoverflow.com/a/77531416/1181063

Back in Python 3.6, using invalid escape sequences in string literals was deprecated (bpo-27364). Since then, attempting to use an invalid escape sequence has emitted a DeprecationWarning?. This can often go unnoticed if you don't run Python with warnings enabled. DeprecationWarnings? are silenced by default.

Python 3.12 upgraded the DeprecationWarning? to a SyntaxWarning?. SyntaxWarnings? are emitted by the compiler when the code is parsed, not when it's being run, so they cannot be ignored using a runtime warning filter. Unlike DeprecationWarnings?, SyntaxWarnings? are displayed by default, which is why you're seeing it now. This increase in visibility was intentional. In a future version of Python, using invalid escape sequences in string literals is planned to eventually become a hard SyntaxError?.

comment:2 Changed at 2025-01-13T21:24:49Z by hacklschorsch

Maybe it's of interest that the list of warnings above are printed twice (Strg+F for "hashtree.py:73" for example)

Note: See TracTickets for help on using tickets.