diff options
author | Gaurav Juvekar <gauravjuvekar@gmail.com> | 2017-03-27 07:02:29 (GMT) |
---|---|---|
committer | Gaurav Juvekar <gauravjuvekar@gmail.com> | 2017-03-27 07:02:29 (GMT) |
commit | 52f4d73e7b8577660ccf9d8c0a9f6fb5837c2e28 (patch) | |
tree | 2bb003e6b01f19574d54be840f180a3333123a90 | |
parent | b34164950ee97eb57f28ed665137e95de70effb9 (diff) | |
download | SCons-52f4d73e7b8577660ccf9d8c0a9f6fb5837c2e28.zip SCons-52f4d73e7b8577660ccf9d8c0a9f6fb5837c2e28.tar.gz SCons-52f4d73e7b8577660ccf9d8c0a9f6fb5837c2e28.tar.bz2 |
Make --tree=all work with Python 3
The codecs module is used which is distributed in the python standard library.
-rw-r--r-- | src/engine/SCons/Util.py | 14 | ||||
-rw-r--r-- | test/option--tree.py | 9 |
2 files changed, 21 insertions, 2 deletions
diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index ecdd77f..2d8e75a 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -31,6 +31,7 @@ import sys import copy import re import types +import codecs try: from UserDict import UserDict @@ -288,7 +289,18 @@ def print_tree(root, child_func, prune=0, showtags=0, margin=[0], visited=None): or in the whole tree if prune. """ - rname = str(root).encode('string-escape') + rname = str(root) + if sys.version_info.major < 3: + # Python 2 UTF-8 encoded str are str. escape_encode is a str to str + # encoding + rname = codecs.escape_encode(rname)[0] + else: + # Python 3 UTF-8 encoded str are bytes. escape_encode is a byte to byte + # encoding here. + rname = rname.encode('utf-8') + rname = codecs.escape_encode(rname)[0] + # Finally, we need a string again. + rname = rname.decode('ascii') # Initialize 'visited' dict, if required if visited is None: diff --git a/test/option--tree.py b/test/option--tree.py index 1c1832b..a9618d8 100644 --- a/test/option--tree.py +++ b/test/option--tree.py @@ -57,7 +57,14 @@ test.write('SConstruct', """ env = Environment() env.Tool("textfile") -env.Textfile("Foo", unichr(0xe7).encode('utf-8')) +try: + # Python 2 + write = unichr(0xe7).encode('utf-8') +except NameError: + # Python 3 + # str is utf-8 by default + write = chr(0xe7) +env.Textfile("Foo", write) """) test.run(arguments = '-Q --tree=all', |