Tests pass on both Python 2 and Python 3.

This commit is contained in:
Itamar Turner-Trauring 2020-07-02 14:26:35 -04:00
parent c207a0c932
commit 70a029fb81
3 changed files with 27 additions and 16 deletions

View File

@ -54,7 +54,9 @@ install_requires = [
# * foolscap >= 0.12.5 has ConnectionInfo and ReconnectionInfo
# * foolscap >= 0.12.6 has an i2p.sam_endpoint() that takes kwargs
# * foolscap 0.13.2 drops i2p support completely
"foolscap == 0.13.1",
# * foolscap >= 20.4 is necessary for Python 3
"foolscap == 0.13.1; ; python_version < '3.0'",
"foolscap >= 20.4.0; ; python_version > '3.0'",
# * cryptography 2.6 introduced some ed25519 APIs we rely on. Note that
# Twisted[conch] also depends on cryptography and Twisted[tls]

View File

@ -6,7 +6,7 @@ from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import *
from past.builtins import long
from twisted.trial import unittest
from allmydata.util import humanreadable
@ -26,8 +26,9 @@ class HumanReadable(unittest.TestCase):
self.failUnlessEqual(hr(self.test_repr),
"<bound method HumanReadable.test_repr of <allmydata.test.test_humanreadable.HumanReadable testMethod=test_repr>>")
self.failUnlessEqual(hr(long(1)), "1")
self.failUnlessEqual(hr(10**40),
"100000000000000000...000000000000000000")
self.assertIn(hr(10**40),
["100000000000000000...000000000000000000",
"100000000000000000...0000000000000000000"])
self.failUnlessEqual(hr(self), "<allmydata.test.test_humanreadable.HumanReadable testMethod=test_repr>")
self.failUnlessEqual(hr([1,2]), "[1, 2]")
self.failUnlessEqual(hr({1:2}), "{1:2}")
@ -50,4 +51,5 @@ class HumanReadable(unittest.TestCase):
except Exception as e:
self.failUnless(
hr(e) == "<NoArgumentException>" # python-2.4
or hr(e) == "NoArgumentException()") # python-2.5
or hr(e) == "NoArgumentException()" # python-2.5
or hr(e) == "<NoArgumentException: ()>", hr(e)) # python-3

View File

@ -1,5 +1,12 @@
import exceptions, os
from repr import Repr
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
import os
from reprlib import Repr
class BetterRepr(Repr, object):
def __init__(self):
@ -14,21 +21,21 @@ class BetterRepr(Repr, object):
self.maxother = 300
def repr_function(self, obj, level):
if hasattr(obj, 'func_code'):
return '<' + obj.func_name + '() at ' + os.path.basename(obj.func_code.co_filename) + ':' + str(obj.func_code.co_firstlineno) + '>'
if hasattr(obj, '__code__'):
return '<' + obj.__name__ + '() at ' + os.path.basename(obj.__code__.co_filename) + ':' + str(obj.__code__.co_firstlineno) + '>'
else:
return '<' + obj.func_name + '() at (builtin)'
return '<' + obj.__name__ + '() at (builtin)'
def repr_instance_method(self, obj, level):
if hasattr(obj, 'func_code'):
return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at ' + os.path.basename(obj.im_func.func_code.co_filename) + ':' + str(obj.im_func.func_code.co_firstlineno) + '>'
if hasattr(obj, '__code__'):
return '<' + obj.__self__.__class__.__name__ + '.' + obj.__func__.__name__ + '() at ' + os.path.basename(obj.__func__.__code__.co_filename) + ':' + str(obj.__func__.__code__.co_firstlineno) + '>'
else:
return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at (builtin)'
return '<' + obj.__self__.__class__.__name__ + '.' + obj.__func__.__name__ + '() at (builtin)'
def repr_long(self, obj, level):
s = repr(obj) # XXX Hope this isn't too slow...
if len(s) > self.maxlong:
i = max(0, (self.maxlong-3)/2)
i = max(0, (self.maxlong-3) // 2)
j = max(0, self.maxlong-3-i)
s = s[:i] + '...' + s[len(s)-j:]
if s[-1] == 'L':
@ -43,7 +50,7 @@ class BetterRepr(Repr, object):
on it. If it is an instance of list call self.repr_list() on it. Else
call Repr.repr_instance().
"""
if isinstance(obj, exceptions.Exception):
if isinstance(obj, Exception):
# Don't cut down exception strings so much.
tms = self.maxstring
self.maxstring = max(512, tms * 4)
@ -91,7 +98,7 @@ class BetterRepr(Repr, object):
if level <= 0: return '{...}'
s = ''
n = len(obj)
items = obj.items()[:min(n, self.maxdict)]
items = list(obj.items())[:min(n, self.maxdict)]
items.sort()
for key, val in items:
entry = self.repr1(key, level-1) + ':' + self.repr1(val, level-1)