[Python-checkins] cpython (2.7): Issue #27952: Get fixcid.py working with the re module

martin.panter python-checkins at python.org
Sun Sep 11 06:07:30 EDT 2016


https://hg.python.org/cpython/rev/740e43eb8138
changeset:   103616:740e43eb8138
branch:      2.7
parent:      103593:afc0d4478083
user:        Martin Panter <vadmium+py at gmail.com>
date:        Sun Sep 11 09:48:57 2016 +0000
summary:
  Issue #27952: Get fixcid.py working with the re module

files:
  Lib/test/test_tools.py  |  83 +++++++++++++++++++++++++++++
  Misc/NEWS               |   7 ++
  Tools/scripts/fixcid.py |  30 +++++----
  3 files changed, 106 insertions(+), 14 deletions(-)


diff --git a/Lib/test/test_tools.py b/Lib/test/test_tools.py
--- a/Lib/test/test_tools.py
+++ b/Lib/test/test_tools.py
@@ -5,9 +5,11 @@
 """
 
 import os
+import runpy
 import sys
 import unittest
 import shutil
+from cStringIO import StringIO
 import subprocess
 import sysconfig
 import tempfile
@@ -359,6 +361,87 @@
         self.pindent_test(clean, closed)
 
 
+class FixcidTests(unittest.TestCase):
+    def test_parse_strings(self):
+        old1 = 'int xx = "xx\\"xx"[xx];\n'
+        old2 = "int xx = 'x\\'xx' + xx;\n"
+        output = self.run_script(old1 + old2)
+        new1 = 'int yy = "xx\\"xx"[yy];\n'
+        new2 = "int yy = 'x\\'xx' + yy;\n"
+        self.assertMultiLineEqual(output,
+            "1\n"
+            "< {old1}"
+            "> {new1}"
+            "{new1}"
+            "2\n"
+            "< {old2}"
+            "> {new2}"
+            "{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2)
+        )
+
+    def test_alter_comments(self):
+        output = self.run_script(
+            substfile=
+                "xx yy\n"
+                "*aa bb\n",
+            args=("-c", "-",),
+            input=
+                "/* xx altered */\n"
+                "int xx;\n"
+                "/* aa unaltered */\n"
+                "int aa;\n",
+        )
+        self.assertMultiLineEqual(output,
+            "1\n"
+            "< /* xx altered */\n"
+            "> /* yy altered */\n"
+            "/* yy altered */\n"
+            "2\n"
+            "< int xx;\n"
+            "> int yy;\n"
+            "int yy;\n"
+            "/* aa unaltered */\n"
+            "4\n"
+            "< int aa;\n"
+            "> int bb;\n"
+            "int bb;\n"
+        )
+
+    def test_directory(self):
+        os.mkdir(test_support.TESTFN)
+        self.addCleanup(test_support.rmtree, test_support.TESTFN)
+        c_filename = os.path.join(test_support.TESTFN, "file.c")
+        with open(c_filename, "w") as file:
+            file.write("int xx;\n")
+        with open(os.path.join(test_support.TESTFN, "file.py"), "w") as file:
+            file.write("xx = 'unaltered'\n")
+        script = os.path.join(scriptsdir, "fixcid.py")
+        output = self.run_script(args=(test_support.TESTFN,))
+        self.assertMultiLineEqual(output,
+            "{}:\n"
+            "1\n"
+            '< int xx;\n'
+            '> int yy;\n'.format(c_filename)
+        )
+
+    def run_script(self, input="", args=("-",), substfile="xx yy\n"):
+        substfilename = test_support.TESTFN + ".subst"
+        with open(substfilename, "w") as file:
+            file.write(substfile)
+        self.addCleanup(test_support.unlink, substfilename)
+
+        argv = ["fixcid.py", "-s", substfilename] + list(args)
+        script = os.path.join(scriptsdir, "fixcid.py")
+        with test_support.swap_attr(sys, "argv", argv), \
+                test_support.swap_attr(sys, "stdin", StringIO(input)), \
+                test_support.captured_stdout() as output:
+            try:
+                runpy.run_path(script, run_name="__main__")
+            except SystemExit as exit:
+                self.assertEqual(exit.code, 0)
+        return output.getvalue()
+
+
 def test_main():
     test_support.run_unittest(*[obj for obj in globals().values()
                                     if isinstance(obj, type)])
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -191,6 +191,13 @@
 - Issue #10910: Avoid C++ compilation errors on FreeBSD and OS X.
   Also update FreedBSD version checks for the original ctype UTF-8 workaround.
 
+Tools/Demos
+-----------
+
+- Issue #27952: Get Tools/scripts/fixcid.py working with the current "re"
+  module, avoid invalid Python backslash escapes, and fix a bug parsing
+  escaped C quote signs.
+
 Windows
 -------
 
diff --git a/Tools/scripts/fixcid.py b/Tools/scripts/fixcid.py
--- a/Tools/scripts/fixcid.py
+++ b/Tools/scripts/fixcid.py
@@ -88,9 +88,9 @@
     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
+    return re.match(Wanted, name)
 
 def recursedown(dirname):
     dbg('recursedown(%r)\n' % (dirname,))
@@ -168,6 +168,7 @@
     if filename == '-': return 0 # Done in filter mode
     f.close()
     if not g: return 0 # No changes
+    g.close()
 
     # Finishing touch -- move files
 
@@ -193,21 +194,21 @@
 
 # Tokenizing ANSI C (partly)
 
-Identifier = '\(struct \)?[a-zA-Z_][a-zA-Z0-9_]+'
-String = '"\([^\n\\"]\|\\\\.\)*"'
-Char = '\'\([^\n\\\']\|\\\\.\)*\''
-CommentStart = '/\*'
-CommentEnd = '\*/'
+Identifier = '(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
+Intnumber = Hexnumber + '|' + Octnumber + '|' + Decnumber
 Exponent = '[eE][-+]?[0-9]+'
-Pointfloat = '\([0-9]+\.[0-9]*\|\.[0-9]+\)\(' + Exponent + '\)?'
+Pointfloat = r'([0-9]+\.[0-9]*|\.[0-9]+)(' + Exponent + r')?'
 Expfloat = '[0-9]+' + Exponent
-Floatnumber = Pointfloat + '\|' + Expfloat
-Number = Floatnumber + '\|' + Intnumber
+Floatnumber = Pointfloat + '|' + Expfloat
+Number = Floatnumber + '|' + Intnumber
 
 # Anything else is an operator -- don't list this explicitly because of '/*'
 
@@ -228,9 +229,10 @@
 ##  print '-->', repr(line)
     i = 0
     while i < len(line):
-        i = Program.search(line, i)
-        if i < 0: break
-        found = Program.group(0)
+        match = Program.search(line, i)
+        if match is None: break
+        i = match.start()
+        found = match.group(0)
 ##      if Program is InsideCommentProgram: print '...',
 ##      else: print '   ',
 ##      print found

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list