Mercurial > cpython-lru_cache-weakref
annotate Lib/ipaddress.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 81ad910d75a9 |
| children | 07488c3c85f1 |
| rev | line source |
|---|---|
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1 # Copyright 2007 Google Inc. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2 # Licensed to PSF under a Contributor Agreement. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
3 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
4 """A fast, lightweight IPv4/IPv6 manipulation library in Python. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
5 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
7 and networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
8 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
9 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
10 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
11 __version__ = '1.0' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
12 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
13 |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
14 import functools |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
15 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
16 IPV4LENGTH = 32 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
17 IPV6LENGTH = 128 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
18 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
19 class AddressValueError(ValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
20 """A Value Error related to the address.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
21 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
22 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
23 class NetmaskValueError(ValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
24 """A Value Error related to the netmask.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
25 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
26 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
27 def ip_address(address): |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
28 """Take an IP string/int and return an object of the correct type. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
29 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
30 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
31 address: A string or integer, the IP address. Either IPv4 or |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
32 IPv6 addresses may be supplied; integers less than 2**32 will |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
33 be considered to be IPv4 by default. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
34 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
35 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
36 An IPv4Address or IPv6Address object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
37 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
38 Raises: |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
39 ValueError: if the *address* passed isn't either a v4 or a v6 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
40 address |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
41 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
42 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
43 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
44 return IPv4Address(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
45 except (AddressValueError, NetmaskValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
46 pass |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
47 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
48 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
49 return IPv6Address(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
50 except (AddressValueError, NetmaskValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
51 pass |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
52 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
53 raise ValueError('%r does not appear to be an IPv4 or IPv6 address' % |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
54 address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
55 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
56 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
57 def ip_network(address, strict=True): |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
58 """Take an IP string/int and return an object of the correct type. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
59 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
60 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
61 address: A string or integer, the IP network. Either IPv4 or |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
62 IPv6 networks may be supplied; integers less than 2**32 will |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
63 be considered to be IPv4 by default. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
64 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
65 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
66 An IPv4Network or IPv6Network object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
67 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
68 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
69 ValueError: if the string passed isn't either a v4 or a v6 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
70 address. Or if the network has host bits set. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
71 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
72 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
73 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
74 return IPv4Network(address, strict) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
75 except (AddressValueError, NetmaskValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
76 pass |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
77 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
78 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
79 return IPv6Network(address, strict) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
80 except (AddressValueError, NetmaskValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
81 pass |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
82 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
83 raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
84 address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
85 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
86 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
87 def ip_interface(address): |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
88 """Take an IP string/int and return an object of the correct type. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
89 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
90 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
91 address: A string or integer, the IP address. Either IPv4 or |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
92 IPv6 addresses may be supplied; integers less than 2**32 will |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
93 be considered to be IPv4 by default. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
94 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
95 Returns: |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
96 An IPv4Interface or IPv6Interface object. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
97 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
98 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
99 ValueError: if the string passed isn't either a v4 or a v6 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
100 address. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
101 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
102 Notes: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
103 The IPv?Interface classes describe an Address on a particular |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
104 Network, so they're basically a combination of both the Address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
105 and Network classes. |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
106 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
107 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
108 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
109 return IPv4Interface(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
110 except (AddressValueError, NetmaskValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
111 pass |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
112 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
113 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
114 return IPv6Interface(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
115 except (AddressValueError, NetmaskValueError): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
116 pass |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
117 |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
118 raise ValueError('%r does not appear to be an IPv4 or IPv6 interface' % |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
119 address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
120 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
121 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
122 def v4_int_to_packed(address): |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
123 """Represent an address as 4 packed bytes in network (big-endian) order. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
124 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
125 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
126 address: An integer representation of an IPv4 IP address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
127 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
128 Returns: |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
129 The integer address packed as 4 bytes in network (big-endian) order. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
130 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
131 Raises: |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
132 ValueError: If the integer is negative or too large to be an |
|
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
133 IPv4 IP address. |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
134 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
135 """ |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
136 try: |
|
78662
811d91591f73
Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
Nick Coghlan <ncoghlan@gmail.com>
parents:
78661
diff
changeset
|
137 return address.to_bytes(4, 'big') |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
138 except: |
|
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
139 raise ValueError("Address negative or too large for IPv4") |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
140 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
141 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
142 def v6_int_to_packed(address): |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
143 """Represent an address as 16 packed bytes in network (big-endian) order. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
144 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
145 Args: |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
146 address: An integer representation of an IPv6 IP address. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
147 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
148 Returns: |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
149 The integer address packed as 16 bytes in network (big-endian) order. |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
150 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
151 """ |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
152 try: |
|
78662
811d91591f73
Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
Nick Coghlan <ncoghlan@gmail.com>
parents:
78661
diff
changeset
|
153 return address.to_bytes(16, 'big') |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
154 except: |
|
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
155 raise ValueError("Address negative or too large for IPv6") |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
156 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
157 |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
158 def _split_optional_netmask(address): |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
159 """Helper to split the netmask and raise AddressValueError if needed""" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
160 addr = str(address).split('/') |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
161 if len(addr) > 2: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
162 raise AddressValueError("Only one '/' permitted in %r" % address) |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
163 return addr |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
164 |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
165 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
166 def _find_address_range(addresses): |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
167 """Find a sequence of IPv#Address. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
168 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
169 Args: |
|
77116
0be296605165
Issue #14814: improve docstrings and arguments value handling, as per Terry J. Reedy's comments
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77084
diff
changeset
|
170 addresses: a list of IPv#Address objects. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
171 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
172 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
173 A tuple containing the first and last IP addresses in the sequence. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
174 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
175 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
176 first = last = addresses[0] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
177 for ip in addresses[1:]: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
178 if ip._ip == last._ip + 1: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
179 last = ip |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
180 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
181 break |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
182 return (first, last) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
183 |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
184 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
185 def _count_righthand_zero_bits(number, bits): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
186 """Count the number of zero bits on the right hand side. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
187 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
188 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
189 number: an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
190 bits: maximum number of bits to count. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
191 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
192 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
193 The number of zero bits on the right hand side of the number. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
194 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
195 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
196 if number == 0: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
197 return bits |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
198 for i in range(bits): |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
199 if (number >> i) & 1: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
200 return i |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
201 # All bits of interest were zero, even if there are more in the number |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
202 return bits |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
203 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
204 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
205 def summarize_address_range(first, last): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
206 """Summarize a network range given the first and last IP addresses. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
207 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
208 Example: |
|
79567
81ad910d75a9
Issue #15888: fixing problems in ipaddress doctests. Patch by Chris Jerdonek
Eli Bendersky <eliben@gmail.com>
parents:
78662
diff
changeset
|
209 >>> list(summarize_address_range(IPv4Address('192.0.2.0'), |
|
81ad910d75a9
Issue #15888: fixing problems in ipaddress doctests. Patch by Chris Jerdonek
Eli Bendersky <eliben@gmail.com>
parents:
78662
diff
changeset
|
210 ... IPv4Address('192.0.2.130'))) |
|
81ad910d75a9
Issue #15888: fixing problems in ipaddress doctests. Patch by Chris Jerdonek
Eli Bendersky <eliben@gmail.com>
parents:
78662
diff
changeset
|
211 ... #doctest: +NORMALIZE_WHITESPACE |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
212 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), |
|
79567
81ad910d75a9
Issue #15888: fixing problems in ipaddress doctests. Patch by Chris Jerdonek
Eli Bendersky <eliben@gmail.com>
parents:
78662
diff
changeset
|
213 IPv4Network('192.0.2.130/32')] |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
214 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
215 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
216 first: the first IPv4Address or IPv6Address in the range. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
217 last: the last IPv4Address or IPv6Address in the range. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
218 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
219 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
220 An iterator of the summarized IPv(4|6) network objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
221 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
222 Raise: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
223 TypeError: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
224 If the first and last objects are not IP addresses. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
225 If the first and last objects are not the same version. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
226 ValueError: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
227 If the last object is not greater than the first. |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
228 If the version of the first address is not 4 or 6. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
229 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
230 """ |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
231 if (not (isinstance(first, _BaseAddress) and |
|
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
232 isinstance(last, _BaseAddress))): |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
233 raise TypeError('first and last must be IP addresses, not networks') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
234 if first.version != last.version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
235 raise TypeError("%s and %s are not of the same version" % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
236 first, last)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
237 if first > last: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
238 raise ValueError('last IP address must be greater than first') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
239 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
240 if first.version == 4: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
241 ip = IPv4Network |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
242 elif first.version == 6: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
243 ip = IPv6Network |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
244 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
245 raise ValueError('unknown IP version') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
246 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
247 ip_bits = first._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
248 first_int = first._ip |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
249 last_int = last._ip |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
250 while first_int <= last_int: |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
251 nbits = min(_count_righthand_zero_bits(first_int, ip_bits), |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
252 (last_int - first_int + 1).bit_length() - 1) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
253 net = ip('%s/%d' % (first, ip_bits - nbits)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
254 yield net |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
255 first_int += 1 << nbits |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
256 if first_int - 1 == ip._ALL_ONES: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
257 break |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
258 first = first.__class__(first_int) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
259 |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
260 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
261 def _collapse_addresses_recursive(addresses): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
262 """Loops through the addresses, collapsing concurrent netblocks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
263 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
264 Example: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
265 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
266 ip1 = IPv4Network('192.0.2.0/26') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
267 ip2 = IPv4Network('192.0.2.64/26') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
268 ip3 = IPv4Network('192.0.2.128/26') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
269 ip4 = IPv4Network('192.0.2.192/26') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
270 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
271 _collapse_addresses_recursive([ip1, ip2, ip3, ip4]) -> |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
272 [IPv4Network('192.0.2.0/24')] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
273 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
274 This shouldn't be called directly; it is called via |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
275 collapse_addresses([]). |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
276 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
277 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
278 addresses: A list of IPv4Network's or IPv6Network's |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
279 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
280 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
281 A list of IPv4Network's or IPv6Network's depending on what we were |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
282 passed. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
283 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
284 """ |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
285 while True: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
286 last_addr = None |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
287 ret_array = [] |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
288 optimized = False |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
289 |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
290 for cur_addr in addresses: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
291 if not ret_array: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
292 last_addr = cur_addr |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
293 ret_array.append(cur_addr) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
294 elif (cur_addr.network_address >= last_addr.network_address and |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
295 cur_addr.broadcast_address <= last_addr.broadcast_address): |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
296 optimized = True |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
297 elif cur_addr == list(last_addr.supernet().subnets())[1]: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
298 ret_array[-1] = last_addr = last_addr.supernet() |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
299 optimized = True |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
300 else: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
301 last_addr = cur_addr |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
302 ret_array.append(cur_addr) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
303 |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
304 addresses = ret_array |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
305 if not optimized: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
306 return addresses |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
307 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
308 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
309 def collapse_addresses(addresses): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
310 """Collapse a list of IP objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
311 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
312 Example: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
313 collapse_addresses([IPv4Network('192.0.2.0/25'), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
314 IPv4Network('192.0.2.128/25')]) -> |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
315 [IPv4Network('192.0.2.0/24')] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
316 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
317 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
318 addresses: An iterator of IPv4Network or IPv6Network objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
319 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
320 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
321 An iterator of the collapsed IPv(4|6)Network objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
322 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
323 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
324 TypeError: If passed a list of mixed version objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
325 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
326 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
327 i = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
328 addrs = [] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
329 ips = [] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
330 nets = [] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
331 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
332 # split IP addresses and networks |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
333 for ip in addresses: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
334 if isinstance(ip, _BaseAddress): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
335 if ips and ips[-1]._version != ip._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
336 raise TypeError("%s and %s are not of the same version" % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
337 ip, ips[-1])) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
338 ips.append(ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
339 elif ip._prefixlen == ip._max_prefixlen: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
340 if ips and ips[-1]._version != ip._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
341 raise TypeError("%s and %s are not of the same version" % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
342 ip, ips[-1])) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
343 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
344 ips.append(ip.ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
345 except AttributeError: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
346 ips.append(ip.network_address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
347 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
348 if nets and nets[-1]._version != ip._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
349 raise TypeError("%s and %s are not of the same version" % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
350 ip, nets[-1])) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
351 nets.append(ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
352 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
353 # sort and dedup |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
354 ips = sorted(set(ips)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
355 nets = sorted(set(nets)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
356 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
357 while i < len(ips): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
358 (first, last) = _find_address_range(ips[i:]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
359 i = ips.index(last) + 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
360 addrs.extend(summarize_address_range(first, last)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
361 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
362 return iter(_collapse_addresses_recursive(sorted( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
363 addrs + nets, key=_BaseNetwork._get_networks_key))) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
364 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
365 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
366 def get_mixed_type_key(obj): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
367 """Return a key suitable for sorting between networks and addresses. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
368 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
369 Address and Network objects are not sortable by default; they're |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
370 fundamentally different so the expression |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
371 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
372 IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
373 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
374 doesn't make any sense. There are some times however, where you may wish |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
375 to have ipaddress sort these for you anyway. If you need to do this, you |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
376 can use this function as the key= argument to sorted(). |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
377 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
378 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
379 obj: either a Network or Address object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
380 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
381 appropriate key. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
382 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
383 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
384 if isinstance(obj, _BaseNetwork): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
385 return obj._get_networks_key() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
386 elif isinstance(obj, _BaseAddress): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
387 return obj._get_address_key() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
388 return NotImplemented |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
389 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
390 |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
391 class _TotalOrderingMixin: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
392 # Helper that derives the other comparison operations from |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
393 # __lt__ and __eq__ |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
394 # We avoid functools.total_ordering because it doesn't handle |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
395 # NotImplemented correctly yet (http://bugs.python.org/issue10042) |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
396 def __eq__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
397 raise NotImplementedError |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
398 def __ne__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
399 equal = self.__eq__(other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
400 if equal is NotImplemented: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
401 return NotImplemented |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
402 return not equal |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
403 def __lt__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
404 raise NotImplementedError |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
405 def __le__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
406 less = self.__lt__(other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
407 if less is NotImplemented or not less: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
408 return self.__eq__(other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
409 return less |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
410 def __gt__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
411 less = self.__lt__(other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
412 if less is NotImplemented: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
413 return NotImplemented |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
414 equal = self.__eq__(other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
415 if equal is NotImplemented: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
416 return NotImplemented |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
417 return not (less or equal) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
418 def __ge__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
419 less = self.__lt__(other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
420 if less is NotImplemented: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
421 return NotImplemented |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
422 return not less |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
423 |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
424 class _IPAddressBase(_TotalOrderingMixin): |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
425 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
426 """The mother class.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
427 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
428 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
429 def exploded(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
430 """Return the longhand version of the IP address as a string.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
431 return self._explode_shorthand_ip_string() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
432 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
433 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
434 def compressed(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
435 """Return the shorthand version of the IP address as a string.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
436 return str(self) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
437 |
|
77478
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
438 @property |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
439 def version(self): |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
440 msg = '%200s has no version specified' % (type(self),) |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
441 raise NotImplementedError(msg) |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
442 |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
443 def _check_int_address(self, address): |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
444 if address < 0: |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
445 msg = "%d (< 0) is not permitted as an IPv%d address" |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
446 raise AddressValueError(msg % (address, self._version)) |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
447 if address > self._ALL_ONES: |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
448 msg = "%d (>= 2**%d) is not permitted as an IPv%d address" |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
449 raise AddressValueError(msg % (address, self._max_prefixlen, |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
450 self._version)) |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
451 |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
452 def _check_packed_address(self, address, expected_len): |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
453 address_len = len(address) |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
454 if address_len != expected_len: |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
455 msg = "%r (len %d != %d) is not permitted as an IPv%d address" |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
456 raise AddressValueError(msg % (address, address_len, |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
457 expected_len, self._version)) |
|
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
458 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
459 def _ip_int_from_prefix(self, prefixlen=None): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
460 """Turn the prefix length netmask into a int for comparison. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
461 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
462 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
463 prefixlen: An integer, the prefix length. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
464 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
465 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
466 An integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
467 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
468 """ |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
469 if prefixlen is None: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
470 prefixlen = self._prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
471 return self._ALL_ONES ^ (self._ALL_ONES >> prefixlen) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
472 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
473 def _prefix_from_ip_int(self, ip_int, mask=32): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
474 """Return prefix length from the decimal netmask. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
475 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
476 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
477 ip_int: An integer, the IP address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
478 mask: The netmask. Defaults to 32. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
479 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
480 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
481 An integer, the prefix length. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
482 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
483 """ |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
484 return mask - _count_righthand_zero_bits(ip_int, mask) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
485 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
486 def _ip_string_from_prefix(self, prefixlen=None): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
487 """Turn a prefix length into a dotted decimal string. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
488 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
489 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
490 prefixlen: An integer, the netmask prefix length. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
491 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
492 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
493 A string, the dotted decimal netmask string. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
494 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
495 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
496 if not prefixlen: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
497 prefixlen = self._prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
498 return self._string_from_ip_int(self._ip_int_from_prefix(prefixlen)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
499 |
|
78428
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
500 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
501 class _BaseAddress(_IPAddressBase): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
502 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
503 """A generic IP object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
504 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
505 This IP class contains the version independent methods which are |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
506 used by single IP addresses. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
507 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
508 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
509 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
510 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
511 if (not isinstance(address, bytes) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
512 and '/' in str(address)): |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
513 raise AddressValueError("Unexpected '/' in %r" % address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
514 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
515 def __int__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
516 return self._ip |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
517 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
518 def __eq__(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
519 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
520 return (self._ip == other._ip |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
521 and self._version == other._version) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
522 except AttributeError: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
523 return NotImplemented |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
524 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
525 def __lt__(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
526 if self._version != other._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
527 raise TypeError('%s and %s are not of the same version' % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
528 self, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
529 if not isinstance(other, _BaseAddress): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
530 raise TypeError('%s and %s are not of the same type' % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
531 self, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
532 if self._ip != other._ip: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
533 return self._ip < other._ip |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
534 return False |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
535 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
536 # Shorthand for Integer addition and subtraction. This is not |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
537 # meant to ever support addition/subtraction of addresses. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
538 def __add__(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
539 if not isinstance(other, int): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
540 return NotImplemented |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
541 return self.__class__(int(self) + other) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
542 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
543 def __sub__(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
544 if not isinstance(other, int): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
545 return NotImplemented |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
546 return self.__class__(int(self) - other) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
547 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
548 def __repr__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
549 return '%s(%r)' % (self.__class__.__name__, str(self)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
550 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
551 def __str__(self): |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
552 return str(self._string_from_ip_int(self._ip)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
553 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
554 def __hash__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
555 return hash(hex(int(self._ip))) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
556 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
557 def _get_address_key(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
558 return (self._version, self) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
559 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
560 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
561 class _BaseNetwork(_IPAddressBase): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
562 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
563 """A generic IP network object. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
564 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
565 This IP class contains the version independent methods which are |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
566 used by networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
567 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
568 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
569 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
570 self._cache = {} |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
571 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
572 def __repr__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
573 return '%s(%r)' % (self.__class__.__name__, str(self)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
574 |
|
77345
df6d1a4d83fa
#14814: Remove dead code from ipaddress
Hynek Schlawack <hs@ox.cx>
parents:
77338
diff
changeset
|
575 def __str__(self): |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
576 return '%s/%d' % (self.network_address, self.prefixlen) |
|
77345
df6d1a4d83fa
#14814: Remove dead code from ipaddress
Hynek Schlawack <hs@ox.cx>
parents:
77338
diff
changeset
|
577 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
578 def hosts(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
579 """Generate Iterator over usable hosts in a network. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
580 |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
581 This is like __iter__ except it doesn't return the network |
|
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
582 or broadcast addresses. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
583 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
584 """ |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
585 network = int(self.network_address) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
586 broadcast = int(self.broadcast_address) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
587 for x in range(network + 1, broadcast): |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
588 yield self._address_class(x) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
589 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
590 def __iter__(self): |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
591 network = int(self.network_address) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
592 broadcast = int(self.broadcast_address) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
593 for x in range(network, broadcast + 1): |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
594 yield self._address_class(x) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
595 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
596 def __getitem__(self, n): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
597 network = int(self.network_address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
598 broadcast = int(self.broadcast_address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
599 if n >= 0: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
600 if network + n > broadcast: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
601 raise IndexError |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
602 return self._address_class(network + n) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
603 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
604 n += 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
605 if broadcast + n < network: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
606 raise IndexError |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
607 return self._address_class(broadcast + n) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
608 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
609 def __lt__(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
610 if self._version != other._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
611 raise TypeError('%s and %s are not of the same version' % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
612 self, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
613 if not isinstance(other, _BaseNetwork): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
614 raise TypeError('%s and %s are not of the same type' % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
615 self, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
616 if self.network_address != other.network_address: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
617 return self.network_address < other.network_address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
618 if self.netmask != other.netmask: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
619 return self.netmask < other.netmask |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
620 return False |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
621 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
622 def __eq__(self, other): |
|
77970
2e9cba1d1554
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
Nick Coghlan <ncoghlan@gmail.com>
parents:
77967
diff
changeset
|
623 try: |
|
2e9cba1d1554
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
Nick Coghlan <ncoghlan@gmail.com>
parents:
77967
diff
changeset
|
624 return (self._version == other._version and |
|
2e9cba1d1554
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
Nick Coghlan <ncoghlan@gmail.com>
parents:
77967
diff
changeset
|
625 self.network_address == other.network_address and |
|
2e9cba1d1554
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
Nick Coghlan <ncoghlan@gmail.com>
parents:
77967
diff
changeset
|
626 int(self.netmask) == int(other.netmask)) |
|
2e9cba1d1554
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
Nick Coghlan <ncoghlan@gmail.com>
parents:
77967
diff
changeset
|
627 except AttributeError: |
|
2e9cba1d1554
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
Nick Coghlan <ncoghlan@gmail.com>
parents:
77967
diff
changeset
|
628 return NotImplemented |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
629 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
630 def __hash__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
631 return hash(int(self.network_address) ^ int(self.netmask)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
632 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
633 def __contains__(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
634 # always false if one is v4 and the other is v6. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
635 if self._version != other._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
636 return False |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
637 # dealing with another network. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
638 if isinstance(other, _BaseNetwork): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
639 return False |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
640 # dealing with another address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
641 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
642 # address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
643 return (int(self.network_address) <= int(other._ip) <= |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
644 int(self.broadcast_address)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
645 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
646 def overlaps(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
647 """Tell if self is partly contained in other.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
648 return self.network_address in other or ( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
649 self.broadcast_address in other or ( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
650 other.network_address in self or ( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
651 other.broadcast_address in self))) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
652 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
653 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
654 def broadcast_address(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
655 x = self._cache.get('broadcast_address') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
656 if x is None: |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
657 x = self._address_class(int(self.network_address) | |
|
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
658 int(self.hostmask)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
659 self._cache['broadcast_address'] = x |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
660 return x |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
661 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
662 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
663 def hostmask(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
664 x = self._cache.get('hostmask') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
665 if x is None: |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
666 x = self._address_class(int(self.netmask) ^ self._ALL_ONES) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
667 self._cache['hostmask'] = x |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
668 return x |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
669 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
670 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
671 def with_prefixlen(self): |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
672 return '%s/%d' % (self.network_address, self._prefixlen) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
673 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
674 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
675 def with_netmask(self): |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
676 return '%s/%s' % (self.network_address, self.netmask) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
677 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
678 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
679 def with_hostmask(self): |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
680 return '%s/%s' % (self.network_address, self.hostmask) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
681 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
682 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
683 def num_addresses(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
684 """Number of hosts in the current subnet.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
685 return int(self.broadcast_address) - int(self.network_address) + 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
686 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
687 @property |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
688 def _address_class(self): |
|
77478
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
689 # Returning bare address objects (rather than interfaces) allows for |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
690 # more consistent behaviour across the network address, broadcast |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
691 # address and individual host addresses. |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
692 msg = '%200s has no associated address class' % (type(self),) |
|
739f5c725958
Improve an internal ipaddress test, add a comment explaining why treating networks as containers of interfaces rather than addresses would prove confusing
Nick Coghlan <ncoghlan@gmail.com>
parents:
77383
diff
changeset
|
693 raise NotImplementedError(msg) |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
694 |
|
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
695 @property |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
696 def prefixlen(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
697 return self._prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
698 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
699 def address_exclude(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
700 """Remove an address from a larger block. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
701 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
702 For example: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
703 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
704 addr1 = ip_network('192.0.2.0/28') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
705 addr2 = ip_network('192.0.2.1/32') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
706 addr1.address_exclude(addr2) = |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
707 [IPv4Network('192.0.2.0/32'), IPv4Network('192.0.2.2/31'), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
708 IPv4Network('192.0.2.4/30'), IPv4Network('192.0.2.8/29')] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
709 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
710 or IPv6: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
711 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
712 addr1 = ip_network('2001:db8::1/32') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
713 addr2 = ip_network('2001:db8::1/128') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
714 addr1.address_exclude(addr2) = |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
715 [ip_network('2001:db8::1/128'), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
716 ip_network('2001:db8::2/127'), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
717 ip_network('2001:db8::4/126'), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
718 ip_network('2001:db8::8/125'), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
719 ... |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
720 ip_network('2001:db8:8000::/33')] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
721 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
722 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
723 other: An IPv4Network or IPv6Network object of the same type. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
724 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
725 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
726 An iterator of the the IPv(4|6)Network objects which is self |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
727 minus other. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
728 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
729 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
730 TypeError: If self and other are of difffering address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
731 versions, or if other is not a network object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
732 ValueError: If other is not completely contained by self. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
733 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
734 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
735 if not self._version == other._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
736 raise TypeError("%s and %s are not of the same version" % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
737 self, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
738 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
739 if not isinstance(other, _BaseNetwork): |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
740 raise TypeError("%s is not a network object" % other) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
741 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
742 if not (other.network_address >= self.network_address and |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
743 other.broadcast_address <= self.broadcast_address): |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
744 raise ValueError('%s not contained in %s' % (other, self)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
745 if other == self: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
746 raise StopIteration |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
747 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
748 # Make sure we're comparing the network of other. |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
749 other = other.__class__('%s/%s' % (other.network_address, |
|
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
750 other.prefixlen)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
751 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
752 s1, s2 = self.subnets() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
753 while s1 != other and s2 != other: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
754 if (other.network_address >= s1.network_address and |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
755 other.broadcast_address <= s1.broadcast_address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
756 yield s2 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
757 s1, s2 = s1.subnets() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
758 elif (other.network_address >= s2.network_address and |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
759 other.broadcast_address <= s2.broadcast_address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
760 yield s1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
761 s1, s2 = s2.subnets() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
762 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
763 # If we got here, there's a bug somewhere. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
764 raise AssertionError('Error performing exclusion: ' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
765 's1: %s s2: %s other: %s' % |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
766 (s1, s2, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
767 if s1 == other: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
768 yield s2 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
769 elif s2 == other: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
770 yield s1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
771 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
772 # If we got here, there's a bug somewhere. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
773 raise AssertionError('Error performing exclusion: ' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
774 's1: %s s2: %s other: %s' % |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
775 (s1, s2, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
776 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
777 def compare_networks(self, other): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
778 """Compare two IP objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
779 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
780 This is only concerned about the comparison of the integer |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
781 representation of the network addresses. This means that the |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
782 host bits aren't considered at all in this method. If you want |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
783 to compare host bits, you can easily enough do a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
784 'HostA._ip < HostB._ip' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
785 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
786 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
787 other: An IP object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
788 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
789 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
790 If the IP versions of self and other are the same, returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
791 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
792 -1 if self < other: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
793 eg: IPv4Network('192.0.2.0/25') < IPv4Network('192.0.2.128/25') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
794 IPv6Network('2001:db8::1000/124') < |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
795 IPv6Network('2001:db8::2000/124') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
796 0 if self == other |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
797 eg: IPv4Network('192.0.2.0/24') == IPv4Network('192.0.2.0/24') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
798 IPv6Network('2001:db8::1000/124') == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
799 IPv6Network('2001:db8::1000/124') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
800 1 if self > other |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
801 eg: IPv4Network('192.0.2.128/25') > IPv4Network('192.0.2.0/25') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
802 IPv6Network('2001:db8::2000/124') > |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
803 IPv6Network('2001:db8::1000/124') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
804 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
805 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
806 TypeError if the IP versions are different. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
807 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
808 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
809 # does this need to raise a ValueError? |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
810 if self._version != other._version: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
811 raise TypeError('%s and %s are not of the same type' % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
812 self, other)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
813 # self._version == other._version below here: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
814 if self.network_address < other.network_address: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
815 return -1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
816 if self.network_address > other.network_address: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
817 return 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
818 # self.network_address == other.network_address below here: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
819 if self.netmask < other.netmask: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
820 return -1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
821 if self.netmask > other.netmask: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
822 return 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
823 return 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
824 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
825 def _get_networks_key(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
826 """Network-only key function. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
827 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
828 Returns an object that identifies this address' network and |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
829 netmask. This function is a suitable "key" argument for sorted() |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
830 and list.sort(). |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
831 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
832 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
833 return (self._version, self.network_address, self.netmask) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
834 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
835 def subnets(self, prefixlen_diff=1, new_prefix=None): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
836 """The subnets which join to make the current subnet. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
837 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
838 In the case that self contains only one IP |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
839 (self._prefixlen == 32 for IPv4 or self._prefixlen == 128 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
840 for IPv6), yield an iterator with just ourself. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
841 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
842 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
843 prefixlen_diff: An integer, the amount the prefix length |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
844 should be increased by. This should not be set if |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
845 new_prefix is also set. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
846 new_prefix: The desired new prefix length. This must be a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
847 larger number (smaller prefix) than the existing prefix. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
848 This should not be set if prefixlen_diff is also set. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
849 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
850 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
851 An iterator of IPv(4|6) objects. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
852 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
853 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
854 ValueError: The prefixlen_diff is too small or too large. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
855 OR |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
856 prefixlen_diff and new_prefix are both set or new_prefix |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
857 is a smaller number than the current prefix (smaller |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
858 number means a larger network) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
859 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
860 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
861 if self._prefixlen == self._max_prefixlen: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
862 yield self |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
863 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
864 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
865 if new_prefix is not None: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
866 if new_prefix < self._prefixlen: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
867 raise ValueError('new prefix must be longer') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
868 if prefixlen_diff != 1: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
869 raise ValueError('cannot set prefixlen_diff and new_prefix') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
870 prefixlen_diff = new_prefix - self._prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
871 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
872 if prefixlen_diff < 0: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
873 raise ValueError('prefix length diff must be > 0') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
874 new_prefixlen = self._prefixlen + prefixlen_diff |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
875 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
876 if not self._is_valid_netmask(str(new_prefixlen)): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
877 raise ValueError( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
878 'prefix length diff %d is invalid for netblock %s' % ( |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
879 new_prefixlen, self)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
880 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
881 first = self.__class__('%s/%s' % |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
882 (self.network_address, |
|
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
883 self._prefixlen + prefixlen_diff)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
884 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
885 yield first |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
886 current = first |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
887 while True: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
888 broadcast = current.broadcast_address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
889 if broadcast == self.broadcast_address: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
890 return |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
891 new_addr = self._address_class(int(broadcast) + 1) |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
892 current = self.__class__('%s/%s' % (new_addr, |
|
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
893 new_prefixlen)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
894 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
895 yield current |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
896 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
897 def supernet(self, prefixlen_diff=1, new_prefix=None): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
898 """The supernet containing the current network. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
899 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
900 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
901 prefixlen_diff: An integer, the amount the prefix length of |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
902 the network should be decreased by. For example, given a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
903 /24 network and a prefixlen_diff of 3, a supernet with a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
904 /21 netmask is returned. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
905 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
906 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
907 An IPv4 network object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
908 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
909 Raises: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
910 ValueError: If self.prefixlen - prefixlen_diff < 0. I.e., you have |
|
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
911 a negative prefix length. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
912 OR |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
913 If prefixlen_diff and new_prefix are both set or new_prefix is a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
914 larger number than the current prefix (larger number means a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
915 smaller network) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
916 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
917 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
918 if self._prefixlen == 0: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
919 return self |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
920 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
921 if new_prefix is not None: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
922 if new_prefix > self._prefixlen: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
923 raise ValueError('new prefix must be shorter') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
924 if prefixlen_diff != 1: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
925 raise ValueError('cannot set prefixlen_diff and new_prefix') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
926 prefixlen_diff = self._prefixlen - new_prefix |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
927 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
928 if self.prefixlen - prefixlen_diff < 0: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
929 raise ValueError( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
930 'current prefixlen is %d, cannot have a prefixlen_diff of %d' % |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
931 (self.prefixlen, prefixlen_diff)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
932 # TODO (pmoody): optimize this. |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
933 t = self.__class__('%s/%d' % (self.network_address, |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
934 self.prefixlen - prefixlen_diff), |
|
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
935 strict=False) |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
936 return t.__class__('%s/%d' % (t.network_address, t.prefixlen)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
937 |
|
78428
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
938 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
939 def is_multicast(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
940 """Test if the address is reserved for multicast use. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
941 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
942 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
943 A boolean, True if the address is a multicast address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
944 See RFC 2373 2.7 for details. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
945 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
946 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
947 return (self.network_address.is_multicast and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
948 self.broadcast_address.is_multicast) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
949 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
950 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
951 def is_reserved(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
952 """Test if the address is otherwise IETF reserved. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
953 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
954 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
955 A boolean, True if the address is within one of the |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
956 reserved IPv6 Network ranges. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
957 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
958 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
959 return (self.network_address.is_reserved and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
960 self.broadcast_address.is_reserved) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
961 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
962 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
963 def is_link_local(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
964 """Test if the address is reserved for link-local. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
965 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
966 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
967 A boolean, True if the address is reserved per RFC 4291. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
968 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
969 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
970 return (self.network_address.is_link_local and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
971 self.broadcast_address.is_link_local) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
972 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
973 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
974 def is_private(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
975 """Test if this address is allocated for private networks. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
976 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
977 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
978 A boolean, True if the address is reserved per RFC 4193. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
979 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
980 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
981 return (self.network_address.is_private and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
982 self.broadcast_address.is_private) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
983 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
984 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
985 def is_unspecified(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
986 """Test if the address is unspecified. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
987 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
988 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
989 A boolean, True if this is the unspecified address as defined in |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
990 RFC 2373 2.5.2. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
991 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
992 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
993 return (self.network_address.is_unspecified and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
994 self.broadcast_address.is_unspecified) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
995 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
996 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
997 def is_loopback(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
998 """Test if the address is a loopback address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
999 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1000 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1001 A boolean, True if the address is a loopback address as defined in |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1002 RFC 2373 2.5.3. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1003 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1004 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1005 return (self.network_address.is_loopback and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1006 self.broadcast_address.is_loopback) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1007 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1008 |
|
77281
0eb63de72e96
#14814: Remove 2.x's new-style classes syntax from ipaddress
Hynek Schlawack <hs@ox.cx>
parents:
77274
diff
changeset
|
1009 class _BaseV4: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1010 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1011 """Base IPv4 object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1012 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1013 The following methods are used by IPv4 objects in both single IP |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1014 addresses and networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1015 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1016 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1017 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1018 # Equivalent to 255.255.255.255 or 32 bits of 1's. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1019 _ALL_ONES = (2**IPV4LENGTH) - 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1020 _DECIMAL_DIGITS = frozenset('0123456789') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1021 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1022 # the valid octets for host and netmasks. only useful for IPv4. |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1023 _valid_mask_octets = frozenset((255, 254, 252, 248, 240, 224, 192, 128, 0)) |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1024 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1025 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1026 self._version = 4 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1027 self._max_prefixlen = IPV4LENGTH |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1028 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1029 def _explode_shorthand_ip_string(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1030 return str(self) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1031 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1032 def _ip_int_from_string(self, ip_str): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1033 """Turn the given IP string into an integer for comparison. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1034 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1035 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1036 ip_str: A string, the IP ip_str. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1037 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1038 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1039 The IP ip_str as an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1040 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1041 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1042 AddressValueError: if ip_str isn't a valid IPv4 Address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1043 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1044 """ |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1045 if not ip_str: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1046 raise AddressValueError('Address cannot be empty') |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1047 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1048 octets = ip_str.split('.') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1049 if len(octets) != 4: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1050 raise AddressValueError("Expected 4 octets in %r" % ip_str) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1051 |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1052 try: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1053 return int.from_bytes(map(self._parse_octet, octets), 'big') |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1054 except ValueError as exc: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1055 raise AddressValueError("%s in %r" % (exc, ip_str)) from None |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1056 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1057 def _parse_octet(self, octet_str): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1058 """Convert a decimal octet into an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1059 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1060 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1061 octet_str: A string, the number to parse. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1062 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1063 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1064 The octet as an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1065 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1066 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1067 ValueError: if the octet isn't strictly a decimal from [0..255]. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1068 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1069 """ |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1070 if not octet_str: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1071 raise ValueError("Empty octet not permitted") |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1072 # Whitelist the characters, since int() allows a lot of bizarre stuff. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1073 if not self._DECIMAL_DIGITS.issuperset(octet_str): |
|
78004
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1074 msg = "Only decimal digits permitted in %r" |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1075 raise ValueError(msg % octet_str) |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1076 # We do the length check second, since the invalid character error |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1077 # is likely to be more informative for the user |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1078 if len(octet_str) > 3: |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1079 msg = "At most 3 characters permitted in %r" |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1080 raise ValueError(msg % octet_str) |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1081 # Convert to integer (we know digits are legal) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1082 octet_int = int(octet_str, 10) |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1083 # Any octets that look like they *might* be written in octal, |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1084 # and which don't look exactly the same in both octal and |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1085 # decimal are rejected as ambiguous |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1086 if octet_int > 7 and octet_str[0] == '0': |
|
78004
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1087 msg = "Ambiguous (octal/decimal) value in %r not permitted" |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1088 raise ValueError(msg % octet_str) |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1089 if octet_int > 255: |
|
77967
d03dbc324b60
Issue 14814: Explain how to get more error detail in the ipaddress tutorial, and tweak the display for octet errors in IPv4 (noticed the formatting problem when adding to the docs)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77965
diff
changeset
|
1090 raise ValueError("Octet %d (> 255) not permitted" % octet_int) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1091 return octet_int |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1092 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1093 def _string_from_ip_int(self, ip_int): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1094 """Turns a 32-bit integer into dotted decimal notation. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1095 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1096 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1097 ip_int: An integer, the IP address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1098 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1099 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1100 The IP address as a string in dotted decimal notation. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1101 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1102 """ |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1103 return '.'.join(map(str, ip_int.to_bytes(4, 'big'))) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1104 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1105 def _is_valid_netmask(self, netmask): |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1106 """Verify that the netmask is valid. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1107 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1108 Args: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1109 netmask: A string, either a prefix or dotted decimal |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1110 netmask. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1111 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1112 Returns: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1113 A boolean, True if the prefix represents a valid IPv4 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1114 netmask. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1115 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1116 """ |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1117 mask = netmask.split('.') |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1118 if len(mask) == 4: |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1119 try: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1120 for x in mask: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1121 if int(x) not in self._valid_mask_octets: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1122 return False |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1123 except ValueError: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1124 # Found something that isn't an integer or isn't valid |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1125 return False |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1126 for idx, y in enumerate(mask): |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1127 if idx > 0 and y > mask[idx - 1]: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1128 return False |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1129 return True |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1130 try: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1131 netmask = int(netmask) |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1132 except ValueError: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1133 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1134 return 0 <= netmask <= self._max_prefixlen |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1135 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1136 def _is_hostmask(self, ip_str): |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1137 """Test if the IP string is a hostmask (rather than a netmask). |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1138 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1139 Args: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1140 ip_str: A string, the potential hostmask. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1141 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1142 Returns: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1143 A boolean, True if the IP string is a hostmask. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1144 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1145 """ |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1146 bits = ip_str.split('.') |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1147 try: |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1148 parts = [x for x in map(int, bits) if x in self._valid_mask_octets] |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1149 except ValueError: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1150 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1151 if len(parts) != len(bits): |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1152 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1153 if parts[0] < parts[-1]: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1154 return True |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1155 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1156 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1157 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1158 def max_prefixlen(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1159 return self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1160 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1161 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1162 def version(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1163 return self._version |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1164 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1165 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1166 class IPv4Address(_BaseV4, _BaseAddress): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1167 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1168 """Represent and manipulate single IPv4 Addresses.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1169 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1170 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1171 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1172 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1173 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1174 address: A string or integer representing the IP |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1175 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1176 Additionally, an integer can be passed, so |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1177 IPv4Address('192.0.2.1') == IPv4Address(3221225985). |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1178 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1179 IPv4Address(int(IPv4Address('192.0.2.1'))) == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1180 IPv4Address('192.0.2.1') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1181 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1182 Raises: |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
1183 AddressValueError: If ipaddress isn't a valid IPv4 address. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1184 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1185 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1186 _BaseAddress.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1187 _BaseV4.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1188 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1189 # Efficient constructor from integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1190 if isinstance(address, int): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1191 self._check_int_address(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1192 self._ip = address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1193 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1194 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1195 # Constructing from a packed address |
|
77953
30e8f2242190
Issue 14814: Eliminate bytes warnings from ipaddress by correctly throwing an exception early when given bytes data of the wrong length. Also removes 2.x backwards compatibility code from associated tests.
Nick Coghlan <ncoghlan@gmail.com>
parents:
77952
diff
changeset
|
1196 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1197 self._check_packed_address(address, 4) |
|
78662
811d91591f73
Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
Nick Coghlan <ncoghlan@gmail.com>
parents:
78661
diff
changeset
|
1198 self._ip = int.from_bytes(address, 'big') |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1199 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1200 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1201 # Assume input argument to be string or any object representation |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1202 # which converts into a formatted IP string. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1203 addr_str = str(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1204 self._ip = self._ip_int_from_string(addr_str) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1205 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1206 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1207 def packed(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1208 """The binary representation of this address.""" |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1209 return v4_int_to_packed(self._ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1210 |
|
78428
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1211 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1212 def is_reserved(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1213 """Test if the address is otherwise IETF reserved. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1214 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1215 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1216 A boolean, True if the address is within the |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1217 reserved IPv4 Network range. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1218 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1219 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1220 reserved_network = IPv4Network('240.0.0.0/4') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1221 return self in reserved_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1222 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1223 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1224 def is_private(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1225 """Test if this address is allocated for private networks. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1226 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1227 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1228 A boolean, True if the address is reserved per RFC 1918. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1229 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1230 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1231 private_10 = IPv4Network('10.0.0.0/8') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1232 private_172 = IPv4Network('172.16.0.0/12') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1233 private_192 = IPv4Network('192.168.0.0/16') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1234 return (self in private_10 or |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1235 self in private_172 or |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1236 self in private_192) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1237 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1238 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1239 def is_multicast(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1240 """Test if the address is reserved for multicast use. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1241 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1242 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1243 A boolean, True if the address is multicast. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1244 See RFC 3171 for details. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1245 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1246 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1247 multicast_network = IPv4Network('224.0.0.0/4') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1248 return self in multicast_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1249 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1250 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1251 def is_unspecified(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1252 """Test if the address is unspecified. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1253 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1254 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1255 A boolean, True if this is the unspecified address as defined in |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1256 RFC 5735 3. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1257 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1258 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1259 unspecified_address = IPv4Address('0.0.0.0') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1260 return self == unspecified_address |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1261 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1262 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1263 def is_loopback(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1264 """Test if the address is a loopback address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1265 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1266 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1267 A boolean, True if the address is a loopback per RFC 3330. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1268 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1269 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1270 loopback_network = IPv4Network('127.0.0.0/8') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1271 return self in loopback_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1272 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1273 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1274 def is_link_local(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1275 """Test if the address is reserved for link-local. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1276 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1277 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1278 A boolean, True if the address is link-local per RFC 3927. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1279 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1280 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1281 linklocal_network = IPv4Network('169.254.0.0/16') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1282 return self in linklocal_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1283 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1284 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1285 class IPv4Interface(IPv4Address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1286 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1287 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1288 if isinstance(address, (bytes, int)): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1289 IPv4Address.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1290 self.network = IPv4Network(self._ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1291 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1292 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1293 |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1294 addr = _split_optional_netmask(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1295 IPv4Address.__init__(self, addr[0]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1296 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1297 self.network = IPv4Network(address, strict=False) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1298 self._prefixlen = self.network._prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1299 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1300 self.netmask = self.network.netmask |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1301 self.hostmask = self.network.hostmask |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1302 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1303 def __str__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1304 return '%s/%d' % (self._string_from_ip_int(self._ip), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1305 self.network.prefixlen) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1306 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1307 def __eq__(self, other): |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1308 address_equal = IPv4Address.__eq__(self, other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1309 if not address_equal or address_equal is NotImplemented: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1310 return address_equal |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1311 try: |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1312 return self.network == other.network |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1313 except AttributeError: |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1314 # An interface with an associated network is NOT the |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1315 # same as an unassociated address. That's why the hash |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1316 # takes the extra info into account. |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1317 return False |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1318 |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1319 def __lt__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1320 address_less = IPv4Address.__lt__(self, other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1321 if address_less is NotImplemented: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1322 return NotImplemented |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1323 try: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1324 return self.network < other.network |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1325 except AttributeError: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1326 # We *do* allow addresses and interfaces to be sorted. The |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1327 # unassociated address is considered less than all interfaces. |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1328 return False |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1329 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1330 def __hash__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1331 return self._ip ^ self._prefixlen ^ int(self.network.network_address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1332 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1333 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1334 def ip(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1335 return IPv4Address(self._ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1336 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1337 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1338 def with_prefixlen(self): |
|
78661
258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
Nick Coghlan <ncoghlan@gmail.com>
parents:
78430
diff
changeset
|
1339 return '%s/%s' % (self._string_from_ip_int(self._ip), |
|
258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
Nick Coghlan <ncoghlan@gmail.com>
parents:
78430
diff
changeset
|
1340 self._prefixlen) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1341 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1342 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1343 def with_netmask(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1344 return '%s/%s' % (self._string_from_ip_int(self._ip), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1345 self.netmask) |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1346 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1347 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1348 def with_hostmask(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1349 return '%s/%s' % (self._string_from_ip_int(self._ip), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1350 self.hostmask) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1351 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1352 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1353 class IPv4Network(_BaseV4, _BaseNetwork): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1354 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1355 """This class represents and manipulates 32-bit IPv4 network + addresses.. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1356 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1357 Attributes: [examples for IPv4Network('192.0.2.0/27')] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1358 .network_address: IPv4Address('192.0.2.0') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1359 .hostmask: IPv4Address('0.0.0.31') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1360 .broadcast_address: IPv4Address('192.0.2.32') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1361 .netmask: IPv4Address('255.255.255.224') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1362 .prefixlen: 27 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1363 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1364 """ |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
1365 # Class to use when creating address objects |
|
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
1366 _address_class = IPv4Address |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1367 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1368 def __init__(self, address, strict=True): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1369 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1370 """Instantiate a new IPv4 network object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1371 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1372 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1373 address: A string or integer representing the IP [& network]. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1374 '192.0.2.0/24' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1375 '192.0.2.0/255.255.255.0' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1376 '192.0.0.2/0.0.0.255' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1377 are all functionally the same in IPv4. Similarly, |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1378 '192.0.2.1' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1379 '192.0.2.1/255.255.255.255' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1380 '192.0.2.1/32' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1381 are also functionaly equivalent. That is to say, failing to |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1382 provide a subnetmask will create an object with a mask of /32. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1383 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1384 If the mask (portion after the / in the argument) is given in |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1385 dotted quad form, it is treated as a netmask if it starts with a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1386 non-zero field (e.g. /255.0.0.0 == /8) and as a hostmask if it |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1387 starts with a zero field (e.g. 0.255.255.255 == /8), with the |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1388 single exception of an all-zero mask which is treated as a |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1389 netmask == /0. If no mask is given, a default of /32 is used. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1390 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1391 Additionally, an integer can be passed, so |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1392 IPv4Network('192.0.2.1') == IPv4Network(3221225985) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1393 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1394 IPv4Interface(int(IPv4Interface('192.0.2.1'))) == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1395 IPv4Interface('192.0.2.1') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1396 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1397 Raises: |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
1398 AddressValueError: If ipaddress isn't a valid IPv4 address. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1399 NetmaskValueError: If the netmask isn't valid for |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1400 an IPv4 address. |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
1401 ValueError: If strict is True and a network address is not |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1402 supplied. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1403 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1404 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1405 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1406 _BaseV4.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1407 _BaseNetwork.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1408 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1409 # Constructing from a packed address |
|
77953
30e8f2242190
Issue 14814: Eliminate bytes warnings from ipaddress by correctly throwing an exception early when given bytes data of the wrong length. Also removes 2.x backwards compatibility code from associated tests.
Nick Coghlan <ncoghlan@gmail.com>
parents:
77952
diff
changeset
|
1410 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1411 self.network_address = IPv4Address(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1412 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1413 self.netmask = IPv4Address(self._ALL_ONES) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1414 #fixme: address/network test here |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1415 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1416 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1417 # Efficient constructor from integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1418 if isinstance(address, int): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1419 self.network_address = IPv4Address(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1420 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1421 self.netmask = IPv4Address(self._ALL_ONES) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1422 #fixme: address/network test here. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1423 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1424 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1425 # Assume input argument to be string or any object representation |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1426 # which converts into a formatted IP prefix string. |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1427 addr = _split_optional_netmask(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1428 self.network_address = IPv4Address(self._ip_int_from_string(addr[0])) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1429 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1430 if len(addr) == 2: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1431 mask = addr[1].split('.') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1432 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1433 if len(mask) == 4: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1434 # We have dotted decimal netmask. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1435 if self._is_valid_netmask(addr[1]): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1436 self.netmask = IPv4Address(self._ip_int_from_string( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1437 addr[1])) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1438 elif self._is_hostmask(addr[1]): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1439 self.netmask = IPv4Address( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1440 self._ip_int_from_string(addr[1]) ^ self._ALL_ONES) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1441 else: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1442 raise NetmaskValueError('%r is not a valid netmask' |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1443 % addr[1]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1444 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1445 self._prefixlen = self._prefix_from_ip_int(int(self.netmask)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1446 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1447 # We have a netmask in prefix length form. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1448 if not self._is_valid_netmask(addr[1]): |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1449 raise NetmaskValueError('%r is not a valid netmask' |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1450 % addr[1]) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1451 self._prefixlen = int(addr[1]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1452 self.netmask = IPv4Address(self._ip_int_from_prefix( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1453 self._prefixlen)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1454 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1455 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1456 self.netmask = IPv4Address(self._ip_int_from_prefix( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1457 self._prefixlen)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1458 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1459 if strict: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1460 if (IPv4Address(int(self.network_address) & int(self.netmask)) != |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1461 self.network_address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1462 raise ValueError('%s has host bits set' % self) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1463 self.network_address = IPv4Address(int(self.network_address) & |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1464 int(self.netmask)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1465 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1466 if self._prefixlen == (self._max_prefixlen - 1): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1467 self.hosts = self.__iter__ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1468 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1469 |
|
77281
0eb63de72e96
#14814: Remove 2.x's new-style classes syntax from ipaddress
Hynek Schlawack <hs@ox.cx>
parents:
77274
diff
changeset
|
1470 class _BaseV6: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1471 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1472 """Base IPv6 object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1473 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1474 The following methods are used by IPv6 objects in both single IP |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1475 addresses and networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1476 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1477 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1478 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1479 _ALL_ONES = (2**IPV6LENGTH) - 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1480 _HEXTET_COUNT = 8 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1481 _HEX_DIGITS = frozenset('0123456789ABCDEFabcdef') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1482 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1483 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1484 self._version = 6 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1485 self._max_prefixlen = IPV6LENGTH |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1486 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1487 def _ip_int_from_string(self, ip_str): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1488 """Turn an IPv6 ip_str into an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1489 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1490 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1491 ip_str: A string, the IPv6 ip_str. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1492 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1493 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1494 An int, the IPv6 address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1495 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1496 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1497 AddressValueError: if ip_str isn't a valid IPv6 Address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1498 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1499 """ |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1500 if not ip_str: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1501 raise AddressValueError('Address cannot be empty') |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1502 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1503 parts = ip_str.split(':') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1504 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1505 # An IPv6 address needs at least 2 colons (3 parts). |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1506 _min_parts = 3 |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1507 if len(parts) < _min_parts: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1508 msg = "At least %d parts expected in %r" % (_min_parts, ip_str) |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1509 raise AddressValueError(msg) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1510 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1511 # If the address has an IPv4-style suffix, convert it to hexadecimal. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1512 if '.' in parts[-1]: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1513 try: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1514 ipv4_int = IPv4Address(parts.pop())._ip |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1515 except AddressValueError as exc: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1516 raise AddressValueError("%s in %r" % (exc, ip_str)) from None |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1517 parts.append('%x' % ((ipv4_int >> 16) & 0xFFFF)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1518 parts.append('%x' % (ipv4_int & 0xFFFF)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1519 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1520 # An IPv6 address can't have more than 8 colons (9 parts). |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1521 # The extra colon comes from using the "::" notation for a single |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1522 # leading or trailing zero part. |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1523 _max_parts = self._HEXTET_COUNT + 1 |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1524 if len(parts) > _max_parts: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1525 msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str) |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1526 raise AddressValueError(msg) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1527 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1528 # Disregarding the endpoints, find '::' with nothing in between. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1529 # This indicates that a run of zeroes has been skipped. |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1530 skip_index = None |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1531 for i in range(1, len(parts) - 1): |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1532 if not parts[i]: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1533 if skip_index is not None: |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1534 # Can't have more than one '::' |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1535 msg = "At most one '::' permitted in %r" % ip_str |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1536 raise AddressValueError(msg) |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1537 skip_index = i |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1538 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1539 # parts_hi is the number of parts to copy from above/before the '::' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1540 # parts_lo is the number of parts to copy from below/after the '::' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1541 if skip_index is not None: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1542 # If we found a '::', then check if it also covers the endpoints. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1543 parts_hi = skip_index |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1544 parts_lo = len(parts) - skip_index - 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1545 if not parts[0]: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1546 parts_hi -= 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1547 if parts_hi: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1548 msg = "Leading ':' only permitted as part of '::' in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1549 raise AddressValueError(msg % ip_str) # ^: requires ^:: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1550 if not parts[-1]: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1551 parts_lo -= 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1552 if parts_lo: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1553 msg = "Trailing ':' only permitted as part of '::' in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1554 raise AddressValueError(msg % ip_str) # :$ requires ::$ |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1555 parts_skipped = self._HEXTET_COUNT - (parts_hi + parts_lo) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1556 if parts_skipped < 1: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1557 msg = "Expected at most %d other parts with '::' in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1558 raise AddressValueError(msg % (self._HEXTET_COUNT-1, ip_str)) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1559 else: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1560 # Otherwise, allocate the entire address to parts_hi. The |
|
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1561 # endpoints could still be empty, but _parse_hextet() will check |
|
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1562 # for that. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1563 if len(parts) != self._HEXTET_COUNT: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1564 msg = "Exactly %d parts expected without '::' in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1565 raise AddressValueError(msg % (self._HEXTET_COUNT, ip_str)) |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1566 if not parts[0]: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1567 msg = "Leading ':' only permitted as part of '::' in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1568 raise AddressValueError(msg % ip_str) # ^: requires ^:: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1569 if not parts[-1]: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1570 msg = "Trailing ':' only permitted as part of '::' in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1571 raise AddressValueError(msg % ip_str) # :$ requires ::$ |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1572 parts_hi = len(parts) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1573 parts_lo = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1574 parts_skipped = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1575 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1576 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1577 # Now, parse the hextets into a 128-bit integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1578 ip_int = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1579 for i in range(parts_hi): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1580 ip_int <<= 16 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1581 ip_int |= self._parse_hextet(parts[i]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1582 ip_int <<= 16 * parts_skipped |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1583 for i in range(-parts_lo, 0): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1584 ip_int <<= 16 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1585 ip_int |= self._parse_hextet(parts[i]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1586 return ip_int |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1587 except ValueError as exc: |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1588 raise AddressValueError("%s in %r" % (exc, ip_str)) from None |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1589 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1590 def _parse_hextet(self, hextet_str): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1591 """Convert an IPv6 hextet string into an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1592 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1593 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1594 hextet_str: A string, the number to parse. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1595 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1596 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1597 The hextet as an integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1598 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1599 Raises: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1600 ValueError: if the input isn't strictly a hex number from |
|
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1601 [0..FFFF]. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1602 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1603 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1604 # Whitelist the characters, since int() allows a lot of bizarre stuff. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1605 if not self._HEX_DIGITS.issuperset(hextet_str): |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1606 raise ValueError("Only hex digits permitted in %r" % hextet_str) |
|
78004
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1607 # We do the length check second, since the invalid character error |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1608 # is likely to be more informative for the user |
|
77952
b7cfdb48af62
Issue 14814: Better handling of cases where octet/hextet parsing fails, including ensuring that tracebacks are still clean even when calling class constructors directly
Nick Coghlan <ncoghlan@gmail.com>
parents:
77478
diff
changeset
|
1609 if len(hextet_str) > 4: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1610 msg = "At most 4 characters permitted in %r" |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1611 raise ValueError(msg % hextet_str) |
|
78004
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1612 # Length check means we can skip checking the integer value |
|
7aa75ea4116d
Issue 14814: The new systematic tests aren't just about error reporting any more - change names accordingly. Added and tweaked some example to ensure they were covering the intended code paths
Nick Coghlan <ncoghlan@gmail.com>
parents:
78001
diff
changeset
|
1613 return int(hextet_str, 16) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1614 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1615 def _compress_hextets(self, hextets): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1616 """Compresses a list of hextets. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1617 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1618 Compresses a list of strings, replacing the longest continuous |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1619 sequence of "0" in the list with "" and adding empty strings at |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1620 the beginning or at the end of the string such that subsequently |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1621 calling ":".join(hextets) will produce the compressed version of |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1622 the IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1623 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1624 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1625 hextets: A list of strings, the hextets to compress. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1626 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1627 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1628 A list of strings. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1629 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1630 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1631 best_doublecolon_start = -1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1632 best_doublecolon_len = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1633 doublecolon_start = -1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1634 doublecolon_len = 0 |
|
78662
811d91591f73
Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
Nick Coghlan <ncoghlan@gmail.com>
parents:
78661
diff
changeset
|
1635 for index, hextet in enumerate(hextets): |
|
811d91591f73
Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
Nick Coghlan <ncoghlan@gmail.com>
parents:
78661
diff
changeset
|
1636 if hextet == '0': |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1637 doublecolon_len += 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1638 if doublecolon_start == -1: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1639 # Start of a sequence of zeros. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1640 doublecolon_start = index |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1641 if doublecolon_len > best_doublecolon_len: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1642 # This is the longest sequence of zeros so far. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1643 best_doublecolon_len = doublecolon_len |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1644 best_doublecolon_start = doublecolon_start |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1645 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1646 doublecolon_len = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1647 doublecolon_start = -1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1648 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1649 if best_doublecolon_len > 1: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1650 best_doublecolon_end = (best_doublecolon_start + |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1651 best_doublecolon_len) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1652 # For zeros at the end of the address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1653 if best_doublecolon_end == len(hextets): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1654 hextets += [''] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1655 hextets[best_doublecolon_start:best_doublecolon_end] = [''] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1656 # For zeros at the beginning of the address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1657 if best_doublecolon_start == 0: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1658 hextets = [''] + hextets |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1659 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1660 return hextets |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1661 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1662 def _string_from_ip_int(self, ip_int=None): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1663 """Turns a 128-bit integer into hexadecimal notation. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1664 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1665 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1666 ip_int: An integer, the IP address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1667 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1668 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1669 A string, the hexadecimal representation of the address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1670 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1671 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1672 ValueError: The address is bigger than 128 bits of all ones. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1673 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1674 """ |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1675 if ip_int is None: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1676 ip_int = int(self._ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1677 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1678 if ip_int > self._ALL_ONES: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1679 raise ValueError('IPv6 address is too large') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1680 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1681 hex_str = '%032x' % ip_int |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1682 hextets = ['%x' % int(hex_str[x:x+4], 16) for x in range(0, 32, 4)] |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1683 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1684 hextets = self._compress_hextets(hextets) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1685 return ':'.join(hextets) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1686 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1687 def _explode_shorthand_ip_string(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1688 """Expand a shortened IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1689 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1690 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1691 ip_str: A string, the IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1692 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1693 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1694 A string, the expanded IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1695 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1696 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1697 if isinstance(self, IPv6Network): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1698 ip_str = str(self.network_address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1699 elif isinstance(self, IPv6Interface): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1700 ip_str = str(self.ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1701 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1702 ip_str = str(self) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1703 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1704 ip_int = self._ip_int_from_string(ip_str) |
|
77965
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1705 hex_str = '%032x' % ip_int |
|
af4ae710daf3
Issue 14814: Make the ipaddress code easier to follow by using newer language features (patch by Serhiy Storchaka)
Nick Coghlan <ncoghlan@gmail.com>
parents:
77960
diff
changeset
|
1706 parts = [hex_str[x:x+4] for x in range(0, 32, 4)] |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1707 if isinstance(self, (_BaseNetwork, IPv6Interface)): |
|
78430
cf9526db1a7e
Issue #14814: Remove redundant property from interface objects - prefixlen can be accessed via the associated network object
Nick Coghlan <ncoghlan@gmail.com>
parents:
78428
diff
changeset
|
1708 return '%s/%d' % (':'.join(parts), self._prefixlen) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1709 return ':'.join(parts) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1710 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1711 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1712 def max_prefixlen(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1713 return self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1714 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1715 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1716 def version(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1717 return self._version |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1718 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1719 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1720 class IPv6Address(_BaseV6, _BaseAddress): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1721 |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
1722 """Represent and manipulate single IPv6 Addresses.""" |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1723 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1724 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1725 """Instantiate a new IPv6 address object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1726 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1727 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1728 address: A string or integer representing the IP |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1729 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1730 Additionally, an integer can be passed, so |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1731 IPv6Address('2001:db8::') == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1732 IPv6Address(42540766411282592856903984951653826560) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1733 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1734 IPv6Address(int(IPv6Address('2001:db8::'))) == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1735 IPv6Address('2001:db8::') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1736 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1737 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1738 AddressValueError: If address isn't a valid IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1739 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1740 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1741 _BaseAddress.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1742 _BaseV6.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1743 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1744 # Efficient constructor from integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1745 if isinstance(address, int): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1746 self._check_int_address(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1747 self._ip = address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1748 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1749 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1750 # Constructing from a packed address |
|
77953
30e8f2242190
Issue 14814: Eliminate bytes warnings from ipaddress by correctly throwing an exception early when given bytes data of the wrong length. Also removes 2.x backwards compatibility code from associated tests.
Nick Coghlan <ncoghlan@gmail.com>
parents:
77952
diff
changeset
|
1751 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1752 self._check_packed_address(address, 16) |
|
78662
811d91591f73
Close #14814: Avoid depending on struct by using newer features. Also use enumerate where appropriate (patch by Serhiy Storchaka). Declaring PEP 3144 final at this point - any further changes to code or docs can go in new issues.
Nick Coghlan <ncoghlan@gmail.com>
parents:
78661
diff
changeset
|
1753 self._ip = int.from_bytes(address, 'big') |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1754 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1755 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1756 # Assume input argument to be string or any object representation |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1757 # which converts into a formatted IP string. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1758 addr_str = str(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1759 self._ip = self._ip_int_from_string(addr_str) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1760 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1761 @property |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1762 def packed(self): |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1763 """The binary representation of this address.""" |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1764 return v6_int_to_packed(self._ip) |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1765 |
|
78428
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1766 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1767 def is_multicast(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1768 """Test if the address is reserved for multicast use. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1769 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1770 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1771 A boolean, True if the address is a multicast address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1772 See RFC 2373 2.7 for details. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1773 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1774 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1775 multicast_network = IPv6Network('ff00::/8') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1776 return self in multicast_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1777 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1778 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1779 def is_reserved(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1780 """Test if the address is otherwise IETF reserved. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1781 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1782 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1783 A boolean, True if the address is within one of the |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1784 reserved IPv6 Network ranges. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1785 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1786 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1787 reserved_networks = [IPv6Network('::/8'), IPv6Network('100::/8'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1788 IPv6Network('200::/7'), IPv6Network('400::/6'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1789 IPv6Network('800::/5'), IPv6Network('1000::/4'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1790 IPv6Network('4000::/3'), IPv6Network('6000::/3'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1791 IPv6Network('8000::/3'), IPv6Network('A000::/3'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1792 IPv6Network('C000::/3'), IPv6Network('E000::/4'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1793 IPv6Network('F000::/5'), IPv6Network('F800::/6'), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1794 IPv6Network('FE00::/9')] |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1795 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1796 return any(self in x for x in reserved_networks) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1797 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1798 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1799 def is_link_local(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1800 """Test if the address is reserved for link-local. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1801 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1802 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1803 A boolean, True if the address is reserved per RFC 4291. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1804 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1805 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1806 linklocal_network = IPv6Network('fe80::/10') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1807 return self in linklocal_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1808 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1809 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1810 def is_site_local(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1811 """Test if the address is reserved for site-local. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1812 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1813 Note that the site-local address space has been deprecated by RFC 3879. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1814 Use is_private to test if this address is in the space of unique local |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1815 addresses as defined by RFC 4193. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1816 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1817 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1818 A boolean, True if the address is reserved per RFC 3513 2.5.6. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1819 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1820 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1821 sitelocal_network = IPv6Network('fec0::/10') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1822 return self in sitelocal_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1823 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1824 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1825 def is_private(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1826 """Test if this address is allocated for private networks. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1827 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1828 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1829 A boolean, True if the address is reserved per RFC 4193. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1830 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1831 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1832 private_network = IPv6Network('fc00::/7') |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1833 return self in private_network |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1834 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1835 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1836 def is_unspecified(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1837 """Test if the address is unspecified. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1838 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1839 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1840 A boolean, True if this is the unspecified address as defined in |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1841 RFC 2373 2.5.2. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1842 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1843 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1844 return self._ip == 0 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1845 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1846 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1847 def is_loopback(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1848 """Test if the address is a loopback address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1849 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1850 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1851 A boolean, True if the address is a loopback address as defined in |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1852 RFC 2373 2.5.3. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1853 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1854 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1855 return self._ip == 1 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1856 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1857 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1858 def ipv4_mapped(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1859 """Return the IPv4 mapped address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1860 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1861 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1862 If the IPv6 address is a v4 mapped address, return the |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1863 IPv4 mapped address. Return None otherwise. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1864 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1865 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1866 if (self._ip >> 32) != 0xFFFF: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1867 return None |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1868 return IPv4Address(self._ip & 0xFFFFFFFF) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1869 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1870 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1871 def teredo(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1872 """Tuple of embedded teredo IPs. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1873 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1874 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1875 Tuple of the (server, client) IPs or None if the address |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1876 doesn't appear to be a teredo address (doesn't start with |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1877 2001::/32) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1878 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1879 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1880 if (self._ip >> 96) != 0x20010000: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1881 return None |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1882 return (IPv4Address((self._ip >> 64) & 0xFFFFFFFF), |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1883 IPv4Address(~self._ip & 0xFFFFFFFF)) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1884 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1885 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1886 def sixtofour(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1887 """Return the IPv4 6to4 embedded address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1888 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1889 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1890 The IPv4 6to4-embedded address if present or None if the |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1891 address doesn't appear to contain a 6to4 embedded address. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1892 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1893 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1894 if (self._ip >> 112) != 0x2002: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1895 return None |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1896 return IPv4Address((self._ip >> 80) & 0xFFFFFFFF) |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1897 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1898 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1899 class IPv6Interface(IPv6Address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1900 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1901 def __init__(self, address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1902 if isinstance(address, (bytes, int)): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1903 IPv6Address.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1904 self.network = IPv6Network(self._ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1905 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1906 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1907 |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
1908 addr = _split_optional_netmask(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1909 IPv6Address.__init__(self, addr[0]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1910 self.network = IPv6Network(address, strict=False) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1911 self.netmask = self.network.netmask |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1912 self._prefixlen = self.network._prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1913 self.hostmask = self.network.hostmask |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1914 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1915 def __str__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1916 return '%s/%d' % (self._string_from_ip_int(self._ip), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1917 self.network.prefixlen) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1918 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1919 def __eq__(self, other): |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1920 address_equal = IPv6Address.__eq__(self, other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1921 if not address_equal or address_equal is NotImplemented: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1922 return address_equal |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1923 try: |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1924 return self.network == other.network |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1925 except AttributeError: |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1926 # An interface with an associated network is NOT the |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1927 # same as an unassociated address. That's why the hash |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1928 # takes the extra info into account. |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1929 return False |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1930 |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1931 def __lt__(self, other): |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1932 address_less = IPv6Address.__lt__(self, other) |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1933 if address_less is NotImplemented: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1934 return NotImplemented |
|
77971
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1935 try: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1936 return self.network < other.network |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1937 except AttributeError: |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1938 # We *do* allow addresses and interfaces to be sorted. The |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1939 # unassociated address is considered less than all interfaces. |
|
9b359b6c9a39
Issue 14814: Ensure ordering semantics across all 3 entity types in ipaddress are consistent and well-defined
Nick Coghlan <ncoghlan@gmail.com>
parents:
77970
diff
changeset
|
1940 return False |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1941 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1942 def __hash__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1943 return self._ip ^ self._prefixlen ^ int(self.network.network_address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1944 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1945 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1946 def ip(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1947 return IPv6Address(self._ip) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1948 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1949 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1950 def with_prefixlen(self): |
|
78661
258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
Nick Coghlan <ncoghlan@gmail.com>
parents:
78430
diff
changeset
|
1951 return '%s/%s' % (self._string_from_ip_int(self._ip), |
|
258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
Nick Coghlan <ncoghlan@gmail.com>
parents:
78430
diff
changeset
|
1952 self._prefixlen) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1953 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1954 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1955 def with_netmask(self): |
|
78661
258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
Nick Coghlan <ncoghlan@gmail.com>
parents:
78430
diff
changeset
|
1956 return '%s/%s' % (self._string_from_ip_int(self._ip), |
|
258558e36d8a
Issue #14814: document the Interface APIs and fix various problems with the string representations (initial patch by Eli Bendersky).
Nick Coghlan <ncoghlan@gmail.com>
parents:
78430
diff
changeset
|
1957 self.netmask) |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1958 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1959 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1960 def with_hostmask(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1961 return '%s/%s' % (self._string_from_ip_int(self._ip), |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1962 self.hostmask) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1963 |
|
78428
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1964 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1965 def is_unspecified(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1966 return self._ip == 0 and self.network.is_unspecified |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1967 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1968 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1969 def is_loopback(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1970 return self._ip == 1 and self.network.is_loopback |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1971 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1972 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1973 class IPv6Network(_BaseV6, _BaseNetwork): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1974 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1975 """This class represents and manipulates 128-bit IPv6 networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1976 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1977 Attributes: [examples for IPv6('2001:db8::1000/124')] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1978 .network_address: IPv6Address('2001:db8::1000') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1979 .hostmask: IPv6Address('::f') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1980 .broadcast_address: IPv6Address('2001:db8::100f') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1981 .netmask: IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1982 .prefixlen: 124 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1983 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1984 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1985 |
|
77160
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
1986 # Class to use when creating address objects |
|
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
1987 _address_class = IPv6Address |
|
f70e12499d05
Issue #14814: In the spirit of TOOWTDI, ditch the redundant version parameter to the factory functions by using the appropriate direct class references instead
Nick Coghlan <ncoghlan@gmail.com>
parents:
77159
diff
changeset
|
1988 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1989 def __init__(self, address, strict=True): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1990 """Instantiate a new IPv6 Network object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1991 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1992 Args: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1993 address: A string or integer representing the IPv6 network or the |
|
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1994 IP and prefix/netmask. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1995 '2001:db8::/128' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1996 '2001:db8:0000:0000:0000:0000:0000:0000/128' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1997 '2001:db8::' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1998 are all functionally the same in IPv6. That is to say, |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1999 failing to provide a subnetmask will create an object with |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2000 a mask of /128. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2001 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2002 Additionally, an integer can be passed, so |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2003 IPv6Network('2001:db8::') == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2004 IPv6Network(42540766411282592856903984951653826560) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2005 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2006 IPv6Network(int(IPv6Network('2001:db8::'))) == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2007 IPv6Network('2001:db8::') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2008 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2009 strict: A boolean. If true, ensure that we have been passed |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2010 A true network address, eg, 2001:db8::1000/124 and not an |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2011 IP address on a network, eg, 2001:db8::1/124. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2012 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2013 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2014 AddressValueError: If address isn't a valid IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2015 NetmaskValueError: If the netmask isn't valid for |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2016 an IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2017 ValueError: If strict was True and a network address was not |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2018 supplied. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2019 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2020 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2021 _BaseV6.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2022 _BaseNetwork.__init__(self, address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2023 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2024 # Efficient constructor from integer. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2025 if isinstance(address, int): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2026 self.network_address = IPv6Address(address) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2027 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2028 self.netmask = IPv6Address(self._ALL_ONES) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2029 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2030 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2031 # Constructing from a packed address |
|
77953
30e8f2242190
Issue 14814: Eliminate bytes warnings from ipaddress by correctly throwing an exception early when given bytes data of the wrong length. Also removes 2.x backwards compatibility code from associated tests.
Nick Coghlan <ncoghlan@gmail.com>
parents:
77952
diff
changeset
|
2032 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
2033 self.network_address = IPv6Address(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2034 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2035 self.netmask = IPv6Address(self._ALL_ONES) |
|
77383
4aeb5b9b62d7
#14814: Remove redundant code from ipaddress.IPv6Network
Hynek Schlawack <hs@ox.cx>
parents:
77360
diff
changeset
|
2036 return |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2037 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2038 # Assume input argument to be string or any object representation |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2039 # which converts into a formatted IP prefix string. |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
2040 addr = _split_optional_netmask(address) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2041 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2042 self.network_address = IPv6Address(self._ip_int_from_string(addr[0])) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2043 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2044 if len(addr) == 2: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2045 if self._is_valid_netmask(addr[1]): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2046 self._prefixlen = int(addr[1]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2047 else: |
|
77960
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
2048 raise NetmaskValueError('%r is not a valid netmask' |
|
16ff4889a858
Issue 14814: Provide more informative error messages in ipaddress, and ensure that errors are caught as expected
Nick Coghlan <ncoghlan@gmail.com>
parents:
77959
diff
changeset
|
2049 % addr[1]) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2050 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2051 self._prefixlen = self._max_prefixlen |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2052 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2053 self.netmask = IPv6Address(self._ip_int_from_prefix(self._prefixlen)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2054 if strict: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2055 if (IPv6Address(int(self.network_address) & int(self.netmask)) != |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2056 self.network_address): |
|
77959
d9c98730e2e8
Issue 14814: %s implies coercion with str() - remove a lot of redundant str() calls from the ipaddress implementation
Nick Coghlan <ncoghlan@gmail.com>
parents:
77953
diff
changeset
|
2057 raise ValueError('%s has host bits set' % self) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2058 self.network_address = IPv6Address(int(self.network_address) & |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2059 int(self.netmask)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2060 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2061 if self._prefixlen == (self._max_prefixlen - 1): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2062 self.hosts = self.__iter__ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2063 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2064 def _is_valid_netmask(self, prefixlen): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2065 """Verify that the netmask/prefixlen is valid. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2066 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2067 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2068 prefixlen: A string, the netmask in prefix length format. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2069 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2070 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2071 A boolean, True if the prefix represents a valid IPv6 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2072 netmask. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2073 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2074 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2075 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2076 prefixlen = int(prefixlen) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2077 except ValueError: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2078 return False |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2079 return 0 <= prefixlen <= self._max_prefixlen |
|
78428
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2080 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2081 @property |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2082 def is_site_local(self): |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2083 """Test if the address is reserved for site-local. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2084 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2085 Note that the site-local address space has been deprecated by RFC 3879. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2086 Use is_private to test if this address is in the space of unique local |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2087 addresses as defined by RFC 4193. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2088 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2089 Returns: |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2090 A boolean, True if the address is reserved per RFC 3513 2.5.6. |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2091 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2092 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2093 return (self.network_address.is_site_local and |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2094 self.broadcast_address.is_site_local) |
