[Py2to3] Replace deprecated base64.(de|en)codestring

* In Py3 base64.encodestring is deprecated so rather than use the
   Py3 only encodebytes instead use b64encode. The other advantage is
   that with issue a consistent TypeError is raised that we can catch.
This commit is contained in:
Calum Lind 2017-07-03 10:41:09 +01:00
commit 277576268c
15 changed files with 39 additions and 39 deletions

View file

@ -390,7 +390,7 @@ class Core(component.Component):
""" """
try: try:
filedump = b64decode(filedump) filedump = b64decode(filedump)
except Exception as ex: except TypeError as ex:
log.error('There was an error decoding the filedump string: %s', ex) log.error('There was an error decoding the filedump string: %s', ex)
try: try:
@ -958,7 +958,7 @@ class Core(component.Component):
try: try:
filedump = b64decode(filedump) filedump = b64decode(filedump)
except Exception as ex: except TypeError as ex:
log.error('There was an error decoding the filedump string!') log.error('There was an error decoding the filedump string!')
log.exception(ex) log.exception(ex)
return return

View file

@ -15,10 +15,10 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64
import logging import logging
import os import os
import shutil import shutil
from base64 import b64encode
from twisted.internet import reactor from twisted.internet import reactor
from twisted.internet.task import LoopingCall, deferLater from twisted.internet.task import LoopingCall, deferLater
@ -333,7 +333,7 @@ class Core(CorePluginBase):
d = component.get('Core').add_torrent_magnet(filedump.strip(), options) d = component.get('Core').add_torrent_magnet(filedump.strip(), options)
else: else:
d = component.get('Core').add_torrent_file_async( d = component.get('Core').add_torrent_file_async(
filename, base64.encodestring(filedump), options, filename, b64encode(filedump), options,
) )
d.addCallback(on_torrent_added, filename, filepath) d.addCallback(on_torrent_added, filename, filepath)
d.addErrback(fail_torrent_add, filepath, magnet) d.addErrback(fail_torrent_add, filepath, magnet)

View file

