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

Use raw strings in preproc.py #288

Closed
wants to merge 1 commit into from
Closed

Use raw strings in preproc.py #288

wants to merge 1 commit into from

Conversation

ryandesign
Copy link
Contributor

When Python 3.12 is used to build magic, these warnings appear:

magic/../scripts/preproc.py:62: SyntaxWarning: invalid escape sequence '\('
  defrex = re.compile('defined[ \t]*\(([^\)]+)\)')
magic/../scripts/preproc.py:63: SyntaxWarning: invalid escape sequence '\|'
  orrex = re.compile('(.+)\|\|(.+)')
magic/../scripts/preproc.py:65: SyntaxWarning: invalid escape sequence '\|'
  notrex = re.compile('!([^&\|]+)')
magic/../scripts/preproc.py:66: SyntaxWarning: invalid escape sequence '\('
  parenrex = re.compile('\(([^\)]+)\)')
magic/../scripts/preproc.py:170: SyntaxWarning: invalid escape sequence '\('
  paramrex = re.compile('^([^\(]+)\(([^\)]+)\)')
magic/../scripts/preproc.py:180: SyntaxWarning: invalid escape sequence '\*'
  ccstartrex = re.compile('/\*')                # C-style comment start
magic/../scripts/preproc.py:181: SyntaxWarning: invalid escape sequence '\*'
  ccendrex = re.compile('\*/')          # C-style comment end
magic/../scripts/preproc.py:417: SyntaxWarning: invalid escape sequence '\('
  pcondition = condition + '\('
magic/../scripts/preproc.py:420: SyntaxWarning: invalid escape sequence '\)'
  pcondition += '(.*)\)'
magic/../scripts/preproc.py:426: SyntaxWarning: invalid escape sequence '\g'
  pvalue = pvalue.replace(param, '\g<' + str(idx) + '>')

I've fixed it by using raw strings. See https://docs.python.org/3/library/re.html:

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

I've run the build four times: with Python 3.11 (with and without my patch) and Python 3.12 (with and without my patch). The output of preproc.py (proto.magicrc) is identical in all four runs, but with my patch Python 3.12 no longer produces warnings. And presumably in some future version of Python the warning will become an error.

Avoids "SyntaxWarning: invalid escape sequence" with Python 3.12.
Copy link
Owner

@RTimothyEdwards RTimothyEdwards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very disappointing to think that the Python developers would just redefine the operation of a core language feature that would make it backwardly-incompatible with uncountable numbers of existing scripts. But thanks for the patch!

@RTimothyEdwards
Copy link
Owner

Pulled and merged into version 8.3.460 on opencircuitdesign.com.

RTimothyEdwards added a commit that referenced this pull request Jan 24, 2024
from Ryan Schmidt, changing regular expression strings in the python
preprocessor to raw string types so that they don't produce warnings
in python 3.12.
@ryandesign ryandesign deleted the patch-2 branch April 1, 2024 07:55
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.

2 participants