10000 bpo-18802: Add more details to ipaddress documentation (GH-6083) · python/cpython@5609b78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5609b78

Browse files
csabellazhangyangyu
authored andcommitted
bpo-18802: Add more details to ipaddress documentation (GH-6083)
Original patch by Jon Foster and Berker Peksag.
1 parent 4be79f2 commit 5609b78

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

Doc/library/ipaddress.rst

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ Address objects
9191
The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
9292
attributes. Some attributes that are only meaningful for IPv6 addresses are
9393
also implemented by :class:`IPv4Address` objects, in order to make it easier to
94-
write code that handles both IP versions correctly.
94+
write code that handles both IP versions correctly. Address objects are
95+
:term:`hashable`, so they can be used as keys in dictionaries.
9596

9697
.. class:: IPv4Address(address)
9798

@@ -368,6 +369,8 @@ All attributes implemented by address objects are implemented by network
368369
objects as well. In addition, network objects implement additional attributes.
369370
All of these are common betwe 10000 en :class:`IPv4Network` and :class:`IPv6Network`,
370371
so to avoid duplication they are only documented for :class:`IPv4Network`.
372+
Network objects are :term:`hashable`, so they can be used as keys in
373+
dictionaries.
371374

372375
.. class:: IPv4Network(address, strict=True)
373376

@@ -377,8 +380,9 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
377380
a slash (``/``). The IP address is the network address, and the mask
378381
can be either a single number, which means it's a *prefix*, or a string
379382
representation of an IPv4 address. If it's the latter, the mask is
380-
interpreted as a *net mask* if it starts with a non-zero field, or as
381-
a *host mask* if it starts with a zero field. If no mask is provided,
383+
interpreted as a *net mask* if it starts with a non-zero field, or as a
384+
*host mask* if it starts with a zero field, with the single exception of
385+
an all-zero mask which is treated as a *net mask*. If no mask is provided,
382386
it's considered to be ``/32``.
383387

384388
For example, the following *address* specifications are equivalent:
@@ -408,7 +412,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
408412

409413
Unless stated otherwise, all network methods accepting other network/address
410414
objects will raise :exc:`TypeError` if the argument's IP version is
411-
incompatible to ``self``
415+
incompatible to ``self``.
412416

413417
.. versionchanged:: 3.5
414418

@@ -418,7 +422,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
418422
.. attribute:: max_prefixlen
419423

420424
Refer to the corresponding attribute documentation in
421-
:class:`IPv4Address`
425+
:class:`IPv4Address`.
422426

423427
.. attribute:: is_multicast
424428
.. attribute:: is_private
@@ -428,7 +432,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
428432
.. attribute:: is_link_local
429433

430434
These attributes are true for the network as a whole if they are true
431-
for both the network address and the broadcast address
435+
for both the network address and the broadcast address.
432436

433437
.. attribute:: network_address
434438

@@ -590,10 +594,10 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
590594

591595
Construct an IPv6 network definition. *address* can be one of the following:
592596

593-
1. A string consisting of an IP address and an optional mask, separated by
594-
a slash (``/``). The IP address is the network address, and the mask
595-
is a single number, which represents a *prefix*. If no mask is provided,
596-
it's considered to be ``/128``.
597+
1. A string consisting of an IP address and an optional prefix length,
598+
separated by a slash (``/``). The IP address is the network address,
599+
and the prefix length must be a single number, the *prefix*. If no
600+
prefix length is provided, it's considered to be ``/128``.
597601

598602
Note that currently expanded netmasks are not supported. That means
599603
``2001:db00::0/24`` is a valid argument while ``2001:db00::0/ffff:ff00::``
@@ -652,12 +656,12 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
652656
.. method:: compare_networks(other)
653657

654658
Refer to the corresponding attribute documentation in
655-
:class:`IPv4Network`
659+
:class:`IPv4Network`.
656660

657661
.. attribute:: is_site_local
658662

659663
These attribute is true for the network as a whole if it is true
660-
for both the network address and the broadcast address
664+
for both the network address and the broadcast address.
661665

662666

663667
Operators
@@ -671,8 +675,8 @@ IPv6).
671675
Logical operators
672676
"""""""""""""""""
673677

674-
Network objects can be compared with the usual set of logical operators,
675-
similarly to address objects.
678+
Network objects can be compared with the usual set of logical operators.
679+
Network objects are ordered first by network address, then by net mask.
676680

677681

678682
Iteration
@@ -722,6 +726,9 @@ Network objects can act as containers of addresses. Some examples::
722726
Interface objects
723727
-----------------
724728

729+
Interface objects are :term:`hashable`, so they can be used as keys in
730+
dictionaries.
731+
725732
.. class:: IPv4Interface(address)
726733

727734
Construct an IPv4 interface. The meaning of *address* is as in the
@@ -793,6 +800,30 @@ Interface objects
793800
:class:`IPv4Interface`.
794801

795802

803+
Operators
804+
^^^^^^^^^
805+
806+
Interface objects support some operators. Unless stated otherwise, operators
807+
can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
808+
IPv6).
809+
810+
811+
Logical operators
812+
"""""""""""""""""
813+
814+
Interface objects can be compared with the usual set of logical operators.
815+
816+
For equality comparison (``==`` and ``!=``), both the IP address and network
817+
must be the same for the objects to be equal. An interface will not compare
818+
equal to any address or network object.
819+
820+
For ordering (``<``, ``>``, etc) the rules are different. Interface and
821+
address objects with the same IP version can be compared, and the address
822+
objects will always sort before the interface objects. Two interface objects
823+
are first compared by their networks and, if those are the same, then by their
824+
IP addresses.
825+
826+
796827
Other Module Level Functions
797828
----------------------------
798829

@@ -858,7 +889,7 @@ The module also provides the following module level functions:
858889

859890
doesn't make sense. There are some times however, where you may wish to
860891
have :mod:`ipaddress` sort these anyway. If you need to do this, you can use
861-
this function as the ``key`` argument to :func:`sorted()`.
892+
this function as the *key* argument to :func:`sorted()`.
862893

863894
*obj* is either a network or address object.
864895

@@ -876,4 +907,4 @@ module defines the following exceptions:
876907

877908
.. exception:: NetmaskValueError(ValueError)
878909

879-
Any value error related to the netmask.
910+
Any value error related to the net mask.

Lib/test/test_ipaddress.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ def test_weakref(self):
404404
class NetmaskTestMixin_v4(CommonTestMixin_v4):
405405
"""Input validation on interfaces and networks is very similar"""
406406

407+
def test_no_mask(self):
408+
self.assertEqual(str(self.factory('1.2.3.4')), '1.2.3.4/32')
409+
407410
def test_split_netmask(self):
408411
addr = "1.2.3.4/32/24"
409412
with self.assertAddressError("Only one '/' permitted in %r" % addr):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Documentation changes for ipaddress. Patch by Jon Foster and Berker Peksag.

0 commit comments

Comments
 (0)
0