@ -7,7 +7,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64 from base64 import b64encode
from hashlib import sha1 as sha from hashlib import sha1 as sha
import pytest import pytest
@ -123,7 +123,7 @@ class CoreTestCase(BaseTestCase):
for f in filenames: for f in filenames:
filename = common.get_test_data_file(f) filename = common.get_test_data_file(f)
with open(filename, 'rb') as _file: with open(filename, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
files_to_add.append((filename, filedump, options)) files_to_add.append((filename, filedump, options))
errors = yield self.core.add_torrent_files(files_to_add) errors = yield self.core.add_torrent_files(files_to_add)
self.assertEqual(len(errors), 0) self.assertEqual(len(errors), 0)
@ -136,7 +136,7 @@ class CoreTestCase(BaseTestCase):
for f in filenames: for f in filenames:
filename = common.get_test_data_file(f) filename = common.get_test_data_file(f)
with open(filename, 'rb') as _file: with open(filename, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
files_to_add.append((filename, filedump, options)) files_to_add.append((filename, filedump, options))
errors = yield self.core.add_torrent_files(files_to_add) errors = yield self.core.add_torrent_files(files_to_add)
self.assertEqual(len(errors), 1) self.assertEqual(len(errors), 1)
@ -147,7 +147,7 @@ class CoreTestCase(BaseTestCase):
options = {} options = {}
filename = common.get_test_data_file('test.torrent') filename = common.get_test_data_file('test.torrent')
with open(filename, 'rb') as _file: with open(filename, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options) torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options)
# Get the info hash from the test.torrent # Get the info hash from the test.torrent
@ -215,7 +215,7 @@ class CoreTestCase(BaseTestCase):
options = {} options = {}
filename = common.get_test_data_file('test.torrent') filename = common.get_test_data_file('test.torrent')
with open(filename, 'rb') as _file: with open(filename, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options) torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options)
removed = self.core.remove_torrent(torrent_id, True) removed = self.core.remove_torrent(torrent_id, True)
self.assertTrue(removed) self.assertTrue(removed)
@ -229,12 +229,12 @@ class CoreTestCase(BaseTestCase):
options = {} options = {}
filename = common.get_test_data_file('test.torrent') filename = common.get_test_data_file('test.torrent')
with open(filename, 'rb') as _file: with open(filename, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options) torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options)
filename2 = common.get_test_data_file('unicode_filenames.torrent') filename2 = common.get_test_data_file('unicode_filenames.torrent')
with open(filename2, 'rb') as _file: with open(filename2, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id2 = yield self.core.add_torrent_file_async(filename2, filedump, options) torrent_id2 = yield self.core.add_torrent_file_async(filename2, filedump, options)
d = self.core.remove_torrents([torrent_id, torrent_id2], True) d = self.core.remove_torrents([torrent_id, torrent_id2], True)
@ -252,7 +252,7 @@ class CoreTestCase(BaseTestCase):
options = {} options = {}
filename = common.get_test_data_file('test.torrent') filename = common.get_test_data_file('test.torrent')
with open(filename, 'rb') as _file: with open(filename, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options) torrent_id = yield self.core.add_torrent_file_async(filename, filedump, options)
val = yield self.core.remove_torrents(['invalidid1', 'invalidid2', torrent_id], False) val = yield self.core.remove_torrents(['invalidid1', 'invalidid2', torrent_id], False)
self.assertEqual(len(val), 2) self.assertEqual(len(val), 2)

View file

@ -7,9 +7,9 @@
from __future__ import print_function, unicode_literals from __future__ import print_function, unicode_literals
import base64
import os import os
import time import time
from base64 import b64encode
from twisted.internet import reactor from twisted.internet import reactor
from twisted.internet.task import deferLater from twisted.internet.task import deferLater
@ -129,7 +129,7 @@ class TorrentTestCase(BaseTestCase):
options = {'seed_mode': True} options = {'seed_mode': True}
filename = common.get_test_data_file('test_torrent.file.torrent') filename = common.get_test_data_file('test_torrent.file.torrent')
with open(filename) as _file: with open(filename) as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = self.core.add_torrent_file(filename, filedump, options) torrent_id = self.core.add_torrent_file(filename, filedump, options)
torrent = self.core.torrentmanager.torrents[torrent_id] torrent = self.core.torrentmanager.torrents[torrent_id]
@ -147,7 +147,7 @@ class TorrentTestCase(BaseTestCase):
options = {'seed_mode': True, 'add_paused': True} options = {'seed_mode': True, 'add_paused': True}
filename = common.get_test_data_file('test_torrent.file.torrent') filename = common.get_test_data_file('test_torrent.file.torrent')
with open(filename) as _file: with open(filename) as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = self.core.add_torrent_file(filename, filedump, options) torrent_id = self.core.add_torrent_file(filename, filedump, options)
torrent = self.core.torrentmanager.torrents[torrent_id] torrent = self.core.torrentmanager.torrents[torrent_id]

View file

@ -7,8 +7,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64
import warnings import warnings
from base64 import b64encode
import pytest import pytest
from twisted.internet import defer from twisted.internet import defer
@ -46,7 +46,7 @@ class TorrentmanagerTestCase(BaseTestCase):
def test_remove_torrent(self): def test_remove_torrent(self):
filename = common.get_test_data_file('test.torrent') filename = common.get_test_data_file('test.torrent')
with open(filename) as _file: with open(filename) as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
torrent_id = yield self.core.add_torrent_file_async(filename, filedump, {}) torrent_id = yield self.core.add_torrent_file_async(filename, filedump, {})
self.assertTrue(self.core.torrentmanager.remove(torrent_id, False)) self.assertTrue(self.core.torrentmanager.remove(torrent_id, False))

View file

@ -10,8 +10,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64
import os import os
from base64 import b64encode
from twisted.internet import defer from twisted.internet import defer
@ -85,7 +85,7 @@ class Command(BaseCommand):
self.console.write('{!info!}Attempting to add torrent: %s' % path) self.console.write('{!info!}Attempting to add torrent: %s' % path)
filename = os.path.split(path)[-1] filename = os.path.split(path)[-1]
with open(path, 'rb') as _file: with open(path, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
deferreds.append( deferreds.append(
client.core.add_torrent_file_async( client.core.add_torrent_file_async(
filename, filedump, t_options, filename, filedump, t_options,

View file

@ -84,7 +84,7 @@ class Command(BaseCommand):
elif options.install: elif options.install:
import os.path import os.path
import base64 from base64 import b64encode
import shutil import shutil
filepath = options.install filepath = options.install
@ -102,7 +102,7 @@ class Command(BaseCommand):
if not client.is_localhost(): if not client.is_localhost():
# We need to send this plugin to the daemon # We need to send this plugin to the daemon
with open(filepath, 'rb') as _file: with open(filepath, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
try: try:
client.core.upload_plugin(filename, filedump) client.core.upload_plugin(filename, filedump)
client.core.rescan_plugins() client.core.rescan_plugins()

View file

@ -11,10 +11,10 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64
import glob import glob
import logging import logging
import os import os
from base64 import b64encode
import deluge.common import deluge.common
from deluge.ui.client import client from deluge.ui.client import client
@ -78,7 +78,7 @@ def add_torrent(t_file, options, success_cb, fail_cb, ress):
filename = os.path.split(f)[-1] filename = os.path.split(f)[-1]
with open(f, 'rb') as _file: with open(f, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
client.core.add_torrent_file_async( client.core.add_torrent_file_async(
filename, filedump, t_options, filename, filedump, t_options,

View file

@ -9,9 +9,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64
import logging import logging
import os import os
from base64 import b64encode
import deluge.common import deluge.common
import deluge.component as component import deluge.component as component
@ -393,7 +393,7 @@ class AddTorrents(BaseMode):
directory = os.path.join(*self.path_stack[:self.path_stack_pos]) directory = os.path.join(*self.path_stack[:self.path_stack_pos])
path = os.path.join(directory, filename) path = os.path.join(directory, filename)
with open(path, 'rb') as _file: with open(path, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
t_options = {} t_options = {}
if result['location']['value']: if result['location']['value']:
t_options['download_location'] = result['location']['value'] t_options['download_location'] = result['location']['value']

View file

@ -9,9 +9,9 @@
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import base64
import logging import logging
import os import os
from base64 import b64encode
from xml.sax.saxutils import escape as xml_escape from xml.sax.saxutils import escape as xml_escape
from xml.sax.saxutils import unescape as xml_unescape from xml.sax.saxutils import unescape as xml_unescape
@ -753,7 +753,7 @@ class AddTorrentDialog(component.Component):
else: else:
torrents_to_add.append(( torrents_to_add.append((
os.path.split(filename)[-1], os.path.split(filename)[-1],
base64.encodestring(self.infos[torrent_id]), b64encode(self.infos[torrent_id]),
options, options,
)) ))
row = self.torrent_liststore.iter_next(row) row = self.torrent_liststore.iter_next(row)

View file

@ -9,9 +9,9 @@
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import base64
import logging import logging
import os.path import os.path
from base64 import b64encode
import gtk import gtk
from gobject import TYPE_UINT64, idle_add from gobject import TYPE_UINT64, idle_add
@ -385,7 +385,7 @@ class CreateTorrentDialog(object):
if add_to_session: if add_to_session:
with open(target, 'rb') as _file: with open(target, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
client.core.add_torrent_file_async( client.core.add_torrent_file_async(
os.path.split(target)[-1], os.path.split(target)[-1],
filedump, filedump,

View file

@ -9,10 +9,10 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import base64
import logging import logging
import os import os
import sys import sys
from base64 import b64encode
from glob import glob from glob import glob
from tempfile import mkstemp from tempfile import mkstemp
@ -223,5 +223,5 @@ def process_args(args):
component.get('AddTorrentDialog').show(config['focus_add_dialog']) component.get('AddTorrentDialog').show(config['focus_add_dialog'])
else: else:
with open(path, 'rb') as _file: with open(path, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
client.core.add_torrent_file(os.path.split(path)[-1], filedump, None) client.core.add_torrent_file(os.path.split(path)[-1], filedump, None)

View file

@ -954,7 +954,7 @@ class Preferences(component.Component):
chooser.destroy() chooser.destroy()
return return
import base64 from base64 import b64encode
import shutil import shutil
filename = os.path.split(filepath)[1] filename = os.path.split(filepath)[1]
shutil.copyfile( shutil.copyfile(
@ -967,7 +967,7 @@ class Preferences(component.Component):
if not client.is_localhost(): if not client.is_localhost():
# We need to send this plugin to the daemon # We need to send this plugin to the daemon
with open(filepath, 'rb') as _file: with open(filepath, 'rb') as _file:
filedump = base64.encodestring(_file.read()) filedump = b64encode(_file.read())
client.core.upload_plugin(filename, filedump) client.core.upload_plugin(filename, filedump)
client.core.rescan_plugins() client.core.rescan_plugins()

View file

@ -143,11 +143,11 @@ class Auth(JSONComponent):
elif 'old_pwd_md5' in config.config: elif 'old_pwd_md5' in config.config:
# We are using the 1.1 webui auth method # We are using the 1.1 webui auth method
log.debug('Received a password via the 1.1 auth method') log.debug('Received a password via the 1.1 auth method')
from base64 import decodestring from base64 import b64decode
m = hashlib.md5() m = hashlib.md5()
m.update(decodestring(config['old_pwd_salt'])) m.update(b64decode(config['old_pwd_salt']))
m.update(password.encode('utf8')) m.update(password.encode('utf8'))
if m.digest() == decodestring(config['old_pwd_md5']): if m.digest() == b64decode(config['old_pwd_md5']):
# We want to move the password over to sha1 and remove # We want to move the password over to sha1 and remove
# the old passwords from the config file. # the old passwords from the config file.

View file

@ -9,13 +9,13 @@
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import base64
import cgi import cgi
import json import json
import logging import logging
import os import os
import shutil import shutil
import tempfile import tempfile
from base64 import b64encode
from types import FunctionType from types import FunctionType
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
@ -703,7 +703,7 @@ class WebApi(JSONComponent):
else: else:
filename = os.path.basename(torrent['path']) filename = os.path.basename(torrent['path'])
with open(torrent['path'], 'rb') as _file: with open(torrent['path'], 'rb') as _file:
fdump = base64.encodestring(_file.read()) fdump = b64encode(_file.read())
log.info( log.info(
'Adding torrent from file `%s` with options `%r`', 'Adding torrent from file `%s` with options `%r`',
filename, torrent['options'], filename, torrent['options'],
@ -903,7 +903,7 @@ class WebApi(JSONComponent):
client.core.rescan_plugins() client.core.rescan_plugins()
return True return True
with open(path, 'rb') as _file: with open(path, 'rb') as _file:
plugin_data = base64.encodestring(_file.read()) plugin_data = b64encode(_file.read())
def on_upload_complete(*args): def on_upload_complete(*args):
client.core.rescan_plugins() client.core.rescan_plugins()