From cd9d08f3fe9f705d81e68105a3a1d3b50ffd7085 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 9 May 2008 08:14:38 +0200 Subject: [PATCH] moved code for filename encoding to Utils.py to make it reusable elsewhere --- Cython/Compiler/Main.py | 11 +---------- Cython/Utils.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Cython/Compiler/Main.py b/Cython/Compiler/Main.py index a0e84cac..efdedc69 100644 --- a/Cython/Compiler/Main.py +++ b/Cython/Compiler/Main.py @@ -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") diff --git a/Cython/Utils.py b/Cython/Utils.py index 8faa29eb..12ba39ff 100644 --- a/Cython/Utils.py +++ b/Cython/Utils.py @@ -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): -- 2.26.2