phihag / ipaddress

Python 3.3+'s ipaddress for older Python versions
Other
105 stars 53 forks source link

network in network testing not working #18

Closed bewing closed 9 years ago

bewing commented 9 years ago

Unable to test if a subnet/network is wholly contained within another -- all comparisons return false:

>>> from ipaddress import *
>>> net1 = ip_network(u"192.0.2.0/24")
>>> net2 = ip_network(u"192.0.2.112/29")
>>> net1 in net2
False
>>> net2 in net1
False
>>>
phihag commented 9 years ago

Thank you for the report! I can reproduce the problem; when I adapted the code from Python 3, __contains__ wasn't yet implemented for networks in networks. I'm looking into resyncing and getting all of the new features.

bewing commented 9 years ago

Actually, looking at the latest 3.5 beta, it looks like not even the upstream does this correctly. from the class _BaseNetwork:

    def __contains__(self, other):
        # always false if one is v4 and the other is v6.
        if self._version != other._version:
            return False
        # dealing with another network.
        if isinstance(other, _BaseNetwork):
            return False
bewing commented 9 years ago

Looks like upstream is also tracking this issue: http://bugs.python.org/issue20825

phihag commented 9 years ago

See the upstream discussion: in will just check for elements, not subsets.

In ipaddress 1.0.9, I've already applied the upstream patch, you can now use subnet_of / supernet_of.