File tree 2 files changed +18
-6
lines changed
2 files changed +18
-6
lines changed Original file line number Diff line number Diff line change 24
24
from __future__ import with_statement
25
25
import collections
26
26
import inspect
27
+ import io
27
28
import keyword
28
29
import pydoc
29
30
import types
31
+ from six .moves import range
30
32
31
33
from pygments .token import Token
32
34
@@ -280,13 +282,25 @@ def is_callable(obj):
280
282
281
283
282
284
def get_encoding (obj ):
285
+ """Try to obtain encoding information of the source of an object."""
283
286
for line in inspect .findsource (obj )[0 ][:2 ]:
284
287
m = get_encoding_re .search (line )
285
288
if m :
286
289
return m .group (1 )
287
290
return 'ascii'
288
291
289
292
293
+ def get_encoding_file (fname ):
294
+ """Try to obtain encoding information from a Python source file."""
295
+ with io .open (fname , 'rt' , encoding = 'ascii' , errors = 'ignore' ) as f :
296
+ for unused in range (2 ):
297
+ line = f .readline ()
298
+ match = get_encoding_re .search (line )
299
+ if match :
300
+ return match .group (1 )
301
+ return 'ascii'
302
+
303
+
290
304
def get_source_unicode (obj ):
291
305
"""Returns a decoded source of object"""
292
306
if py3 :
Original file line number Diff line number Diff line change 24
24
import code
25
25
import errno
26
26
import inspect
27
+ import io
27
28
import os
28
29
import pkgutil
29
30
import pydoc
@@ -406,12 +407,9 @@ def startup(self):
406
407
"""
407
408
filename = os .environ .get ('PYTHONSTARTUP' )
408
409
if filename :
409
- with open (filename , 'r' ) as f :
410
- if py3 :
411
- self .interp .runsource (f .read (), filename , 'exec' )
412
- else :
413
- self .interp .runsource (f .read (), filename , 'exec' ,
414
- encode = False )
410
+ encoding = inspection .get_encoding_file (filename )
411
+ with io .open (filename , 'rt' , encoding = encoding ) as f :
412
+ self .interp .runsource (f .read (), filename , 'exec' )
415
413
416
414
def current_string (self , concatenate = False ):
417
415
"""If the line ends in a string get it, otherwise return ''"""
You can’t perform that action at this time.
0 commit comments