cpython: 8dde3dcab1e8 (original) (raw)
Mercurial > cpython
changeset 103618:8dde3dcab1e8
Issue #27952: Merge fixcid.py from 3.5 [#27952]
Martin Panter vadmium+py@gmail.com | |
---|---|
date | Sun, 11 Sep 2016 10:06:38 +0000 |
parents | 9a75fa28bd0a(current diff)ca68bb597df9(diff) |
children | dd046963bd42 |
files | Misc/NEWS |
diffstat | 3 files changed, 125 insertions(+), 25 deletions(-)[+] [-] Lib/test/test_tools/test_fixcid.py 91 Misc/NEWS 7 Tools/scripts/fixcid.py 52 |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/Lib/test/test_tools/test_fixcid.py @@ -0,0 +1,91 @@ +'''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):
old1 = 'int xx = "xx\\"xx"[xx];\n'[](#l1.19)
old2 = "int xx = 'x\\'xx' + xx;\n"[](#l1.20)
output = self.run_script(old1 + old2)[](#l1.21)
new1 = 'int yy = "xx\\"xx"[yy];\n'[](#l1.22)
new2 = "int yy = 'x\\'xx' + yy;\n"[](#l1.23)
self.assertMultiLineEqual(output,[](#l1.24)
"1\n"[](#l1.25)
"< {old1}"[](#l1.26)
"> {new1}"[](#l1.27)
"{new1}"[](#l1.28)
"2\n"[](#l1.29)
"< {old2}"[](#l1.30)
"> {new2}"[](#l1.31)
"{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2)[](#l1.32)
)[](#l1.33)
- def test_alter_comments(self):
output = self.run_script([](#l1.36)
substfile=[](#l1.37)
"xx yy\n"[](#l1.38)
"*aa bb\n",[](#l1.39)
args=("-c", "-",),[](#l1.40)
input=[](#l1.41)
"/* xx altered */\n"[](#l1.42)
"int xx;\n"[](#l1.43)
"/* aa unaltered */\n"[](#l1.44)
"int aa;\n",[](#l1.45)
)[](#l1.46)
self.assertMultiLineEqual(output,[](#l1.47)
"1\n"[](#l1.48)
"< /* xx altered */\n"[](#l1.49)
"> /* yy altered */\n"[](#l1.50)
"/* yy altered */\n"[](#l1.51)
"2\n"[](#l1.52)
"< int xx;\n"[](#l1.53)
"> int yy;\n"[](#l1.54)
"int yy;\n"[](#l1.55)
"/* aa unaltered */\n"[](#l1.56)
"4\n"[](#l1.57)
"< int aa;\n"[](#l1.58)
"> int bb;\n"[](#l1.59)
"int bb;\n"[](#l1.60)
)[](#l1.61)
- def test_directory(self):
os.mkdir(support.TESTFN)[](#l1.64)
self.addCleanup(support.rmtree, support.TESTFN)[](#l1.65)
c_filename = os.path.join(support.TESTFN, "file.c")[](#l1.66)
with open(c_filename, "w") as file:[](#l1.67)
file.write("int xx;\n")[](#l1.68)
with open(os.path.join(support.TESTFN, "file.py"), "w") as file:[](#l1.69)
file.write("xx = 'unaltered'\n")[](#l1.70)
script = os.path.join(scriptsdir, "fixcid.py")[](#l1.71)
output = self.run_script(args=(support.TESTFN,))[](#l1.72)
self.assertMultiLineEqual(output,[](#l1.73)
"{}:\n"[](#l1.74)
"1\n"[](#l1.75)
'< int xx;\n'[](#l1.76)
'> int yy;\n'.format(c_filename)[](#l1.77)
)[](#l1.78)
- def run_script(self, input="", *, args=("-",), substfile="xx yy\n"):
substfilename = support.TESTFN + ".subst"[](#l1.81)
with open(substfilename, "w") as file:[](#l1.82)
file.write(substfile)[](#l1.83)
self.addCleanup(support.unlink, substfilename)[](#l1.84)
argv = ["fixcid.py", "-s", substfilename] + list(args)[](#l1.86)
script = os.path.join(scriptsdir, "fixcid.py")[](#l1.87)
with support.swap_attr(sys, "argv", argv), \[](#l1.88)
support.swap_attr(sys, "stdin", StringIO(input)), \[](#l1.89)
support.captured_stdout() as output:[](#l1.90)
try:[](#l1.91)
runpy.run_path(script, run_name="__main__")[](#l1.92)
except SystemExit as exit:[](#l1.93)
self.assertEqual(exit.code, 0)[](#l1.94)
return output.getvalue()[](#l1.95)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -429,6 +429,13 @@ Build
- Issue #21122: Fix LTO builds on OS X. +Tools/Demos +----------- + +- Issue #27952: Get Tools/scripts/fixcid.py working with Python 3 and 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 '/*'
@@ -225,15 +226,16 @@ def initfixline(): def fixline(line): global Program -## print '-->', repr(line) +## print('-->', repr(line)) i = 0 while i < len(line):
i = Program.search(line, i)[](#l3.62)
if i < 0: break[](#l3.63)
found = Program.group(0)[](#l3.64)
-## if Program is InsideCommentProgram: print '...', -## else: print ' ', -## print found
match = Program.search(line, i)[](#l3.68)
if match is None: break[](#l3.69)
i = match.start()[](#l3.70)
found = match.group(0)[](#l3.71)
+## if Program is InsideCommentProgram: print(end='... ') +## else: print(end=' ') +## print(found) if len(found) == 2: if found == '/*': Program = InsideCommentProgram @@ -247,15 +249,15 @@ def fixline(line): print('Found in comment:', found) i = i + n continue
if NotInComment.has_key(found):[](#l3.82)
-## print 'Ignored in comment:', -## print found, '-->', subst -## print 'Line:', line,
if found in NotInComment:[](#l3.86)
+## 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