cpython: cacf3b1e20da (original) (raw)

Mercurial > cpython

changeset 77479:cacf3b1e20da

Issue #14814: Add first draft of PEP 3144 ipaddress module documentation (initial patch by Sandro Tosi) [#14814]

Nick Coghlan ncoghlan@gmail.com
date Sun, 17 Jun 2012 17:24:10 +1000
parents 739f5c725958
children afe67ea94bc6
files Doc/howto/ipaddress.rst Doc/library/internet.rst Doc/library/ipaddress.rst Misc/NEWS
diffstat 4 files changed, 278 insertions(+), 13 deletions(-)[+] [-] Doc/howto/ipaddress.rst 34 Doc/library/internet.rst 1 Doc/library/ipaddress.rst 251 Misc/NEWS 5

line wrap: on

line diff

--- a/Doc/howto/ipaddress.rst +++ b/Doc/howto/ipaddress.rst @@ -1,14 +1,19 @@ .. _ipaddress-howto: - -Ipaddress Howto - + +An Introduction to the ipaddress module + :author: Peter Moody +:author: Nick Coghlan -.. topic:: Abstract +.. topic:: Overview

Creating Address/Network/Interface objects @@ -40,7 +45,7 @@ IP Host Addresses Addresses, often referred to as "host addresses" are the most basic unit when working with IP addressing. The simplest way to create addresses is -to use the ip_address factory function, which automatically determines +to use the :func:ipaddress.ip_address factory function, which automatically determines whether to create an IPv4 or IPv6 address based on the passed in value:: >>> ipaddress.ip_address('192.0.2.1') @@ -113,7 +118,7 @@ integer, so the network prefix includes >>> ipaddress.ip_network(3221225984) IPv4Network('192.0.2.0/32')

Creation of a particular kind of network can be forced by calling the @@ -275,15 +280,18 @@ an integer or string that the other modu Exceptions raised by :mod:ipaddress ===================================== -If you try to create an address/network/interface object with an invalid value -for either the address or netmask, :mod:ipaddress will raise an -:exc:AddressValueError or :exc:NetmaskValueError respectively. However, -this applies only when calling the class constructors directly. The factory -functions and other module level functions will just raise :exc:ValueError. +When creating address/network/interface objects using the version-agnostic +factory functions, any errors will be reported as :exc:ValueError. + +For some use cases, it desirable to know whether it is the address or the +netmask which is incorrect. To support these use cases, the class +constructors actually raise the :exc:ValueError subclasses +:exc:ipaddress.AddressValueError and :exc:ipaddress.NetmaskValueError +to indicate exactly which part of the definition failed to parse correctly. Both of the module specific exceptions have :exc:ValueError as their parent class, so if you're not concerned with the particular type of error, -you can still do the following:: +you can still write code like the following:: try: ipaddress.IPv4Address(address)

--- a/Doc/library/internet.rst +++ b/Doc/library/internet.rst @@ -42,3 +42,4 @@ is currently supported on most popular p http.cookiejar.rst xmlrpc.client.rst xmlrpc.server.rst

new file mode 100644 --- /dev/null +++ b/Doc/library/ipaddress.rst @@ -0,0 +1,251 @@ +:mod:ipaddress --- IPv4/IPv6 manipulation library +=================================================== + +.. module:: ipaddress

+Source code: :source:Lib/ipaddress.py + +-------------- + +The :mod:ipaddress module provides the capabilities to create, manipulate and +operate on IPv4 and IPv6 addresses and networks. + +This is the full module API reference - for an overview and introduction, +see :ref:ipaddress-howto. + +The functions and classes in this module make it straightforward to handle +various tasks related to IP addresses, including checking whether or not two +hosts are on the same subnet, iterating over all hosts in a particular +subnet, as well as checking whether or not a string represents a valid +IP address or network definition. + + +Defining IP Addresses and Interfaces +------------------------------------ + +The :mod:ipaddress module provides factory functions to define IP addresses +and networks: + +.. function:: ip_address(address) +

+ +.. function:: ip_network(address, strict=True) +

+ +.. function:: ip_interface(address) +

+ +Representing IP Addresses and Networks +-------------------------------------- + +The module defines the following and classes to represent IP addresses +and networks: + +.. todo: list the properties and methods + +.. class:: IPv4Address(address) +

+ +.. class:: IPv4Interface(address) +

+ +.. class:: IPv4Network(address, strict=True) +

+ +.. class:: IPv6Address(address) +

+ +.. class:: IPv6Interface(address) +

+ +.. class:: IPv6Network(address, strict=True) +

+ +Other Module Level Functions +---------------------------- + +The module also provides the following module level functions: + +.. function:: v4_int_to_packed(address) +

+ +.. function:: v6_int_to_packed(address) +

+ +.. function:: summarize_address_range(first, last) +

+ +.. function:: collapse_addresses(addresses) +

+ +.. function:: get_mixed_type_key(obj) +

+

+ +Custom Exceptions +----------------- + +To support more specific error reporting from class constructors, the +module defines the following exceptions: + +.. exception:: AddressValueError(ValueError) +

+ +.. exception:: NetmaskValueError(ValueError) +

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -124,6 +124,11 @@ Extension Modules