@@ -96,6 +96,23 @@ def write_unicode(file, text):
9696 text = text .encode (ENCODING , 'backslashreplace' )
9797 file .write (text )
9898
99+ def os_fsencode (filename ):
100+ if not isinstance (filename , unicode ):
101+ return filename
102+ encoding = sys .getfilesystemencoding ()
103+ if encoding == 'mbcs' :
104+ # mbcs doesn't support surrogateescape
105+ return filename .encode (encoding )
106+ encoded = []
107+ for char in filename :
108+ # surrogateescape error handler
109+ if 0xDC80 <= ord (char ) <= 0xDCFF :
110+ byte = chr (ord (char ) - 0xDC00 )
111+ else :
112+ byte = char .encode (encoding )
113+ encoded .append (byte )
114+ return '' .join (encoded )
115+
99116class StringTruncated (RuntimeError ):
100117 pass
101118
@@ -887,7 +904,8 @@ def current_line(self):
887904 newline character'''
888905 if self .is_optimized_out ():
889906 return '(frame information optimized out)'
890- with open (self .filename (), 'r' ) as f :
907+ filename = self .filename ()
908+ with open (os_fsencode (filename ), 'r' ) as f :
891909 all_lines = f .readlines ()
892910 # Convert from 1-based current_line_num to 0-based list offset:
893911 return all_lines [self .current_line_num ()- 1 ]
@@ -1463,7 +1481,7 @@ def invoke(self, args, from_tty):
14631481 if start < 1 :
14641482 start = 1
14651483
1466- with open (filename , 'r' ) as f :
1484+ with open (os_fsencode ( filename ) , 'r' ) as f :
14671485 all_lines = f .readlines ()
14681486 # start and end are 1-based, all_lines is 0-based;
14691487 # so [start-1:end] as a python slice gives us [start, end] as a
0 commit comments