h0arry / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

sorting lists of address and network objects produces varying results #28

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Running a sort across a list of randomly shuffled IPvXAddress and
IPvXNetwork objects should always yield a consistent result.

Currently the ordering of elements with the same base (network) address but
different CIDR prefix shifts for every run of the example code.

What steps will reproduce the problem?

from ipaddr import *
import random

subnet = IPNetwork('192.0.2.0/28')
networks = [subnet]
networks.extend(list(subnet))
networks.extend(subnet.subnet(1))
networks.extend(subnet.subnet(2))
networks.extend(subnet.subnet(3))

random.shuffle(networks)

for subnet in sorted(networks):
    print subnet

What is the expected output?

192.0.2.0/28
192.0.2.0/29
192.0.2.0/30
192.0.2.0/31
192.0.2.0
192.0.2.1
192.0.2.2/31
192.0.2.2
192.0.2.3
192.0.2.4/30
192.0.2.4/31
192.0.2.4
192.0.2.5
192.0.2.6/31
192.0.2.6
192.0.2.7
192.0.2.8/29
192.0.2.8/30
192.0.2.8/31
192.0.2.8
192.0.2.9
192.0.2.10/31
192.0.2.10
192.0.2.11
192.0.2.12/30
192.0.2.12/31
192.0.2.12
192.0.2.13
192.0.2.14/31
192.0.2.14
192.0.2.15

What do you see instead?

192.0.2.0/28
192.0.2.0
192.0.2.0/29
192.0.2.0/30
192.0.2.0/31
192.0.2.1
192.0.2.2
192.0.2.2/31
192.0.2.3
192.0.2.4/30
192.0.2.4/31
192.0.2.4
192.0.2.5
192.0.2.6/31
192.0.2.6
192.0.2.7
192.0.2.8/29
192.0.2.8/30
192.0.2.8/31
192.0.2.8
192.0.2.9
192.0.2.10/31
192.0.2.10
192.0.2.11
192.0.2.12/30
192.0.2.12/31
192.0.2.12
192.0.2.13
192.0.2.14
192.0.2.14/31
192.0.2.15

What version of the product are you using? On what operating system?

ipaddr 2.0.x r97

Please provide any additional information below.

Original issue reported on code.google.com by drk...@gmail.com on 27 Aug 2009 at 9:13

GoogleCodeExporter commented 9 years ago
interesting, I'll check this out.

Original comment by pmo...@google.com on 27 Aug 2009 at 9:26

GoogleCodeExporter commented 9 years ago
Attempting to reproduce this in r176 I see the following:

subnet = IPNetwork('192.0.2.0/30')
networks = [subnet]
networks.extend(list(subnet))

Note at this point we now have mixed types:
print networks

=> [IPv4Network('192.0.2.0/30'), IPv4Address('192.0.2.0'), 
IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'), IPv4Address('192.0.2.3')]

# With a mix of Addresses and Networks the comparison fails with traceback - 
which is correct behavior.

networks[0] > networks[1]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/harro/Source/ipaddr-py/trunk/ipaddr.py", line 556, in __gt__
    str(self), str(other)))
TypeError: 192.0.2.0/30 and 192.0.2.0 are not of the same type

To confirm that the sort is actually OK with same type. Lets cast the IP 
addresses into /32 Networks and compare after randomising:

realnets = []
for x in networks:
  realnets.append(IPNetwork(x))

random.shuffle(realnets)
for subnet in sorted(realnets):
  print subnet

=>
192.0.2.0/28
192.0.2.0/29
192.0.2.0/30
192.0.2.0/31
192.0.2.0/32
192.0.2.1/32
192.0.2.2/31
192.0.2.2/32
192.0.2.3/32
192.0.2.4/30
192.0.2.4/31
192.0.2.4/32
192.0.2.5/32
192.0.2.6/31
192.0.2.6/32
192.0.2.7/32
192.0.2.8/29
192.0.2.8/30
192.0.2.8/31
192.0.2.8/32
192.0.2.9/32
192.0.2.10/31
192.0.2.10/32
192.0.2.11/32
192.0.2.12/30
192.0.2.12/31
192.0.2.12/32
192.0.2.13/32
192.0.2.14/31
192.0.2.14/32
192.0.2.15/32

So looks like this issue can be closed.

Original comment by ha...@google.com on 20 Jul 2010 at 3:30

GoogleCodeExporter commented 9 years ago

Original comment by ha...@google.com on 20 Jul 2010 at 3:32