# HG changeset patch # Parent 98a57845c8ccdc9b587304f7c6c8571762261409 Issue #27952: Get fixcid.py working with the re module diff -r 98a57845c8cc Lib/test/test_tools/test_fixcid.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/test_tools/test_fixcid.py Fri Sep 09 09:59:25 2016 +0000 @@ -0,0 +1,47 @@ +'''Test Tools/scripts/fixcid.py.''' + +from io import StringIO +import os, os.path +import runpy +import sys +from test import support +from test.test_tools import skip_if_missing, scriptsdir +import unittest + +skip_if_missing() + +class Test(unittest.TestCase): + def test_parse_strings(self): + def restore(args): + sys.argv[1:] = args + self.addCleanup(restore, sys.argv[1:]) + sys.argv[1:] = ("-s", support.TESTFN, "-") + + with open(support.TESTFN, "wt", encoding="ascii") as file: + file.write("xx yy\n") + self.addCleanup(support.unlink, support.TESTFN) + + self.addCleanup(setattr, sys, "stdin", sys.stdin) + self.addCleanup(setattr, sys, "stdout", sys.stdout) + old1 = 'int xx = "xx\\"xx"[xx];\n' + old2 = "int xx = 'x\\'xx' + xx;\n" + sys.stdin = StringIO(old1 + old2) + sys.stdout = StringIO() + script = os.path.join(scriptsdir, "fixcid.py") + try: + runpy.run_path(script, run_name="__main__") + except SystemExit as exit: + self.assertEqual(exit.code, 0) + + new1 = 'int yy = "xx\\"xx"[yy];\n' + new2 = "int yy = 'x\\'xx' + yy;\n" + self.assertMultiLineEqual(sys.stdout.getvalue(), + "1\n" + "< {old1}" + "> {new1}" + "{new1}" + "2\n" + "< {old2}" + "> {new2}" + "{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2) + ) diff -r 98a57845c8cc Misc/NEWS --- a/Misc/NEWS Fri Sep 09 07:38:50 2016 +0000 +++ b/Misc/NEWS Fri Sep 09 09:59:25 2016 +0000 @@ -329,6 +329,13 @@ - Issue #27883: Update sqlite to 3.14.1.0 on Windows. +Tools/Demos +----------- + +- Issue #27952: Get Tools/scripts/fixcid.py working with the current "re" + module, avoid deprecated Python backslash escaping, and fix a bug parsing + escaped C quote signs. + What's New in Python 3.6.0 alpha 4 ================================== diff -r 98a57845c8cc Tools/scripts/fixcid.py --- a/Tools/scripts/fixcid.py Fri Sep 09 07:38:50 2016 +0000 +++ b/Tools/scripts/fixcid.py Fri Sep 09 09:59:25 2016 +0000 @@ -88,7 +88,7 @@ sys.exit(bad) # Change this regular expression to select a different set of files -Wanted = '^[a-zA-Z0-9_]+\.[ch]$' +Wanted = r'^[a-zA-Z0-9_]+\.[ch]$' def wanted(name): return re.match(Wanted, name) >= 0 @@ -193,30 +193,30 @@ # Tokenizing ANSI C (partly) -Identifier = '\(struct \)?[a-zA-Z_][a-zA-Z0-9_]+' -String = '"\([^\n\\"]\|\\\\.\)*"' -Char = '\'\([^\n\\\']\|\\\\.\)*\'' -CommentStart = '/\*' -CommentEnd = '\*/' +Identifier = r'(struct )?[a-zA-Z_][a-zA-Z0-9_]+' +String = r'"([^\n\\"]|\\.)*"' +Char = r"'([^\n\\']|\\.)*'" +CommentStart = r'/\*' +CommentEnd = r'\*/' -Hexnumber = '0[xX][0-9a-fA-F]*[uUlL]*' -Octnumber = '0[0-7]*[uUlL]*' -Decnumber = '[1-9][0-9]*[uUlL]*' -Intnumber = Hexnumber + '\|' + Octnumber + '\|' + Decnumber -Exponent = '[eE][-+]?[0-9]+' -Pointfloat = '\([0-9]+\.[0-9]*\|\.[0-9]+\)\(' + Exponent + '\)?' -Expfloat = '[0-9]+' + Exponent -Floatnumber = Pointfloat + '\|' + Expfloat -Number = Floatnumber + '\|' + Intnumber +Hexnumber = r'0[xX][0-9a-fA-F]*[uUlL]*' +Octnumber = r'0[0-7]*[uUlL]*' +Decnumber = r'[1-9][0-9]*[uUlL]*' +Intnumber = Hexnumber + r'|' + Octnumber + r'|' + Decnumber +Exponent = r'[eE][-+]?[0-9]+' +Pointfloat = r'([0-9]+\.[0-9]*|\.[0-9]+)(' + Exponent + r')?' +Expfloat = r'[0-9]+' + Exponent +Floatnumber = Pointfloat + r'|' + Expfloat +Number = Floatnumber + r'|' + Intnumber # Anything else is an operator -- don't list this explicitly because of '/*' OutsideComment = (Identifier, Number, String, Char, CommentStart) -OutsideCommentPattern = '(' + '|'.join(OutsideComment) + ')' +OutsideCommentPattern = r'(' + r'|'.join(OutsideComment) + r')' OutsideCommentProgram = re.compile(OutsideCommentPattern) InsideComment = (Identifier, Number, CommentEnd) -InsideCommentPattern = '(' + '|'.join(InsideComment) + ')' +InsideCommentPattern = r'(' + r'|'.join(InsideComment) + r')' InsideCommentProgram = re.compile(InsideCommentPattern) def initfixline(): @@ -225,15 +225,16 @@ def fixline(line): global Program -## print '-->', repr(line) +## print('-->', repr(line)) i = 0 while i < len(line): - i = Program.search(line, i) - if i < 0: break - found = Program.group(0) -## if Program is InsideCommentProgram: print '...', -## else: print ' ', -## print found + match = Program.search(line, i) + if match is None: break + i = match.start() + found = match.group(0) +## if Program is InsideCommentProgram: print(end='... ') +## else: print(end=' ') +## print(found) if len(found) == 2: if found == '/*': Program = InsideCommentProgram @@ -248,14 +249,14 @@ i = i + n continue if NotInComment.has_key(found): -## print 'Ignored in comment:', -## print found, '-->', subst -## print 'Line:', line, +## print(end='Ignored in comment: ') +## print(found, '-->', subst) +## print('Line:', line, end=' ') subst = found ## else: -## print 'Substituting in comment:', -## print found, '-->', subst -## print 'Line:', line, +## print(end='Substituting in comment: ') +## print(found, '-->', subst) +## print('Line:', line, end=' ') line = line[:i] + subst + line[i+n:] n = len(subst) i = i + n