2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2024-11-22 18:07:12 +01:00

Ensure that Python scripts are read in the desired encoding

This commit is contained in:
Thomas Nagy 2014-09-28 01:30:00 +02:00
parent fcc950dd46
commit 388d94cca2
No known key found for this signature in database
GPG Key ID: 67A565EDFDF90E64
3 changed files with 30 additions and 10 deletions

View File

@ -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)

View File

@ -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):
"""

View File

@ -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: