Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdb847a

Browse files
committed
Issue #27952: Merge fixcid.py from 3.5
2 parents 1cb7aaa + b766538 commit bdb847a

3 files changed

Lines changed: 125 additions & 25 deletions

File tree

Lib/test/test_tools/test_fixcid.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'''Test Tools/scripts/fixcid.py.'''
2+
3+
from io import StringIO
4+
import os, os.path
5+
import runpy
6+
import sys
7+
from test import support
8+
from test.test_tools import skip_if_missing, scriptsdir
9+
import unittest
10+
11+
skip_if_missing()
12+
13+
class Test(unittest.TestCase):
14+
def test_parse_strings(self):
15+
old1 = 'int xx = "xx\\"xx"[xx];\n'
16+
old2 = "int xx = 'x\\'xx' + xx;\n"
17+
output = self.run_script(old1 + old2)
18+
new1 = 'int yy = "xx\\"xx"[yy];\n'
19+
new2 = "int yy = 'x\\'xx' + yy;\n"
20+
self.assertMultiLineEqual(output,
21+
"1\n"
22+
"< {old1}"
23+
"> {new1}"
24+
"{new1}"
25+
"2\n"
26+
"< {old2}"
27+
"> {new2}"
28+
"{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2)
29+
)
30+
31+
def test_alter_comments(self):
32+
output = self.run_script(
33+
substfile=
34+
"xx yy\n"
35+
"*aa bb\n",
36+
args=("-c", "-",),
37+
input=
38+
"/* xx altered */\n"
39+
"int xx;\n"
40+
"/* aa unaltered */\n"
41+
"int aa;\n",
42+
)
43+
self.assertMultiLineEqual(output,
44+
"1\n"
45+
"< /* xx altered */\n"
46+
"> /* yy altered */\n"
47+
"/* yy altered */\n"
48+
"2\n"
49+
"< int xx;\n"
50+
"> int yy;\n"
51+
"int yy;\n"
52+
"/* aa unaltered */\n"
53+
"4\n"
54+
"< int aa;\n"
55+
"> int bb;\n"
56+
"int bb;\n"
57+
)
58+
59+
def test_directory(self):
60+
os.mkdir(support.TESTFN)
61+
self.addCleanup(support.rmtree, support.TESTFN)
62+
c_filename = os.path.join(support.TESTFN, "file.c")
63+
with open(c_filename, "w") as file:
64+
file.write("int xx;\n")
65+
with open(os.path.join(support.TESTFN, "file.py"), "w") as file:
66+
file.write("xx = 'unaltered'\n")
67+
script = os.path.join(scriptsdir, "fixcid.py")
68+
output = self.run_script(args=(support.TESTFN,))
69+
self.assertMultiLineEqual(output,
70+
"{}:\n"
71+
"1\n"
72+
'< int xx;\n'
73+
'> int yy;\n'.format(c_filename)
74+
)
75+
76+
def run_script(self, input="", *, args=("-",), substfile="xx yy\n"):
77+
substfilename = support.TESTFN + ".subst"
78+
with open(substfilename, "w") as file:
79+
file.write(substfile)
80+
self.addCleanup(support.unlink, substfilename)
81+
82+
argv = ["fixcid.py", "-s", substfilename] + list(args)
83+
script = os.path.join(scriptsdir, "fixcid.py")
84+
with support.swap_attr(sys, "argv", argv), \
85+
support.swap_attr(sys, "stdin", StringIO(input)), \
86+
support.captured_stdout() as output:
87+
try:
88+
runpy.run_path(script, run_name="__main__")
89+
except SystemExit as exit:
90+
self.assertEqual(exit.code, 0)
91+
return output.getvalue()

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ Build
429429

430430
- Issue #21122: Fix LTO builds on OS X.
431431

432+
Tools/Demos
433+
-----------
434+
435+
- Issue #27952: Get Tools/scripts/fixcid.py working with Python 3 and the
436+
current "re" module, avoid invalid Python backslash escapes, and fix a bug
437+
parsing escaped C quote signs.
438+
432439
Windows
433440
-------
434441

Tools/scripts/fixcid.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def main():
8888
sys.exit(bad)
8989

