From 388d94cca2c3b65ba0b8fdec9c2d3bdaf5fd5a1a Mon Sep 17 00:00:00 2001 From: Thomas Nagy Date: Sun, 28 Sep 2014 01:30:00 +0200 Subject: [PATCH] Ensure that Python scripts are read in the desired encoding --- waflib/Context.py | 10 +++++----- waflib/Task.py | 20 +++++++++++++++++--- waflib/Utils.py | 10 ++++++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/waflib/Context.py b/waflib/Context.py index 1aaee6ed..e5aa0955 100644 --- a/waflib/Context.py +++ b/waflib/Context.py @@ -248,7 +248,7 @@ class Context(ctx): if self.cur_script: self.path = self.cur_script.parent - def recurse(self, dirs, name=None, mandatory=True, once=True): + def recurse(self, dirs, name=None, mandatory=True, once=True, encoding=None): """ Run user code from the supplied list of directories. The directories can be either absolute, or relative to the directory @@ -283,7 +283,7 @@ class Context(ctx): cache[node] = True self.pre_recurse(node) try: - function_code = node.read('rU') + function_code = node.read('rU', encoding) exec(compile(function_code, node.abspath(), 'exec'), self.exec_dict) finally: self.post_recurse(node) @@ -294,7 +294,7 @@ class Context(ctx): cache[tup] = True self.pre_recurse(node) try: - wscript_module = load_module(node.abspath()) + wscript_module = load_module(node.abspath(), encoding=encoding) user_function = getattr(wscript_module, (name or self.fun), None) if not user_function: if not mandatory: @@ -597,7 +597,7 @@ Dictionary holding already loaded modules, keyed by their absolute path. The modules are added automatically by :py:func:`waflib.Context.load_module` """ -def load_module(path): +def load_module(path, encoding=None): """ Load a source file as a python module. @@ -613,7 +613,7 @@ def load_module(path): module = imp.new_module(WSCRIPT_FILE) try: - code = Utils.readf(path, m='rU') + code = Utils.readf(path, m='rU', encoding=encoding) except (IOError, OSError): raise Errors.WafError('Could not read the file %r' % path) diff --git a/waflib/Task.py b/waflib/Task.py index ee435086..4c7be4af 100644 --- a/waflib/Task.py +++ b/waflib/Task.py @@ -484,15 +484,15 @@ class Task(TaskBase): try: return self.uid_ except AttributeError: - # this is not a real hot zone, but we want to avoid surprises here m = Utils.md5() up = m.update - up(self.__class__.__name__.encode()) + up(self.__class__.__name__) for x in self.inputs + self.outputs: - up(x.abspath().encode()) + up(x.abspath()) self.uid_ = m.digest() return self.uid_ + def set_inputs(self, inp): """ Append the nodes to the *inputs* @@ -824,6 +824,20 @@ class Task(TaskBase): if not tsk.hasrun: #print "task is not ready..." raise Errors.TaskNotReady('not ready') +if sys.hexversion > 0x3000000: + def uid(self): + try: + return self.uid_ + except AttributeError: + m = Utils.md5() + up = m.update + up(self.__class__.__name__.encode('iso8859-1')) + for x in self.inputs + self.outputs: + up(x.abspath().encode('iso8859-1')) + self.uid_ = m.digest() + return self.uid_ + uid.__doc__ = Task.uid.__doc__ + Task.uid = uid def is_before(t1, t2): """ diff --git a/waflib/Utils.py b/waflib/Utils.py index 8a5b8701..ed1f632d 100644 --- a/waflib/Utils.py +++ b/waflib/Utils.py @@ -167,7 +167,10 @@ def readf(fname, m='r', encoding='ISO8859-1'): txt = f.read() finally: f.close() - txt = txt.decode(encoding) + if encoding: + txt = txt.decode(encoding) + else: + txt = txt.decode() else: f = open(fname, m) try: @@ -254,7 +257,10 @@ def readf_win32(f, m='r', encoding='ISO8859-1'): txt = f.read() finally: f.close() - txt = txt.decode(encoding) + if encoding: + txt = txt.decode(encoding) + else: + txt = txt.decode() else: f = os.fdopen(fd, m) try: