phihag / ipaddress

Python 3.3+'s ipaddress for older Python versions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

network in network testing not working

bewing opened this issue · comments

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
>>>

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.

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

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

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.