diff options
author | Calum Lind <calumlind@gmail.com> | 2017-03-05 09:29:51 +0000 |
---|---|---|
committer | Calum Lind <calumlind@gmail.com> | 2017-06-05 22:25:29 +0100 |
commit | 481f77934983284b67b7546470f52c9799bc6184 (patch) | |
tree | 6efeac6d6c038809f989385ec2e743ede0b2a4e1 /deluge/common.py | |
parent | ede0f710f814c62f9e6625c7b166fb7b1fcf5942 (diff) | |
download | deluge-481f77934983284b67b7546470f52c9799bc6184.tar.gz deluge-481f77934983284b67b7546470f52c9799bc6184.tar.bz2 deluge-481f77934983284b67b7546470f52c9799bc6184.zip |
[Python3] Fixes to make code backward compatible
* Continuation of updating code to Python 3 with Python 2 fallback.
* Using io.open allows files to be encoded and decoded automatically on write and read. This
maintains the python boundaries of unicode in code and bytes for output/files so less
explicit encoding or decoding.
* io.StringIO is the replacement for StringIO and will only accept unicode strings.
* io.BytesIO is used where bytes output is required by the enclosing method.
* Update bencode for full compatibility.
Diffstat (limited to 'deluge/common.py')
-rw-r--r-- | deluge/common.py | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/deluge/common.py b/deluge/common.py index 371c567e0..2d71df04a 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -62,6 +62,9 @@ TORRENT_STATE = [ 'Moving' ] +# The output formatting for json.dump +JSON_FORMAT = {'indent': 4, 'sort_keys': True, 'ensure_ascii': False} + PY2 = sys.version_info.major == 2 @@ -678,8 +681,12 @@ def create_magnet_uri(infohash, name=None, trackers=None): str: A magnet uri string. """ + try: + infohash = infohash.decode('hex') + except AttributeError: + pass - uri = [MAGNET_SCHEME, XT_BTIH_PARAM, base64.b32encode(infohash.decode('hex'))] + uri = [MAGNET_SCHEME, XT_BTIH_PARAM, base64.b32encode(infohash)] if name: uri.extend(['&', DN_PARAM, name]) if trackers: @@ -983,7 +990,7 @@ def create_localclient_account(append=False): with open(auth_file, 'a' if append else 'w') as _file: _file.write(':'.join([ 'localclient', - sha(str(random.random())).hexdigest(), + sha(str(random.random()).encode('utf8')).hexdigest(), str(AUTH_LEVEL_ADMIN) ]) + '\n') _file.flush() @@ -1090,7 +1097,14 @@ def unicode_argv(): # As a last resort, just default to utf-8 encoding = encoding or 'utf-8' - return [arg.decode(encoding) for arg in sys.argv] + arg_list = [] + for arg in sys.argv: + try: + arg_list.append(arg.decode(encoding)) + except AttributeError: + arg_list.append(arg) + + return arg_list def run_profiled(func, *args, **kwargs): @@ -1116,8 +1130,8 @@ def run_profiled(func, *args, **kwargs): print('Profile stats saved to %s' % output_file) else: import pstats - import StringIO - strio = StringIO.StringIO() + from io import StringIO + strio = StringIO() ps = pstats.Stats(profiler, stream=strio).sort_stats('cumulative') ps.print_stats() print(strio.getvalue()) |