[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Python 2.6 struct.pack() different in edge cases



commit 7a3c620e096e27023340245d40cee98309505637
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Oct 7 14:30:11 2018 -0700

    Python 2.6 struct.pack() different in edge cases
    
    Python 2.6 structs behave weirdly? Gasp!
    
    Kidding aside, this one's pretty straight forward.
    
      ======================================================================
      ERROR: test_pack
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/client/size.py", line 27, in test_pack
          self.assertRaisesWith(ValueError, 'Size.pack encodes an integer, but was a str', Size.CHAR.pack, 'hi')
        File "/home/atagar/Desktop/stem/stem/util/test_tools.py", line 288, in assertRaisesWith
          return self.assertRaisesRegexp(exc_type, '^%s$' % re.escape(exc_msg), func, *args, **kwargs)
        File "/home/atagar/Desktop/stem/stem/util/test_tools.py", line 293, in assertRaisesRegexp
          func(*args, **kwargs)
        File "/home/atagar/Desktop/stem/stem/client/datatype.py", line 362, in pack
          packed = struct.pack(self.format, content)
      TypeError: unsupported operand type(s) for &: 'str' and 'long'
    
      ----------------------------------------------------------------------
      Ran 4 tests in 0.006s
---
 stem/client/datatype.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/stem/client/datatype.py b/stem/client/datatype.py
index 644d4993..de67fcdc 100644
--- a/stem/client/datatype.py
+++ b/stem/client/datatype.py
@@ -358,6 +358,29 @@ class Size(Field):
     raise NotImplementedError("Use our constant's unpack() and pop() instead")
 
   def pack(self, content):
+    # TODO: Python 2.6's struct module behaves a little differently in a couple
+    # respsects...
+    #
+    #   * Invalid types raise a TypeError rather than a struct.error.
+    #
+    #   * Negative values are happily packed despite being unsigned fields with
+    #     a message printed to stdout (!) that says...
+    #
+    #       stem/client/datatype.py:362: DeprecationWarning: struct integer overflow masking is deprecated
+    #         packed = struct.pack(self.format, content)
+    #       stem/client/datatype.py:362: DeprecationWarning: 'B' format requires 0 <= number <= 255
+    #         packed = struct.pack(self.format, content)
+    #
+    # Rather than adjust this method to account for these differences doing
+    # duplicate upfront checks just for python 2.6. When we drop 2.6 support
+    # this can obviously be dropped.
+
+    if stem.prereq._is_python_26():
+      if not stem.util._is_int(content):
+        raise ValueError('Size.pack encodes an integer, but was a %s' % type(content).__name__)
+      elif content < 0:
+        raise ValueError('Packed values must be positive (attempted to pack %i as a %s)' % (content, self.name))
+
     try:
       packed = struct.pack(self.format, content)
     except struct.error:



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits