File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -73,13 +73,29 @@ def exec_code_object(code, global_map):
73
73
import ConfigParser as configparser
74
74
75
75
# Python 3.2 provides `tokenize.open`, the best way to open source files.
76
+ import tokenize
76
77
try :
77
- import tokenize
78
78
open_source = tokenize .open # pylint: disable=E1101
79
79
except AttributeError :
80
- def open_source (fname ):
81
- """Open a source file the best way."""
82
- return open (fname , "rU" )
80
+ try :
81
+ detect_encoding = tokenize .detect_encoding
82
+ except AttributeError :
83
+ def open_source (fname ):
84
+ """Open a source file the best way."""
85
+ return open (fname , "rU" )
86
+ else :
87
+ from io import TextIOWrapper
88
+ # Copied from the 3.2 stdlib:
89
+ def open_source (fname ):
90
+ """Open a file in read only mode using the encoding detected by
91
+ detect_encoding().
92
+ """
93
+ buffer = open (fname , 'rb' )
94
+ encoding , lines = detect_encoding (buffer .readline )
95
+ buffer .seek (0 )
96
+ text = TextIOWrapper (buffer , encoding , line_buffering = True )
97
+ text .mode = 'r'
98
+ return text
83
99
84
100
# Python 3.x is picky about bytes and strings, so provide methods to
85
101
# get them right, and make them no-ops in 2.x
You can’t perform that action at this time.
0 commit comments