You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some time ago I submitted a proposal to add length hints to the itertools module. These did not get merged for several valid reasons: #101790
However, since ipaddress is a pure Python module, I think adding length (hints) is less hard to get right here.
Therefore I would like to propose adding __len__ (or __length_hint__) to IPv4Network and IPv6Network. This can be useful for progress reporting (for example when scanning a range of IP addresses).
Update: Adding this works fine for IPv4Networks, but a large IPv6Network will result in issues with integer sizes:
Traceback (most recent call last):
File "cpython/Lib/ipaddress.py", line 2350, in <module>
print(length_hint(IPv6Network('2607:fb10:2018::/45')))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: Python int too large to convert to C ssize_t
Traceback (most recent call last):
File "cpython/Lib/ipaddress.py", line 2350, in <module>
print(len(IPv6Network('2607:fb10:2018::/45')))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: cannot fit 'int' into an index-sized integer
Feature or enhancement
Proposal:
Some time ago I submitted a proposal to add length hints to the
itertoolsmodule. These did not get merged for several valid reasons: #101790However, since
ipaddressis a pure Python module, I think adding length (hints) is less hard to get right here.Therefore I would like to propose adding
__len__(or__length_hint__) toIPv4NetworkandIPv6Network. This can be useful for progress reporting (for example when scanning a range of IP addresses).Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: