getify / JSON.minify

Simple minifier for JSON to remove comments and whitespace
http://web.archive.org/web/20100629021329/http://blog.getify.com/2010/06/json-comments/
410 stars 102 forks source link

python: use raw strings in regex containing escapes. Fix SyntaxWarning in python 3.12 #74

Closed muxator closed 10 months ago

muxator commented 10 months ago

Before this change:

<BASE>/json_minify/__init__.py:20: SyntaxWarning: invalid escape sequence '\*'
  tokenizer = re.compile('"|(/\*)|(\*/)|(//)|\n|\r')
getify commented 10 months ago

Is this compatible with Python 2? (is the existing code? I don't know.)

muxator commented 10 months ago

Is this compatible with Python 2?

Yes, the change is compatible with python 2.7, too; in fact, this usage has been encouraged since the olden python 2.7 days (https://docs.python.org/2.7/library/re.html):

Regular expressions use the backslash character ('\') [...]. This collides with Python’s usage of the same character for the same purpose in string literals; [...]

The solution is to use Python’s raw string notation for regular expression patterns; [...] Usually patterns will be expressed in Python code using this raw string notation.

is the existing code?

Just to be on the safe side, I have run again the python tests on both python 2.7.18 and 3.12.0 on Fedora 39.