Generating and printing a range of ip addresses

Terry Reedy tjreedy at udel.edu
Sat Feb 14 17:15:41 CET 2004


"Stephen Briley" <sdb1031 at yahoo.com> wrote in message
news:20040214073612.19320.qmail at web60910.mail.yahoo.com...

Major problem is bug in *your* code, minor problem is buglet in
IPv4.__cmp__.

> >>> from ipv4 import *
> >>> ip = IPv4('10.0.0.1')
> >>> startadd = ip.nexthost()
> >>> endadd = ('10.0.3.0')

You forgot IPv4 call, so endadd is str, not IPv4 instance!  endadd =
IPv4('10.0.3.0')

> >>> print startadd
> 10.0.0.2
> >>> print endadd
> 10.0.3.0
>
> >>> while startadd != endadd:
> ... startadd = ip.nexthost()
> ... print startadd
> ...
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "C:\Python23\lib\ipv4.py", line 250, in __cmp__
>     return cmp(self._address, other._address)
> AttributeError: 'str' object has no attribute '_address'

IPv4 should arguably catch mismatch and give better error message.  Suggest
to IPv4 author that 'he' replace above with something like

    if isinstance(other, IPv4):
        return cmp(self._address, other._address)
    else:
        raise TypeError('Can only compare IPv4 instance to another
another')

He could use try/except/raise, but this would allow probably nonsensical
comparisons with non-IPv4 objects with _address attribute.

Terry J. Reedy








More information about the Python-list mailing list