[netaddr] r340 committed - Added patch from S. Nordhausen and implemented suggestions.

1 view
Skip to first unread message

codesite...@google.com

unread,
Dec 2, 2009, 9:14:39 AM12/2/09
to net...@googlegroups.com
Revision: 340
Author: drkjam
Date: Wed Dec 2 06:13:23 2009
Log: Added patch from S. Nordhausen and implemented suggestions.

http://code.google.com/p/netaddr/source/detail?r=340

Modified:
/branches/rel-0.7.x/CHANGELOG
/branches/rel-0.7.x/netaddr/__init__.py
/branches/rel-0.7.x/netaddr/eui/__init__.py
/branches/rel-0.7.x/netaddr/ip/__init__.py
/branches/rel-0.7.x/netaddr/strategy/eui48.py
/branches/rel-0.7.x/netaddr/strategy/ipv4.py
/branches/rel-0.7.x/netaddr/tests/eui/eui64.txt
/branches/rel-0.7.x/netaddr/tests/eui/tutorial.txt
/branches/rel-0.7.x/netaddr/tests/ip/tutorial.txt
/branches/rel-0.7.x/netaddr/tests/strategy/ipv4.txt

=======================================
--- /branches/rel-0.7.x/CHANGELOG Mon Sep 14 15:12:58 2009
+++ /branches/rel-0.7.x/CHANGELOG Wed Dec 2 06:13:23 2009
@@ -1,3 +1,16 @@
+Release: 0.7.4
+Date: 2nd Dec 2009
+
+Changes since 0.7.3
+-------------------
+
+* Applied speed patches by S. Nordhausen
+
+* Fixed an inconsistency between EUI and IPAddress interfaces. Made
+ EUI.packed and EUI.bin properties (previously methods) and added a
+ words() property.
+
+---------------------------------------------------------------------
Release: 0.7.3
Date: 14th Sep 2009

=======================================
--- /branches/rel-0.7.x/netaddr/__init__.py Mon Sep 14 15:12:58 2009
+++ /branches/rel-0.7.x/netaddr/__init__.py Wed Dec 2 06:13:23 2009
@@ -10,7 +10,7 @@
if _sys.version_info[0:2] < (2, 4):
raise RuntimeError('Python 2.4.x or higher is required!')

-__version__ = '0.7.3'
+__version__ = '0.7.4'

from netaddr.core import AddrConversionError, AddrFormatError, \
NotRegisteredError
=======================================
--- /branches/rel-0.7.x/netaddr/eui/__init__.py Wed Aug 5 14:27:15 2009
+++ /branches/rel-0.7.x/netaddr/eui/__init__.py Wed Dec 2 06:13:23 2009
@@ -291,6 +291,16 @@
self._value = None
self._module = None

+ if isinstance(addr, EUI):
+ # Copy constructor.
+ if version is not None and version != addr._module.version:
+ raise ValueError('cannot switch EUI versions using '
+ 'copy constructor!')
+ self._module = addr._module
+ self._value = addr._value
+ self.dialect = addr.dialect
+ return
+
if version is not None:
if version == 48:
self._module = _eui48
@@ -298,10 +308,9 @@
self._module = _eui64
else:
raise ValueError('unsupported EUI version %r' % version)
-
+ else:
# Choose a default version when addr is an integer and version is
# not specified.
- if self._module is None:
if 0 <= addr <= 0xffffffffffff:
self._module = _eui48
elif 0xffffffffffff < addr <= 0xffffffffffffffff:
@@ -530,10 +539,19 @@
@return: human-readable binary digit string of this address"""
return self._module.int_to_bits(self._value, word_sep)

+ @property
def packed(self):
"""@return: binary packed string of this address"""
return self._module.int_to_packed(self._value)

+ @property
+ def words(self):
+ """
+ A list of unsigned integer octets found in this EUI address.
+ """
+ return self._module.int_to_words(self._value)
+
+ @property
def bin(self):
"""
@return: standard Python binary representation of this address. A
back
=======================================
--- /branches/rel-0.7.x/netaddr/ip/__init__.py Mon Sep 14 15:12:58 2009
+++ /branches/rel-0.7.x/netaddr/ip/__init__.py Wed Dec 2 06:13:23 2009
@@ -241,7 +241,7 @@
"""
super(IPAddress, self).__init__()

- if hasattr(addr, '_value'):
+ if isinstance(addr, BaseIP):
# Copy constructor.
if version is not None and version != addr._module.version:
raise ValueError('cannot switch IP versions using '
@@ -265,7 +265,8 @@
return self._value

def _set_value(self, value):
- if hasattr(value, 'upper') and '/' in value:
+ has_upper = hasattr(value, 'upper')
+ if has_upper and '/' in value:
raise ValueError('%s() does not support netmasks or subnet' \
' prefixes! See documentation for details.'
% self.__class__.__name__)
@@ -291,7 +292,7 @@
% value)
else:
# IP version is explicit.
- if hasattr(value, 'upper'):
+ if has_upper:
try:
self._value = self._module.str_to_int(value)
except AddrFormatError:
=======================================
--- /branches/rel-0.7.x/netaddr/strategy/eui48.py Wed Aug 5 14:27:15 2009
+++ /branches/rel-0.7.x/netaddr/strategy/eui48.py Wed Dec 2 06:13:23 2009
@@ -218,8 +218,7 @@
@return: a packed string that is equivalent to value represented by an
unsigned integer.
"""
- words = int_to_words(int_val)
- return _struct.pack('>6B', *words)
+ return _struct.pack(">HI", int_val >> 32, int_val & 0xffffffff)


