commit 148c038543196ecdc92716a67c71edfa72aab19a Author: Damian Johnson atagar@torproject.org Date: Sun May 13 13:52:42 2018 -0700
Long digest values caused stem.client errors
Interesting. Tests were happy on my laptop but on my PC where I have an older python 2.7 and 3.x version there are errors...
====================================================================== ERROR: test_relay_cell ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/atagar/Desktop/stem/test/unit/client/cell.py", line 160, in test_relay_cell self.assertEqual(3257622417, RelayCell(5, 'RELAY_BEGIN_DIR', '', 3257622417, 564346860).digest) File "/home/atagar/Desktop/stem/stem/client/cell.py", line 315, in __init__ raise ValueError('RELAY cell digest must be a hash, string, or int but was a %s' % type(digest).__name__) ValueError: RELAY cell digest must be a hash, string, or int but was a long
----------------------------------------------------------------------
Fixing our isinstance checks so they recognize both ints and longs. Two quick things to note about this...
* In python 2.7 this is usually done as "isinstance(my_value, (int, long))" but python3 drops the long type so that's a no-go.
* When we drop the python 2.x series these should become "isinstance(my_value, int)" checks. --- stem/client/cell.py | 3 ++- stem/client/datatype.py | 8 ++++++-- test/unit/client/__init__.py | 4 ++-- test/unit/client/cell.py | 6 +++--- 4 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/stem/client/cell.py b/stem/client/cell.py index a3a0f7f0..7f801fd6 100644 --- a/stem/client/cell.py +++ b/stem/client/cell.py @@ -40,6 +40,7 @@ Messages communicated over a Tor relay's ORPort. import datetime import inspect import io +import numbers import os import random import sys @@ -309,7 +310,7 @@ class RelayCell(CircuitCell): digest = Size.LONG.unpack(digest.digest()[:4]) elif isinstance(digest, (bytes, str_type)): digest = Size.LONG.unpack(digest[:4]) - elif isinstance(digest, int): + elif isinstance(digest, numbers.Integral): pass else: raise ValueError('RELAY cell digest must be a hash, string, or int but was a %s' % type(digest).__name__) diff --git a/stem/client/datatype.py b/stem/client/datatype.py index 687c5081..9abea36b 100644 --- a/stem/client/datatype.py +++ b/stem/client/datatype.py @@ -113,6 +113,7 @@ users.** See our :class:`~stem.client.Relay` the API you probably want. import collections import hashlib import io +import numbers import struct
import stem.prereq @@ -158,7 +159,10 @@ class _IntegerEnum(stem.util.enum.Enum): Privides the (enum, int_value) tuple for a given value. """
- if isinstance(val, int): + # TODO: when we drop python 2.x support all "isinstance(val, + # numbers.Integral)" checks should become "isinstance(val, int)" + + if isinstance(val, numbers.Integral): return self._int_to_enum.get(val, self.UNKNOWN), val elif val in self: return val, self._enum_to_int.get(val, val) @@ -310,7 +314,7 @@ class Size(Field): raise NotImplementedError("Use our constant's unpack() and pop() instead")
def pack(self, content): - if not isinstance(content, int): + if not isinstance(content, numbers.Integral): raise ValueError('Size.pack encodes an integer, but was a %s' % type(content).__name__)
packed = struct.pack(self.format, content) diff --git a/test/unit/client/__init__.py b/test/unit/client/__init__.py index 91d3b2be..18677c62 100644 --- a/test/unit/client/__init__.py +++ b/test/unit/client/__init__.py @@ -2,6 +2,8 @@ Unit tests for stem.client.* contents. """
+import os + __all__ = [ 'address', 'cell', @@ -10,8 +12,6 @@ __all__ = [ 'size', ]
-import os - TEST_DATA = os.path.join(os.path.dirname(__file__), 'data')
diff --git a/test/unit/client/cell.py b/test/unit/client/cell.py index f066cc4f..76fe17ea 100644 --- a/test/unit/client/cell.py +++ b/test/unit/client/cell.py @@ -74,7 +74,7 @@ CERTS_CELLS = { }
AUTH_CHALLENGE_CELLS = { - b'\x00\x00\x82\x00&%s\x00\x02\x00\x01\x00\x03' % CHALLENGE: (CHALLENGE, [1, 3]), + b'\x00\x00\x82\x00&' + CHALLENGE + b'\x00\x02\x00\x01\x00\x03': (CHALLENGE, [1, 3]), }
@@ -237,5 +237,5 @@ class TestCell(unittest.TestCase): self.assertEqual(challenge, cell.challenge) self.assertEqual(methods, cell.methods)
- self.assertRaisesRegexp(ValueError, 'AUTH_CHALLENGE cell should have a payload of 38 bytes, but only had 16', Cell.pop, b'\x00\x00\x82\x00&%s\x00\x02\x00\x01\x00\x03' % CHALLENGE[:10], 2) - self.assertRaisesRegexp(ValueError, 'AUTH_CHALLENGE should have 3 methods, but only had 4 bytes for it', Cell.pop, b'\x00\x00\x82\x00&%s\x00\x03\x00\x01\x00\x03' % CHALLENGE, 2) + self.assertRaisesRegexp(ValueError, 'AUTH_CHALLENGE cell should have a payload of 38 bytes, but only had 16', Cell.pop, b'\x00\x00\x82\x00&' + CHALLENGE[:10] + b'\x00\x02\x00\x01\x00\x03', 2) + self.assertRaisesRegexp(ValueError, 'AUTH_CHALLENGE should have 3 methods, but only had 4 bytes for it', Cell.pop, b'\x00\x00\x82\x00&' + CHALLENGE + b'\x00\x03\x00\x01\x00\x03', 2)