[Py2to3] Force unicode_literals and fix related issues

* 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.
This commit is contained in:
Calum Lind 2017-02-12 11:26:31 +00:00
parent 011afe3e89
commit 84802da29b
272 changed files with 585 additions and 155 deletions

View File

@ -1,4 +1,6 @@
"""Deluge"""
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from new import classobj
from deluge.core.core import Core

View File

@ -15,6 +15,7 @@ Example:
>>> from deluge._libtorrent import lt
"""
from __future__ import unicode_literals
from deluge.common import VersionSplit, get_version

View File

@ -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
@ -800,7 +800,7 @@ def decode_string(s, encoding='utf8'):
"""
if not s:
return u''
return ''
elif isinstance(s, unicode):
return s
@ -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'):
@ -871,6 +871,7 @@ class VersionSplit(object):
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:

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import traceback
from collections import defaultdict
@ -305,7 +307,7 @@ class ComponentRegistry(object):
# 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):
@ -339,7 +341,7 @@ class ComponentRegistry(object):
"""
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]
def on_dependents_stopped(result, name):
@ -377,7 +379,7 @@ class ComponentRegistry(object):
"""
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]
deferreds = []
@ -403,7 +405,7 @@ class ComponentRegistry(object):
"""
if not names:
names = self.components.keys()
elif isinstance(names, str):
elif isinstance(names, basestring):
names = [names]
deferreds = []

View File

@ -39,6 +39,7 @@ this can only be done for the 'config file version' and not for the 'format'
version as this will be done internally.
"""
from __future__ import unicode_literals
import cPickle as pickle
import json
@ -46,7 +47,7 @@ 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
@ -243,11 +244,8 @@ class Config(object):
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]

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import os

View File

@ -15,6 +15,7 @@ This should typically only be used by the Core. Plugins should utilize the
`:mod:EventManager` for similar functionality.
"""
from __future__ import unicode_literals
import logging

View File

@ -8,6 +8,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import os
import shutil

View File

@ -8,7 +8,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import base64
import glob

View File

@ -8,6 +8,7 @@
#
"""The Deluge daemon"""
from __future__ import unicode_literals
import logging
import os

View File

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

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import deluge.component as component

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import deluge.component as component

View File

@ -9,6 +9,7 @@
"""PluginManager for Core"""
from __future__ import unicode_literals
import logging

View File

@ -8,6 +8,8 @@
#
from __future__ import unicode_literals
import logging
import os
import random

View File

@ -8,6 +8,7 @@
#
"""RPCServer Module"""
from __future__ import unicode_literals
import logging
import os

View File

@ -14,7 +14,7 @@ Attributes:
"""
from __future__ import division
from __future__ import division, unicode_literals
import logging
import os

View File

@ -8,6 +8,7 @@
#
"""TorrentManager handles Torrent objects"""
from __future__ import unicode_literals
import cPickle
import datetime

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import inspect
import re
import warnings

View File

@ -9,6 +9,9 @@
#
from __future__ import unicode_literals
class DelugeError(Exception):
def __new__(cls, *args, **kwargs):

View File

@ -14,6 +14,7 @@ This module describes the types of events that can be generated by the daemon
and subsequently emitted to the clients.
"""
from __future__ import unicode_literals
known_events = {}

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import os.path
import zlib

View File

@ -9,6 +9,7 @@
#
"""Logging functions"""
from __future__ import unicode_literals
import inspect
import logging

View File

@ -7,7 +7,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import os
import sys

View File

@ -11,7 +11,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import logging
import os.path

View File

@ -8,6 +8,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import os

View File

@ -9,6 +9,7 @@
"""PluginManagerBase"""
from __future__ import unicode_literals
import logging
import os.path

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import os
import pkg_resources

View File

@ -13,6 +13,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import base64
import logging
import os

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import os

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from deluge.plugins.pluginbase import WebPluginBase

View File

@ -13,6 +13,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'AutoAdd'

View File

@ -1,2 +1,4 @@
from __future__ import unicode_literals
# this is a namespace package
__import__('pkg_resources').declare_namespace(__name__)

View File

@ -1,2 +1,4 @@
from __future__ import unicode_literals
# this is a namespace package
__import__('pkg_resources').declare_namespace(__name__)

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import os.path
from functools import wraps
from sys import exc_info

View File

@ -8,7 +8,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import logging
import os

View File

@ -8,6 +8,8 @@
#
# pylint: disable=redefined-builtin
from __future__ import unicode_literals
import bz2
import gzip
import zipfile

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from .decompressers import BZipped2, GZipped, Zipped
from .readers import EmuleReader, PeerGuardianReader, SafePeerReader

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from datetime import datetime

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import gzip
import logging
import socket

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import re

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from deluge.plugins.pluginbase import WebPluginBase

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'Blocklist'

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import os.path
import pkg_resources

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import hashlib
import logging
import os

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import gtk

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from deluge.plugins.pluginbase import WebPluginBase

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'Execute'

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import os
import pkg_resources

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import errno
import logging
import os

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import gtk

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from deluge.plugins.pluginbase import WebPluginBase

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'Extractor'

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -15,6 +15,7 @@
torrent-label core plugin.
adds a status field for tracker.
"""
from __future__ import unicode_literals
import logging
import re

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from deluge import component # for systray

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import os

View File

@ -8,6 +8,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import gtk

View File

@ -8,6 +8,8 @@
#
from __future__ import unicode_literals
import logging
import gtk

View File

@ -10,7 +10,7 @@
# See LICENSE for more details.
#
from __future__ import print_function
from __future__ import print_function, unicode_literals
from deluge.ui.client import sclient

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import os

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'Label'

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from twisted.internet import defer

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import smtplib
from email.utils import formatdate

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from os.path import basename

View File

@ -6,6 +6,8 @@
# License: BSD - Please view the LICENSE file for additional information.
# ==============================================================================
from __future__ import unicode_literals
import logging
from twisted.internet import task

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
from deluge.plugins.pluginbase import WebPluginBase

View File

@ -12,6 +12,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'Notifications'

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -12,6 +12,9 @@
#
from __future__ import unicode_literals
def get_resource(filename):
import os
import pkg_resources

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging
import time

View File

@ -11,7 +11,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import logging

View File

@ -1,4 +1,3 @@
#
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
@ -11,6 +10,7 @@
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
from __future__ import unicode_literals
import logging

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from setuptools import find_packages, setup
__plugin_name__ = 'Scheduler'

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
# this is a namespace package
import pkg_resources

View File

@ -11,6 +11,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
from deluge.plugins.init import PluginInitBase

View File

@ -7,6 +7,8 @@
# See LICENSE for more details.
#
from __future__ import unicode_literals
import os.path
import pkg_resources

View File

@ -1,4 +1,3 @@
#
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Ian Martin <ianmartin@cantab.net>
@ -11,7 +10,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import logging
import time

View File

@ -1,4 +1,3 @@
#
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Ian Martin <ianmartin@cantab.net>
@ -15,7 +14,7 @@
port of old plugin by markybob.
"""
from __future__ import division
from __future__ import division, unicode_literals
import logging
import math

View File

@ -12,7 +12,7 @@
# See LICENSE for more details.
#
from __future__ import division
from __future__ import division, unicode_literals
import logging

View File

@ -4,7 +4,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 pytest
from twisted.internet import defer

Some files were not shown because too many files have changed in this diff Show More