9090
# Change this regular expression to select a different set of files
91-
Wanted = '^[a-zA-Z0-9_]+\.[ch]$'
91+
Wanted = r'^[a-zA-Z0-9_]+\.[ch]$'
9292
def wanted(name):
93-
return re.match(Wanted, name) >= 0
93+
return re.match(Wanted, name)
9494

9595
def recursedown(dirname):
9696
dbg('recursedown(%r)\n' % (dirname,))
@@ -168,6 +168,7 @@ def fix(filename):
168168
if filename == '-': return 0 # Done in filter mode
169169
f.close()
170170
if not g: return 0 # No changes
171+
g.close()
171172

172173
# Finishing touch -- move files
173174

@@ -193,21 +194,21 @@ def fix(filename):
193194

194195
# Tokenizing ANSI C (partly)
195196

196-
Identifier = '\(struct \)?[a-zA-Z_][a-zA-Z0-9_]+'
197-
String = '"\([^\n\\"]\|\\\\.\)*"'
198-
Char = '\'\([^\n\\\']\|\\\\.\)*\''
199-
CommentStart = '/\*'
200-
CommentEnd = '\*/'
197+
Identifier = '(struct )?[a-zA-Z_][a-zA-Z0-9_]+'
198+
String = r'"([^\n\\"]|\\.)*"'
199+
Char = r"'([^\n\\']|\\.)*'"
200+
CommentStart = r'/\*'
201+
CommentEnd = r'\*/'
201202

202203
Hexnumber = '0[xX][0-9a-fA-F]*[uUlL]*'
203204
Octnumber = '0[0-7]*[uUlL]*'
204205
Decnumber = '[1-9][0-9]*[uUlL]*'
205-
Intnumber = Hexnumber + '\|' + Octnumber + '\|' + Decnumber
206+
Intnumber = Hexnumber + '|' + Octnumber + '|' + Decnumber
206207
Exponent = '[eE][-+]?[0-9]+'
207-
Pointfloat = '\([0-9]+\.[0-9]*\|\.[0-9]+\)\(' + Exponent + '\)?'
208+
Pointfloat = r'([0-9]+\.[0-9]*|\.[0-9]+)(' + Exponent + r')?'
208209
Expfloat = '[0-9]+' + Exponent
209-
Floatnumber = Pointfloat + '\|' + Expfloat
210-
Number = Floatnumber + '\|' + Intnumber
210+
Floatnumber = Pointfloat + '|' + Expfloat
211+
Number = Floatnumber + '|' + Intnumber
211212

212213
# Anything else is an operator -- don't list this explicitly because of '/*'
213214

@@ -225,15 +226,16 @@ def initfixline():
225226

226227
def fixline(line):
227228
global Program
228-
## print '-->', repr(line)
229+
## print('-->', repr(line))
229230
i = 0
230231
while i < len(line):
231-
i = Program.search(line, i)
232-
if i < 0: break
233-
found = Program.group(0)
234-
## if Program is InsideCommentProgram: print '...',
235-
## else: print ' ',
236-
## print found
232+
match = Program.search(line, i)
233+
if match is None: break
234+
i = match.start()
235+
found = match.group(0)
236+
## if Program is InsideCommentProgram: print(end='... ')
237+
## else: print(end=' ')
238+
## print(found)
237239
if len(found) == 2:
238240
if found == '/*':
239241
Program = InsideCommentProgram
@@ -247,15 +249,15 @@ def fixline(line):
247249
print('Found in comment:', found)
248250
i = i + n
249251
continue
250-
if NotInComment.has_key(found):
251-
## print 'Ignored in comment:',
252-
## print found, '-->', subst
253-
## print 'Line:', line,
252+
if found in NotInComment:
253+
## print(end='Ignored in comment: ')
254+
## print(found, '-->', subst)
255+
## print('Line:', line, end='')
254256
subst = found
255257
## else:
256-
## print 'Substituting in comment:',
257-
## print found, '-->', subst
258-
## print 'Line:', line,
258+
## print(end='Substituting in comment: ')
259+
## print(found, '-->', subst)
260+
## print('Line:', line, end='')
259261
line = line[:i] + subst + line[i+n:]
260262
n = len(subst)
261263
i = i + n

0 commit comments

Comments
 (0)