Skip to content

Commit

Permalink
[Py2to3] Force unicode_literals and fix related issues
Browse files Browse the repository at this point in the history
 * Added `from __future__ import unicode_literals` to every file so
   now all strings in code are forced to be unicode strings unless
   marked as byte string `b'str'` or encoded to byte string `'str'.encode('utf-8')`.

   This is a large change but we have been working towards the goal of unicode
   strings passed in the code so decoding external input and encoding
   output as byte strings (where applicable).

   Note that in Python 2 the `str` type still refers to byte strings.

 * Replaced the use of `str` for `basestring` in isinstance comparison as
   this was the original intention but breaks code when encoutering unicode strings.

 * Marked byte strings in gtkui as the conversion to utf8 is not always handled, mostly
   related to gobject signal names.
  • Loading branch information
cas-- committed Feb 22, 2017
1 parent 011afe3 commit 84802da
Show file tree
Hide file tree
Showing 272 changed files with 585 additions and 155 deletions.
2 changes: 2 additions & 0 deletions deluge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Deluge"""
from __future__ import unicode_literals

# this is a namespace package
import pkg_resources

Expand Down
2 changes: 2 additions & 0 deletions deluge/__rpcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

from new import classobj

from deluge.core.core import Core
Expand Down
1 change: 1 addition & 0 deletions deluge/_libtorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
>>> from deluge._libtorrent import lt
"""
from __future__ import unicode_literals

from deluge.common import VersionSplit, get_version

Expand Down
7 changes: 4 additions & 3 deletions deluge/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#

"""Common functions for various parts of Deluge to use."""
from __future__ import division, print_function
from __future__ import division, print_function, unicode_literals

import base64
import functools
Expand Down Expand Up @@ -800,7 +800,7 @@ def decode_string(s, encoding='utf8'):
"""
if not s:
return u''
return ''
elif isinstance(s, unicode):
return s

Expand All @@ -817,7 +817,7 @@ def decode_string(s, encoding='utf8'):
return s.decode(*l())
except UnicodeDecodeError:
pass
return u''
return ''


def utf8_encoded(s, encoding='utf8'):
Expand Down Expand Up @@ -871,6 +871,7 @@ def __init__(self, ver):
vs = ver.replace('_', '-').split('-')

self.version = [int(x) for x in vs[0].split('.') if x.isdigit()]
self.version_string = ''.join(str(x) for x in vs[0].split('.') if x.isdigit())
self.suffix = None
self.dev = False
if len(vs) > 1:
Expand Down
10 changes: 6 additions & 4 deletions deluge/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging
import traceback
from collections import defaultdict
Expand Down Expand Up @@ -305,7 +307,7 @@ def start(self, names=None):
# Start all the components if names is empty
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]

def on_depends_started(result, name):
Expand Down Expand Up @@ -339,7 +341,7 @@ def stop(self, names=None):
"""
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]

def on_dependents_stopped(result, name):
Expand Down Expand Up @@ -377,7 +379,7 @@ def pause(self, names=None):
"""
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]

deferreds = []
Expand All @@ -403,7 +405,7 @@ def resume(self, names=None):
"""
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]

deferreds = []
Expand Down
10 changes: 4 additions & 6 deletions deluge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@
version as this will be done internally.
"""
from __future__ import unicode_literals

import cPickle as pickle
import json
import logging
import os
import shutil

from deluge.common import get_default_config_dir, utf8_encoded
from deluge.common import decode_string, get_default_config_dir, utf8_encoded

log = logging.getLogger(__name__)
callLater = None # Necessary for the config tests
Expand Down Expand Up @@ -243,11 +244,8 @@ def get_item(self, key):
5
"""
if isinstance(self.__config[key], str):
try:
return self.__config[key].decode('utf8')
except UnicodeDecodeError:
return self.__config[key]
if isinstance(self.__config[key], basestring):
return decode_string(self.__config[key])
else:
return self.__config[key]

Expand Down
2 changes: 2 additions & 0 deletions deluge/configmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging
import os

Expand Down
1 change: 1 addition & 0 deletions deluge/core/alertmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
`:mod:EventManager` for similar functionality.
"""
from __future__ import unicode_literals

import logging

Expand Down
2 changes: 2 additions & 0 deletions deluge/core/authmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging
import os
import shutil
Expand Down
2 changes: 1 addition & 1 deletion deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# See LICENSE for more details.
#

from __future__ import division
from __future__ import division, unicode_literals

import base64
import glob
Expand Down
1 change: 1 addition & 0 deletions deluge/core/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#

"""The Deluge daemon"""
from __future__ import unicode_literals

import logging
import os
Expand Down
2 changes: 1 addition & 1 deletion deluge/core/daemon_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
from __future__ import print_function
from __future__ import print_function, unicode_literals

import os
import sys
Expand Down
2 changes: 2 additions & 0 deletions deluge/core/eventmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging

import deluge.component as component
Expand Down
2 changes: 2 additions & 0 deletions deluge/core/filtermanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging

import deluge.component as component
Expand Down
1 change: 1 addition & 0 deletions deluge/core/pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


"""PluginManager for Core"""
from __future__ import unicode_literals

import logging

Expand Down
2 changes: 2 additions & 0 deletions deluge/core/preferencesmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#


from __future__ import unicode_literals

import logging
import os
import random
Expand Down
1 change: 1 addition & 0 deletions deluge/core/rpcserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#

"""RPCServer Module"""
from __future__ import unicode_literals

import logging
import os
Expand Down
2 changes: 1 addition & 1 deletion deluge/core/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""

from __future__ import division
from __future__ import division, unicode_literals

import logging
import os
Expand Down
1 change: 1 addition & 0 deletions deluge/core/torrentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#

"""TorrentManager handles Torrent objects"""
from __future__ import unicode_literals

import cPickle
import datetime
Expand Down
2 changes: 2 additions & 0 deletions deluge/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import inspect
import re
import warnings
Expand Down
3 changes: 3 additions & 0 deletions deluge/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#


from __future__ import unicode_literals


class DelugeError(Exception):

def __new__(cls, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions deluge/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
and subsequently emitted to the clients.
"""
from __future__ import unicode_literals

known_events = {}

Expand Down
2 changes: 2 additions & 0 deletions deluge/httpdownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging
import os.path
import zlib
Expand Down
1 change: 1 addition & 0 deletions deluge/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#

"""Logging functions"""
from __future__ import unicode_literals

import inspect
import logging
Expand Down
2 changes: 1 addition & 1 deletion deluge/maketorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# See LICENSE for more details.
#

from __future__ import division
from __future__ import division, unicode_literals

import os
import sys
Expand Down
2 changes: 1 addition & 1 deletion deluge/metafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# See LICENSE for more details.
#

from __future__ import division
from __future__ import division, unicode_literals

import logging
import os.path
Expand Down
2 changes: 2 additions & 0 deletions deluge/path_chooser_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import os


Expand Down
1 change: 1 addition & 0 deletions deluge/pluginmanagerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


"""PluginManagerBase"""
from __future__ import unicode_literals

import logging
import os.path
Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

# this is a namespace package
import pkg_resources

Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

# this is a namespace package
import pkg_resources

Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/plugins/autoadd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

from deluge.plugins.init import PluginInitBase


Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/plugins/autoadd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import os

import pkg_resources
Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import base64
import logging
import os
Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/plugins/autoadd/gtkui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging
import os

Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/deluge/plugins/autoadd/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging

from deluge.plugins.pluginbase import WebPluginBase
Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/AutoAdd/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See LICENSE for more details.
#

from __future__ import unicode_literals

from setuptools import find_packages, setup

__plugin_name__ = 'AutoAdd'
Expand Down
2 changes: 2 additions & 0 deletions deluge/plugins/Blocklist/deluge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from __future__ import unicode_literals

# this is a namespace package
__import__('pkg_resources').declare_namespace(__name__)
Loading

0 comments on commit 84802da

Please sign in to comment.