#-----------------------------------------------------------------------------
def packed_to_int(packed_int):
=======================================
--- /branches/rel-0.7.x/netaddr/strategy/ipv4.py Wed Aug 5 14:27:15 2009
+++ /branches/rel-0.7.x/netaddr/strategy/ipv4.py Wed Dec 2 06:13:23 2009
@@ -141,8 +141,7 @@
@return: a packed string that is equivalent to value represented by an
unsigned integer.
"""
- words = int_to_words(int_val)
- return _struct.pack('>4B', *words)
+ return _struct.pack('>I', int_val)


#-----------------------------------------------------------------------------
def packed_to_int(packed_int):
@@ -153,8 +152,7 @@
@return: An unsigned integer equivalent to value of network address
represented by packed binary string.
"""
- words = list(_struct.unpack('>4B', packed_int))
- return _struct.unpack('>I', _struct.pack('4B', *words))[0]
+ return _struct.unpack('>I', packed_int)[0]


#-----------------------------------------------------------------------------
def valid_words(words):
@@ -171,7 +169,10 @@
if not 0 <= int_val <= max_int:
raise ValueError('%r is not a valid integer value supported ' \
'by this address type!' % int_val)
- return _struct.unpack('4B', _struct.pack('>I', int_val))
+ return ( (int_val >> 24),
+ (int_val >> 16 & 255),
+ (int_val >> 8 & 255),
+ (int_val & 255) )


#-----------------------------------------------------------------------------
def words_to_int(words):
=======================================
--- /branches/rel-0.7.x/netaddr/tests/eui/eui64.txt Wed Aug 26 13:15:46 2009
+++ /branches/rel-0.7.x/netaddr/tests/eui/eui64.txt Wed Dec 2 06:13:23 2009
@@ -23,10 +23,10 @@
>>> int(eui) == 7731765737772285
True

->>> eui.packed()
+>>> eui.packed
'\x00\x1bw\xff\xfeIT\xfd'

->>> eui.bin()
+>>> eui.bin
'0b11011011101111111111111111110010010010101010011111101'

>>> eui.bits()
=======================================
--- /branches/rel-0.7.x/netaddr/tests/eui/tutorial.txt Wed Aug 26 13:15:46
2009
+++ /branches/rel-0.7.x/netaddr/tests/eui/tutorial.txt Wed Dec 2 06:13:23
2009
@@ -63,7 +63,7 @@
>>> mac.bits()
'00000000-00011011-01110111-01001001-01010100-11111101'

->>> mac.bin()
+>>> mac.bin
'0b1101101110111010010010101010011111101'

}}}
=======================================
--- /branches/rel-0.7.x/netaddr/tests/ip/tutorial.txt Wed Aug 26 13:15:46
2009
+++ /branches/rel-0.7.x/netaddr/tests/ip/tutorial.txt Wed Dec 2 06:13:23
2009
@@ -78,8 +78,8 @@
>>> ip.bits()
'11000000.00000000.00000010.00000001'

->>> ip.words
-(192, 0, 2, 1)
+>>> ip.words == (192, 0, 2, 1)
+True

}}}

=======================================
--- /branches/rel-0.7.x/netaddr/tests/strategy/ipv4.txt Thu Jun 25 16:12:17
2009
+++ /branches/rel-0.7.x/netaddr/tests/strategy/ipv4.txt Wed Dec 2 06:13:23
2009
@@ -30,8 +30,8 @@
>>> int_to_str(i)
'192.0.2.1'

->>> int_to_words(i)
-(192, 0, 2, 1)
+>>> int_to_words(i) == (192, 0, 2, 1)
+True

>>> int_to_packed(i)
'\xc0\x00\x02\x01'
Reply all
Reply to author
Forward
0 new messages