Skip to content

Commit d9319e4

Browse files
committed
insert-example-code.py: Specify file encoding
The default file encoding is platform dependent in Python. Better tell which encoding is expected.
1 parent f5185ab commit d9319e4

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

docs/manual/insert_example_code.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
def process_source_file(source_directory, source_basename, outfile):
2121
source_filename = os.path.join(source_directory, source_basename)
22-
with open(source_filename, mode='r') as srcfile:
22+
with open(source_filename, mode='r', encoding='utf-8', errors='surrogateescape') as srcfile:
2323
outfile.write('<para>File: <filename>' + source_basename + '</filename></para>\n')
2424
outfile.write('<programlisting>\n<![CDATA[')
2525

@@ -38,9 +38,13 @@ def insert_example_code(examples_base_dir, input_xml_files, output_xml_file):
3838
if not isinstance(input_xml_files, list):
3939
input_xml_files = [input_xml_files]
4040

41-
with open(output_xml_file, mode='w') as outfile:
41+
# Assume that all files are UTF-8 encoded.
42+
# If illegal UTF-8 bytes in the range 0x80..0xff are encountered, they are
43+
# replaced by Unicode Private Use characters in the range 0xdc80..0xdcff
44+
# and restored to their original values when the file is rewritten.
45+
with open(output_xml_file, mode='w', encoding='utf-8', errors='surrogateescape') as outfile:
4246
for input_xml_file in input_xml_files:
43-
with open(input_xml_file, mode='r') as infile:
47+
with open(input_xml_file, mode='r', encoding='utf-8', errors='surrogateescape') as infile:
4448
for line in infile:
4549
# Print the line.
4650
outfile.write(line)

tools/build_scripts/tutorial-custom-cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
subcommand = sys.argv[1]
1414

1515
def insert_ex_code():
16-
# argv[2] argv[3] argv[4] argv[5]
17-
# <py_script_file> <examples_dir> <input_xml_file> <output_xml_file>
16+
# argv[2] argv[3] argv[4] argv[5]
17+
# <py_script_dir> <examples_dir> <input_xml_file> <output_xml_file>
1818

1919
# Search for insert_example_code.py first in <py_script_dir>.
2020
sys.path.insert(0, sys.argv[2])

0 commit comments

Comments
 (0)