summaryrefslogtreecommitdiffstats
path: root/Lib/keyword.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-04-25 16:01:36 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-04-25 16:01:36 (GMT)
commitf0f7ceae3c94df171d94da1055236db1a93d85a9 (patch)
treef4ab400961df3124ee3ba8a9364793366b1ade24 /Lib/keyword.py
parent523809259251f5f2d3b7d1d291af850fd353b0eb (diff)
downloadcpython-f0f7ceae3c94df171d94da1055236db1a93d85a9.zip
cpython-f0f7ceae3c94df171d94da1055236db1a93d85a9.tar.gz
cpython-f0f7ceae3c94df171d94da1055236db1a93d85a9.tar.bz2
17830: preserve line endings of original file when updating keywords.
This fixes the test failures on Windows from the new tests, and includes test fixes as well as the module fix.
Diffstat (limited to 'Lib/keyword.py')
-rwxr-xr-xLib/keyword.py22
1 files changed, 12 insertions, 10 deletions
diff --git a/Lib/keyword.py b/Lib/keyword.py
index 91528f7..6e1e882 100755
--- a/Lib/keyword.py
+++ b/Lib/keyword.py
@@ -60,6 +60,12 @@ def main():
if len(args) > 1: optfile = args[1]
else: optfile = "Lib/keyword.py"
+ # load the output skeleton from the target, taking care to preserve its
+ # newline convention.
+ with open(optfile, newline='') as fp:
+ format = fp.readlines()
+ nl = format[0][len(format[0].strip()):] if format else '\n'
+
# scan the source file for keywords
with open(iptfile) as fp:
strprog = re.compile('"([^"]+)"')
@@ -68,25 +74,21 @@ def main():
if '{1, "' in line:
match = strprog.search(line)
if match:
- lines.append(" '" + match.group(1) + "',\n")
+ lines.append(" '" + match.group(1) + "'," + nl)
lines.sort()
- # load the output skeleton from the target
- with open(optfile) as fp:
- format = fp.readlines()
-
- # insert the lines of keywords
+ # insert the lines of keywords into the skeleton
try:
- start = format.index("#--start keywords--\n") + 1
- end = format.index("#--end keywords--\n")
+ start = format.index("#--start keywords--" + nl) + 1
+ end = format.index("#--end keywords--" + nl)
format[start:end] = lines
except ValueError:
sys.stderr.write("target does not contain format markers\n")
sys.exit(1)
# write the output file
- with open(optfile, 'w') as fp:
- fp.write(''.join(format))
+ with open(optfile, 'w', newline='') as fp:
+ fp.writelines(format)
if __name__ == "__main__":
main()