Skip to content

Commit

Permalink
[Py2to3] A group of small compatiblity code changes
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
aresch authored and cas-- committed Oct 26, 2016
1 parent 837dae2 commit da51e3a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, filename):
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:
Expand All @@ -64,5 +64,8 @@ def next(self):

return (start, end)

# Python 2 compatibility
next = __next__

def close(self):
self.fd.close()
2 changes: 1 addition & 1 deletion deluge/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion deluge/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import sys
from hashlib import sha1 as sha

import pytest
Expand Down Expand Up @@ -259,7 +260,11 @@ def test_get_cache_status(self):

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)

Expand Down
2 changes: 1 addition & 1 deletion deluge/ui/Win32IconImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def frame(self, idx):
# 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)
Expand Down
12 changes: 6 additions & 6 deletions deluge/ui/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
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:
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion deluge/ui/gtkui/peers_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _on_get_torrent_status(self, status):
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"]:
Expand Down
2 changes: 1 addition & 1 deletion msgfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da51e3a

Please sign in to comment.