[Py2to3] A group of small compatiblity code changes

* Replace the uses of long with int.
 * Replace im_func with __func__ as it has been provided for Python 3 forward-compatibility.
 * Fix next/__next__ for Python 3 compatibility.
 * Remove the long number literal
 * Ensure freespace() returns int
This commit is contained in:
Andrew Resch 2016-10-16 20:51:12 -07:00 committed by Calum Lind
parent 837dae242c
commit da51e3a3d5
7 changed files with 20 additions and 12 deletions

View File

@ -47,7 +47,7 @@ class PGReader(object):
if ver != 1 and ver != 2:
raise PGException(_("Invalid version") + " %d" % ver)
def next(self):
def __next__(self):
# Skip over the string
buf = -1
while buf != 0:
@ -64,5 +64,8 @@ class PGReader(object):
return (start, end)
# Python 2 compatibility
next = __next__
def close(self):
self.fd.close()

View File

@ -50,7 +50,7 @@ def todo_test(caller):
# Without the delugereporter the todo would print a stack trace, so in
# that case we rely only on skipTest
if os.environ.get("DELUGE_REPORTER", None):
getattr(caller, caller._testMethodName).im_func.todo = "To be fixed"
getattr(caller, caller._testMethodName).__func__.todo = "To be fixed"
filename = os.path.basename(traceback.extract_stack(None, 2)[0][0])
funcname = traceback.extract_stack(None, 2)[0][2]

View File

@ -1,4 +1,5 @@
import base64
import sys
from hashlib import sha1 as sha
import pytest
@ -259,7 +260,11 @@ class CoreTestCase(BaseTestCase):
def test_get_free_space(self):
space = self.core.get_free_space(".")
self.assertTrue(isinstance(space, (int, long)))
# get_free_space returns long on Python 2 (32-bit).
if sys.version_info >= (3, 0):
self.assertTrue(isinstance(space, int))
else:
self.assertTrue(isinstance(space, (int, long)))
self.assertTrue(space >= 0)
self.assertEquals(self.core.get_free_space("/someinvalidpath"), -1)

View File

@ -178,7 +178,7 @@ class Win32IcoFile(object):
# bitmap row data is aligned to word boundaries
w += 32 - (im.size[0] % 32)
# the total mask data is padded row size * height / bits per char
total_bytes = long((w * im.size[1]) / 8)
total_bytes = int((w * im.size[1]) / 8)
log.debug("tot=%d, off=%d, w=%d, size=%d", len(data), and_mask_offset, w, total_bytes)
self.buf.seek(and_mask_offset)

View File

@ -20,16 +20,16 @@ from deluge.ui.console.main import BaseCommand
log = logging.getLogger(__name__)
def atom(_next, token):
def atom(src, token):
"""taken with slight modifications from http://effbot.org/zone/simple-iterator-parser.htm"""
if token[1] == "(":
out = []
token = _next()
token = next(src)
while token[1] != ")":
out.append(atom(_next, token))
token = _next()
out.append(atom(src, token))
token = next(src)
if token[1] == ",":
token = _next()
token = next(src)
return tuple(out)
elif token[0] is tokenize.NUMBER or token[1] == "-":
try:
@ -58,7 +58,7 @@ def simple_eval(source):
src = cStringIO.StringIO(source).readline
src = tokenize.generate_tokens(src)
src = (token for token in src if token[0] is not tokenize.NL)
res = atom(src.next, src.next())
res = atom(src, next(src))
return res

View File

@ -274,7 +274,7 @@ class PeersTab(Tab):
import binascii
# Split out the :port
ip = ":".join(peer["ip"].split(":")[:-1])
ip_int = long(binascii.hexlify(socket.inet_pton(socket.AF_INET6, ip)), 16)
ip_int = int(binascii.hexlify(socket.inet_pton(socket.AF_INET6, ip)), 16)
peer_ip = "[%s]:%s" % (ip, peer["ip"].split(":")[-1])
if peer["seed"]:

View File

@ -86,7 +86,7 @@ def generate():
voffsets += [l2, o2 + valuestart]
offsets = koffsets + voffsets
output = struct.pack("Iiiiiii",
0x950412deL, # Magic
0x950412de, # Magic
0, # Version
len(keys), # # of entries
7 * 4, # start of key index