-
Notifications
You must be signed in to change notification settings - Fork 117
Implementation of bitwise XOR function for bytes object #72
Conversation
We're doing quite a bit of "try Python 2, and if it explodes, assume Python 3" in the code now. To be honest, I'm not too fond of it, especially in a function like this. What would you say about something like this in import sys
PY3 = sys.version_info < (3, 0) Then in the code (mostly in if PY3:
range = range
integer_types = (int,)
unicode_type = str
def byte_literal(s):
return s.encode('latin1')
else:
range = xrange
integer_types = (int, long)
unicode_type = unicode
def byte_literal(s):
return s I think this is more explicit and easier to read. It also performs better in your |
By the way, now that we don't support Python 2.6 any more, we can drop the |
I think that new approach is a big design improvement! Even we could have boolean values for Python subversions in cases where a better implementation exists for newer subversions (i.e. However, I would like to keep the useful current just-one function implementation, to keep a separated and atomized logic, and also to not repeat docstrings. if PY3:
(...)
else:
(...)
def xor_bytes(b1, b2, is_python3=PY3):
"""Docstring"""
if is_python3:
return ...
else:
return ... Another alternative to hide def xor_bytes(b1, b2):
return common_xor_bytes(b1, b2)
def common_xor_bytes(b1, b2, is_python3=PY3):
"""Docstring"""
if is_python3:
return ...
else:
return ... What do you think about it? Of course, I'll miss having a variable definition for py2/py3 on consecutive lines to compare them, but I can live with it. |
Rebased, and removed all |
def test_xor_bytes(self): | ||
b1 = b'\xff\xff\xff\xff' | ||
b2 = b'\x00\x00\x00\x00' | ||
b3 = b'\xf0\xf0\xf0\xf0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test with some random (but still hard-coded) byte strings. It's too easy to miss subtle errors when you only test with all-on or all-off values.
I think the first |
Great! I will create a new PR to refactor the compat file based on |
Please see my note on #82 (comment) |
@sybrenstuvel rebased with latest |
Great work, thanks! |
This function is needed for future OAEP implementation, that makes use of bitwise XOR between bytes objects. Related to #68.