Skip to content

Fix SyntaxWarning invalid escape sequences in regex strings#197

Open
FranciscoPombal wants to merge 1 commit into
sashs:masterfrom
FranciscoPombal:fix-escape-sequences
Open

Fix SyntaxWarning invalid escape sequences in regex strings#197
FranciscoPombal wants to merge 1 commit into
sashs:masterfrom
FranciscoPombal:fix-escape-sequences

Conversation

@FranciscoPombal
Copy link
Copy Markdown

Problem

When running Ropper on modern Python versions (such as Python 3.12+ and the upcoming 3.14), the interpreter outputs multiple SyntaxWarning messages regarding invalid escape sequences:

$ PYTHONDONTWRITEBYTECODE=1 ropper --version
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/console.py:739: SyntaxWarning: "\d" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\d"? A raw string is also an option.
  match = re.match('/\d+/', text)
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/arch.py:247: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  gadget.Category.STACK_PIVOT : (('^sub (?P<dst>.sp), (?P<src>[x0-9a-fA-F]+)$','^add (?P<dst>.sp), (?P<src>[x0-9a-fA-F]+)$','^mov (?P<dst>.sp), .+ ptr \[(?P<src>...)\]$','^mov (?P<dst>.sp), (?P<src>...)$','^xchg (?P<dst>.sp), (?P<src>...)$','^xchg (?P<dst>...), (?P<src>.sp)$','ret.+'),('push','mov','call','jmp')),
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/arch.py:248: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  gadget.Category.LOAD_MEM : (('mov (?P<dst>...), .+ ptr \[(?P<src>...)\]',),('push','mov','call','jmp')),
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/arch.py:249: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  gadget.Category.WRITE_MEM : (('^mov .+ ptr \[(?P<dst>...)\], (?P<src>...)$',),('push','mov','call','jmp')),
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/arch.py:297: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  gadget.Category.STACK_PIVOT : (('^mov (?P<dst>.sp), .+ ptr \[(?P<src>...)\]$','^mov (?P<dst>.sp), (?P<src>...)$','^xchg (?P<dst>.sp), (?P<src>...)$','^xchg (?P<dst>...), (?P<src>.sp)$','ret.+'),('push','mov','call','jmp')),
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/arch.py:298: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  gadget.Category.LOAD_MEM : (('mov (?P<dst>r..), .+ ptr \[(?P<src>r..)\]',),('push','mov','call','jmp')),
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/arch.py:299: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  gadget.Category.WRITE_MEM : (('^mov .+ ptr \[(?P<dst>r..)\], (?P<src>r..)$',),('push','mov','call','jmp')),
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/search.py:46: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  CONSTRAINT_REGEX = '(\[?[a-zA-Z0-9]+\]?)([\+\*\-=/])?=(\[?[a-zA-Z0-9]+\]?)$'
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/search.py:260: SyntaxWarning: "\$" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\$"? A raw string is also an option.
  filter = filter.replace('$','\$')
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/z3helper.py:46: SyntaxWarning: "\-" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\-"? A raw string is also an option.
  ADJUST_REGEX = '([\\+\-\*/=]=)'
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/z3helper.py:47: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  ASSIGNMENT_REGEX = '('+REG_REGEX.format('reg_dst_1') + ' *' + ADJUST_REGEX + ' *('+NUMBER_REGEX+'|'+REG_REGEX.format('reg_src_1')+'|(\[)'+REG_REGEX.format('reg_src_2')+'(\])))'
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/z3helper.py:47: SyntaxWarning: "\]" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\]"? A raw string is also an option.
  ASSIGNMENT_REGEX = '('+REG_REGEX.format('reg_dst_1') + ' *' + ADJUST_REGEX + ' *('+NUMBER_REGEX+'|'+REG_REGEX.format('reg_src_1')+'|(\[)'+REG_REGEX.format('reg_src_2')+'(\])))'
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/rop.py:167: SyntaxWarning: "\?" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\?"? A raw string is also an option.
  m = re.search(b'\?', opcode)
/home/user/.local/share/uv/tools/ropper/lib/python3.14/site-packages/ropper/rop.py:187: SyntaxWarning: "\?" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\?"? A raw string is also an option.
  m = re.search(b'\?', opcode)
Version: Ropper 1.13.13
Author: Sascha Schirra
Website: http://scoding.de/ropper

Python historically bypassed unescaped regular expressions like \d or \[ inside normal string literals by falling back to passing a literal backslash.
In future Python versions, these warnings will be treated as hard SyntaxError compilation failures, breaking the application entirely.

Solution

This PR resolves the deprecation warnings by explicitly specifying raw string literals using the r prefix modifier for regex search patterns and constraint structures across the code.
Using raw string definitions allows Python to preserve and pass literal backslashes to the underlying re regular expression engine natively without processing them as Python-level string escape sequences first.

Implementation details

This patch was generated entirely by running ruff check . --select W605 --fix at the root of the repo.

Testing/Verification

  1. Run PYTHONDONTWRITEBYTECODE=1 ropper --version.

  2. Compare the output with the expected results:

    Before: Outputted noisy deprecation logs from python3.14/site-packages/ropper/*
    After: Executes cleanly with no SyntaxWarning warnings thrown.

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 this pull request may close these issues.

1 participant