-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python3 port #3
Python3 port #3
Changes from 1 commit
3113043
e33cab4
b8c1ba4
7b7ec95
ee880c8
0227a38
db42162
d125bc8
6713c0f
7f9fc49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,7 +199,7 @@ def to_str (self, separator = ':', resolve_names = False): | |
# Don't even bother for local (though it should never match and OUI!) | ||
name = _eth_oui_to_name.get(self._value[:3]) | ||
if name: | ||
rest = separator.join('%02x' % (ord(x),) for x in self._value[3:]) | ||
rest = separator.join('%02x' % (ord(x),) for x in self._value[3:]) #TODO ERROR | ||
return name + separator + rest | ||
|
||
return separator.join(('%02x' % (ord(x),) for x in self._value)) | ||
|
@@ -387,14 +387,41 @@ def multicast_ethernet_address (self): | |
def __str__ (self): | ||
return self.toStr() | ||
|
||
def __cmp__ (self, other): | ||
if other is None: return 1 | ||
def __eq__(self, other): | ||
try: | ||
if not isinstance(other, IPAddr): | ||
other = IPAddr(other) | ||
return cmp(self.toUnsigned(), other.toUnsigned()) | ||
return self.toUnsigned() == other.toUnsigned() | ||
except: | ||
return -other.__cmp__(self) | ||
return other.__eq__(self) | ||
|
||
def __ne__(self, other): | ||
try: | ||
if not isinstance(other, IPAddr): | ||
other = IPAddr(other) | ||
return self.toUnsigned() != other.toUnsigned() | ||
except: | ||
return other.__ne__(self) | ||
|
||
def __lt__(self, other): | ||
try: | ||
if not isinstance(other, IPAddr): | ||
other = IPAddr(other) | ||
return self.toUnsigned() < other.toUnsigned() | ||
except: | ||
# reversed order | ||
return other.__gt__(self) | ||
|
||
def __gt__(self, other): | ||
try: | ||
if not isinstance(other, IPAddr): | ||
other = IPAddr(other) | ||
return self.toUnsigned() > other.toUnsigned() | ||
except: | ||
# reversed order | ||
return other.__lt__(self) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the functools.total_ordering decorator here instead of a bunch of manual implementations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess so, but I believe this is a special case which I kept from the original code: we try to cast |
||
|
||
def __hash__ (self): | ||
return self._value.__hash__() | ||
|
@@ -436,7 +463,7 @@ def from_num (cls, num): | |
""" | ||
o = b'' | ||
for i in range(16): | ||
o = chr(num & 0xff) + o | ||
o = chr(num & 0xff).encode('latin-1') + o | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should probably just avoid chr(). I don't think there's a direct equivalent for bytes objects, but we could make a chrb or something and put it in util or something. Maybe along the lines of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should work, but the notion of |
||
num >>= 8 | ||
return cls.from_raw(o) | ||
|
||
|
@@ -555,7 +582,7 @@ def to_ipv4 (self, check_ipv4 = True): | |
def num (self): | ||
o = 0 | ||
for b in self._value: | ||
o = (o << 8) | ord(b) | ||
o = (o << 8) | b | ||
return o | ||
|
||
@property | ||
|
@@ -690,7 +717,7 @@ def to_str (self, zero_drop = True, section_drop = True, ipv4 = None): | |
by passing ipv4=True; this probably only makes sense if .is_ipv4_compatible | ||
(or .is_ipv4_mapped, of course). | ||
""" | ||
o = [ord(lo) | (ord(hi)<<8) for hi,lo in | ||
o = [lo | (hi<<8) for hi,lo in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised this works. I wonder if we should do something like I suggested for chr() and have an ordb() or whatever like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The value is always bytes now, so string conversion with |
||
(self._value[i:i+2] for i in range(0,16,2))] | ||
|
||
if (ipv4 is None and self.is_ipv4_mapped) or ipv4: | ||
|
@@ -737,14 +764,39 @@ def fmt (n): | |
def __str__ (self): | ||
return self.to_str() | ||
|
||
def __cmp__ (self, other): | ||
if other is None: return 1 | ||
def __eq__(self, other): | ||
try: | ||
if not isinstance(other, type(self)): | ||
other = type(self)(other) | ||
return self._value == other._value | ||
except: | ||
return other.__eq__(self) | ||
|
||
def __ne__(self, other): | ||
try: | ||
if not isinstance(other, type(self)): | ||
other = type(self)(other) | ||
return not self._value == other._value | ||
except: | ||
return other.__ne__(self) | ||
|
||
def __lt__(self, other): | ||
try: | ||
if not isinstance(other, type(self)): | ||
other = type(self)(other) | ||
return self._value < other._value | ||
except: | ||
# reversed order | ||
return other.__gt__(self) | ||
|
||
def __gt__(self, other): | ||
try: | ||
if not isinstance(other, type(self)): | ||
other = type(self)(other) | ||
return cmp(self._value, other._value) | ||
return self._value > other._value | ||
except: | ||
return -cmp(other,self) | ||
# reversed order | ||
return other.__lt__(self) | ||
|
||
def __hash__ (self): | ||
return self._value.__hash__() | ||
|
@@ -764,7 +816,7 @@ def set_mac (self, eth): | |
e = list(EthAddr(eth).toTuple()) | ||
e[0] ^= 2 | ||
e[3:3] = [0xff,0xfe] | ||
e = ''.join(chr(b) for b in e) | ||
e = b''.join(chr(b).encode('latin-1') for b in e) | ||
return IPAddr6.from_raw(self._value[:8]+e) | ||
|
||
|
||
|
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.
Offhand, this seems like it should be fine to me. I think the problem is probably that there are cases where _value doesn't end up as a bytes object and is a string instead, which it shouldn't be. This is probably because some of the code paths in the constructor were sloppy. (On that note, there are constructor code paths that can be improved since raw inputs and hex inputs are now more easily distinguished via their type.)
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.
I updated all constructors to store values as bytes, in fact this is probably an outdated commit...
ord
is not needed anymore