Skip to content

Commit b626d22

Browse files
bpo-42061: Document __format__ for IP addresses (GH-23018)
Automerge-Triggered-By: GH:ericvsmith (cherry picked from commit 3317466) Co-authored-by: Teugea Ioan-Teodor <[email protected]>
1 parent 60324d2 commit b626d22

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Doc/library/ipaddress.rst

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ write code that handles both IP versions correctly. Address objects are
202202
.. _iana-ipv4-special-registry: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
203203
.. _iana-ipv6-special-registry: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
204204

205+
.. method:: IPv4Address.__format__(fmt)
206+
207+
Returns a string representation of the IP address, controlled by
208+
an explicit format string.
209+
*fmt* can be one of the following: ``'s'``, the default option,
210+
equivalent to :func:`str`, ``'b'`` for a zero-padded binary string,
211+
``'X'`` or ``'x'`` for an uppercase or lowercase hexadecimal
212+
representation, or ``'n'``, which is equivalent to ``'b'`` for IPv4
213+
addresses and ``'x'`` for IPv6. For binary and hexadecimal
214+
representations, the form specifier ``'#'`` and the grouping option
215+
``'_'`` are available. ``__format__`` is used by ``format``, ``str.format``
216+
and f-strings.
217+
218+
>>> format(ipaddress.IPv4Address('192.168.0.1'))
219+
'192.168.0.1'
220+
>>> '{:#b}'.format(ipaddress.IPv4Address('192.168.0.1'))
221+
'0b11000000101010000000000000000001'
222+
>>> f'{ipaddress.IPv6Address("2001:db8::1000"):s}'
223+
'2001:db8::1000'
224+
>>> format(ipaddress.IPv6Address('2001:db8::1000'), '_X')
225+
'2001_0DB8_0000_0000_0000_0000_0000_1000'
226+
>>> '{:#_n}'.format(ipaddress.IPv6Address('2001:db8::1000'))
227+
'0x2001_0db8_0000_0000_0000_0000_0000_1000'
228+
229+
.. versionadded:: 3.9
230+
205231

206232
.. class:: IPv6Address(address)
207233

@@ -246,8 +272,8 @@ write code that handles both IP versions correctly. Address objects are
246272
groups consisting entirely of zeroes included.
247273

248274

249-
For the following attributes, see the corresponding documentation of the
250-
:class:`IPv4Address` class:
275+
For the following attributes and methods, see the corresponding
276+
documentation of the :class:`IPv4Address` class:
251277

252278
.. attribute:: packed
253279
.. attribute:: reverse_pointer
@@ -297,6 +323,12 @@ write code that handles both IP versions correctly. Address objects are
297323
the embedded ``(server, client)`` IP address pair. For any other
298324
address, this property will be ``None``.
299325

326+
.. method:: IPv6Address.__format__(fmt)
327+
328+
Refer to the corresponding method documentation in
329+
:class:`IPv4Address`.
330+
331+
.. versionadded:: 3.9
300332

301333
Conversion to Strings and Integers
302334
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Doc/tools/susp-ignored.csv

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ library/ipaddress,,:db8,IPv6Address('2001:db8::')
149149
library/ipaddress,,::,IPv6Address('2001:db8::')
150150
library/ipaddress,,:db8,>>> ipaddress.IPv6Address('2001:db8::1000')
151151
library/ipaddress,,::,>>> ipaddress.IPv6Address('2001:db8::1000')
152-
library/ipaddress,,:db8,IPv6Address('2001:db8::1000')
153-
library/ipaddress,,::,IPv6Address('2001:db8::1000')
152+
library/ipaddress,,:db8,'2001:db8::1000'
153+
library/ipaddress,,::,'2001:db8::1000'
154+
library/ipaddress,231,:db8,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
155+
library/ipaddress,231,::,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
154156
library/ipaddress,,::,IPv6Address('ff02::5678%1')
155157
library/ipaddress,,::,fe80::1234
156158
library/ipaddress,,:db8,">>> ipaddress.ip_address(""2001:db8::1"").reverse_pointer"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document __format__ functionality for IP addresses.

0 commit comments

Comments
 (0)