cpython: 740e43eb8138 (original) (raw)
Mercurial > cpython
changeset 103616:740e43eb8138 2.7
Issue #27952: Get fixcid.py working with the re module [#27952]
Martin Panter vadmium+py@gmail.com | |
---|---|
date | Sun, 11 Sep 2016 09:48:57 +0000 |
parents | afc0d4478083 |
children | 5ca4c545dfe4 |
files | Lib/test/test_tools.py Misc/NEWS Tools/scripts/fixcid.py |
diffstat | 3 files changed, 106 insertions(+), 14 deletions(-)[+] [-] Lib/test/test_tools.py 83 Misc/NEWS 7 Tools/scripts/fixcid.py 30 |
line wrap: on
line diff
--- a/Lib/test/test_tools.py +++ b/Lib/test/test_tools.py @@ -5,9 +5,11 @@ Tools directory of a Python checkout or """ import os +import runpy import sys import unittest import shutil +from cStringIO import StringIO import subprocess import sysconfig import tempfile @@ -359,6 +361,87 @@ class PindentTests(unittest.TestCase): self.pindent_test(clean, closed) +class FixcidTests(unittest.TestCase):
- def test_parse_strings(self):
old1 = 'int xx = "xx\\"xx"[xx];\n'[](#l1.21)
old2 = "int xx = 'x\\'xx' + xx;\n"[](#l1.22)
output = self.run_script(old1 + old2)[](#l1.23)
new1 = 'int yy = "xx\\"xx"[yy];\n'[](#l1.24)
new2 = "int yy = 'x\\'xx' + yy;\n"[](#l1.25)
self.assertMultiLineEqual(output,[](#l1.26)
"1\n"[](#l1.27)
"< {old1}"[](#l1.28)
"> {new1}"[](#l1.29)
"{new1}"[](#l1.30)
"2\n"[](#l1.31)
"< {old2}"[](#l1.32)
"> {new2}"[](#l1.33)
"{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2)[](#l1.34)
)[](#l1.35)
- def test_alter_comments(self):
output = self.run_script([](#l1.38)
substfile=[](#l1.39)
"xx yy\n"[](#l1.40)
"*aa bb\n",[](#l1.41)
args=("-c", "-",),[](#l1.42)
input=[](#l1.43)
"/* xx altered */\n"[](#l1.44)
"int xx;\n"[](#l1.45)
"/* aa unaltered */\n"[](#l1.46)
"int aa;\n",[](#l1.47)
)[](#l1.48)
self.assertMultiLineEqual(output,[](#l1.49)
"1\n"[](#l1.50)
"< /* xx altered */\n"[](#l1.51)
"> /* yy altered */\n"[](#l1.52)
"/* yy altered */\n"[](#l1.53)
"2\n"[](#l1.54)
"< int xx;\n"[](#l1.55)
"> int yy;\n"[](#l1.56)
"int yy;\n"[](#l1.57)
"/* aa unaltered */\n"[](#l1.58)
"4\n"[](#l1.59)
"< int aa;\n"[](#l1.60)
"> int bb;\n"[](#l1.61)
"int bb;\n"[](#l1.62)
)[](#l1.63)
- def test_directory(self):
os.mkdir(test_support.TESTFN)[](#l1.66)
self.addCleanup(test_support.rmtree, test_support.TESTFN)[](#l1.67)
c_filename = os.path.join(test_support.TESTFN, "file.c")[](#l1.68)
with open(c_filename, "w") as file:[](#l1.69)
file.write("int xx;\n")[](#l1.70)
with open(os.path.join(test_support.TESTFN, "file.py"), "w") as file:[](#l1.71)
file.write("xx = 'unaltered'\n")[](#l1.72)
script = os.path.join(scriptsdir, "fixcid.py")[](#l1.73)
output = self.run_script(args=(test_support.TESTFN,))[](#l1.74)
self.assertMultiLineEqual(output,[](#l1.75)
"{}:\n"[](#l1.76)
"1\n"[](#l1.77)
'< int xx;\n'[](#l1.78)
'> int yy;\n'.format(c_filename)[](#l1.79)
)[](#l1.80)
- def run_script(self, input="", args=("-",), substfile="xx yy\n"):
substfilename = test_support.TESTFN + ".subst"[](#l1.83)
with open(substfilename, "w") as file:[](#l1.84)
file.write(substfile)[](#l1.85)
self.addCleanup(test_support.unlink, substfilename)[](#l1.86)
argv = ["fixcid.py", "-s", substfilename] + list(args)[](#l1.88)
script = os.path.join(scriptsdir, "fixcid.py")[](#l1.89)
with test_support.swap_attr(sys, "argv", argv), \[](#l1.90)
test_support.swap_attr(sys, "stdin", StringIO(input)), \[](#l1.91)
test_support.captured_stdout() as output:[](#l1.92)
try:[](#l1.93)
runpy.run_path(script, run_name="__main__")[](#l1.94)
except SystemExit as exit:[](#l1.95)
self.assertEqual(exit.code, 0)[](#l1.96)
return output.getvalue()[](#l1.97)
+ + def test_main(): test_support.run_unittest(*[obj for obj in globals().values() if isinstance(obj, type)])
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -191,6 +191,13 @@ Build
- 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 -------
--- a/Tools/scripts/fixcid.py +++ b/Tools/scripts/fixcid.py @@ -88,9 +88,9 @@ def main(): 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):
def recursedown(dirname): dbg('recursedown(%r)\n' % (dirname,)) @@ -168,6 +168,7 @@ def fix(filename): if filename == '-': return 0 # Done in filter mode f.close() if not g: return 0 # No changes
# Finishing touch -- move files @@ -193,21 +194,21 @@ def fix(filename):
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 @@ def fixline(line):
print '-->', repr(line)
i = 0[](#l3.56)
while i < len(line):[](#l3.57)
i = Program.search(line, i)[](#l3.58)
if i < 0: break[](#l3.59)
found = Program.group(0)[](#l3.60)
match = Program.search(line, i)[](#l3.61)
if match is None: break[](#l3.62)
i = match.start()[](#l3.63)
found = match.group(0)[](#l3.64)