summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNaveen Nathan <naveen@lastninja.net>2016-05-16 11:09:49 +1000
committerNaveen Nathan <naveen@lastninja.net>2016-05-16 11:09:49 +1000
commit5cc08aaab229a42b4b20b69fe264839aaa449e07 (patch)
tree31f7b1fd67b6bcb5d7b806b3d3ff14ba444fa526
parent3cd26ea6dc041618bd1d8cb5055741a79119725f (diff)
downloadnetaddr-5cc08aaab229a42b4b20b69fe264839aaa449e07.tar.gz
fix IPAddress().netmask_bits to return 0 for 0.0.0.0 and [::] addresses
-rw-r--r--netaddr/ip/__init__.py5
-rw-r--r--netaddr/tests/ip/test_ip_v4.py1
-rw-r--r--netaddr/tests/ip/test_ip_v6.py2
3 files changed, 7 insertions, 1 deletions
diff --git a/netaddr/ip/__init__.py b/netaddr/ip/__init__.py
index 0141b26..0024677 100644
--- a/netaddr/ip/__init__.py
+++ b/netaddr/ip/__init__.py
@@ -348,6 +348,11 @@ class IPAddress(BaseIP):
if not self.is_netmask():
return self._module.width
+ # the '0' address (e.g. 0.0.0.0 or 0000::) is a valid netmask with
+ # no bits set.
+ if self._value == 0:
+ return 0
+
i_val = self._value
numbits = 0
diff --git a/netaddr/tests/ip/test_ip_v4.py b/netaddr/tests/ip/test_ip_v4.py
index 85fcf6a..fe1ae38 100644
--- a/netaddr/tests/ip/test_ip_v4.py
+++ b/netaddr/tests/ip/test_ip_v4.py
@@ -403,6 +403,7 @@ def test_ipaddress_netmask_v4():
assert IPAddress('1.1.1.1').netmask_bits() == 32
assert IPAddress('255.255.255.254').netmask_bits() == 31
assert IPAddress('255.255.255.0').netmask_bits() == 24
+ assert IPAddress('0.0.0.0').netmask_bits() == 0
def test_ipaddress_hex_format():
diff --git a/netaddr/tests/ip/test_ip_v6.py b/netaddr/tests/ip/test_ip_v6.py
index fb19f19..e8fb2ad 100644
--- a/netaddr/tests/ip/test_ip_v6.py
+++ b/netaddr/tests/ip/test_ip_v6.py
@@ -84,7 +84,7 @@ def test_ipnetwork_constructor_v6():
def test_ipaddress_netmask_v6():
- assert IPAddress('::').netmask_bits() == 128
+ assert IPAddress('::').netmask_bits() == 0
def test_objects_use_slots():