Commit cd9d08f3 authored by Stefan Behnel's avatar Stefan Behnel

moved code for filename encoding to Utils.py to make it reusable elsewhere

parent 9f23dd4e
......@@ -138,16 +138,7 @@ class Context:
return scope
def parse(self, source_filename, type_names, pxd, full_module_name):
try:
name = source_filename
if not isinstance(source_filename, unicode):
filename_encoding = sys.getfilesystemencoding()
if filename_encoding is None:
filename_encoding = sys.getdefaultencoding()
name = source_filename.decode(filename_encoding)
except UnicodeDecodeError:
pass
name = Utils.encode_filename(source_filename)
# Parse the given source file and return a parse tree.
try:
f = Utils.open_source_file(source_filename, "rU")
......
......@@ -35,6 +35,18 @@ def castrate_file(path, st):
# support for source file encoding detection and unicode decoding
def encode_filename(filename):
if isinstance(filename, unicode):
return filename
try:
filename_encoding = sys.getfilesystemencoding()
if filename_encoding is None:
filename_encoding = sys.getdefaultencoding()
filename = filename.decode(filename_encoding)
except UnicodeDecodeError:
pass
return filename
_match_file_encoding = re.compile(u"coding[:=]\s*([-\w.]+)").search
def detect_file_encoding(source_filename):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment