19
19
20
20
def process_source_file (source_directory , source_basename , outfile ):
21
21
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 :
23
23
outfile .write ('<para>File: <filename>' + source_basename + '</filename></para>\n ' )
24
24
outfile .write ('<programlisting>\n <![CDATA[' )
25
25
@@ -38,9 +38,13 @@ def insert_example_code(examples_base_dir, input_xml_files, output_xml_file):
38
38
if not isinstance (input_xml_files , list ):
39
39
input_xml_files = [input_xml_files ]
40
40
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 :
42
46
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 :
44
48
for line in infile :
45
49
# Print the line.
46
50
outfile .write (line )
0 commit comments