Mercurial > cpython-lru_cache-weakref
annotate Lib/ipaddress.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | b9623fa5a0dd |
| children |
| 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: |
|
81772
07488c3c85f1
Fix a few typos and a double semicolon. Patch by Eitan Adler.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
79567
diff
changeset
|
726 An iterator of the IPv(4|6)Network objects which is self |
|
77084
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: |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81772
diff
changeset
|
730 TypeError: If self and other are of differing address |
|
77084
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: |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
978 A boolean, True if the address is reserved per |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
979 iana-ipv4-special-registry or iana-ipv6-special-registry. |
|
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
|
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 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
983 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
|
984 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @property |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
986 def is_global(self): |
|
86579
365fd677856f
#17400: fix documentation, add cache to is_global and correctly handle 100.64.0.0/10
Peter Moody <python@hda3.com>
parents:
86552
diff
changeset
|
987 """Test if this address is allocated for public networks. |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
988 |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
989 Returns: |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
990 A boolean, True if the address is not reserved per |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
991 iana-ipv4-special-registry or iana-ipv6-special-registry. |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
992 |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
993 """ |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
994 return not self.is_private |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
995 |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
996 @property |
|
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
|
997 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
|
998 """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
|
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 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
|
1002 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
|
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_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
|
1006 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
|
1007 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1008 @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
|
1009 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
|
1010 """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
|
1011 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1012 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
|
1013 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
|
1014 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
|
1015 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1016 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1017 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
|
1018 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
|
1019 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1020 |
|
77281
0eb63de72e96
#14814: Remove 2.x's new-style classes syntax from ipaddress
Hynek Schlawack <hs@ox.cx>
parents:
77274
diff
changeset
|
1021 class _BaseV4: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1022 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1023 """Base IPv4 object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1024 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1025 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
|
1026 addresses and networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1027 |
|
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 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1030 # 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
|
1031 _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
|
1032 _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
|
1033 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1034 # 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
|
1035 _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
|
1036 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1037 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
|
1038 self._version = 4 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1039 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
|
1040 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1041 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
|
1042 return str(self) |
|
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 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
|
1045 """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
|
1046 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1047 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1048 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
|
1049 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1050 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1051 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
|
1052 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1053 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1054 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
|
1055 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1056 """ |
|
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
|
1057 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
|
1058 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
|
1059 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1060 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
|
1061 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
|
1062 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
|
1063 |
|
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
|
1064 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
|
1065 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
|
1066 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
|
1067 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
|
1068 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1069 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
|
1070 """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
|
1071 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1072 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1073 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
|
1074 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1075 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1076 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
|
1077 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1078 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1079 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
|
1080 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1081 """ |
|
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
|
1082 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
|
1083 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
|
1084 # 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
|
1085 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
|
1086 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
|
1087 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
|
1088 # 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
|
1089 # 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
|
1090 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
|
1091 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
|
1092 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
|
1093 # 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
|
1094 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
|
1095 # 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
|
1096 # 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
|
1097 # 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
|
1098 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
|
1099 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
|
1100 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
|
1101 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
|
1102 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
|
1103 return octet_int |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1104 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1105 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
|
1106 """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
|
1107 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1108 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1109 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
|
1110 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1111 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1112 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
|
1113 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1114 """ |
|
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
|
1115 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
|
1116 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1117 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
|
1118 """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
|
1119 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1120 Args: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1121 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
|
1122 netmask. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1123 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1124 Returns: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1125 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
|
1126 netmask. |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1127 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1128 """ |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1129 mask = netmask.split('.') |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1130 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
|
1131 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
|
1132 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
|
1133 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
|
1134 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
|
1135 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
|
1136 # 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
|
1137 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
|
1138 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
|
1139 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
|
1140 return False |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1141 return True |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1142 try: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1143 netmask = int(netmask) |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1144 except ValueError: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1145 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1146 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
|
1147 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1148 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
|
1149 """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
|
1150 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1151 Args: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1152 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
|
1153 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1154 Returns: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1155 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
|
1156 |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1157 """ |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1158 bits = ip_str.split('.') |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1159 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
|
1160 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
|
1161 except ValueError: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1162 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1163 if len(parts) != len(bits): |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1164 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1165 if parts[0] < parts[-1]: |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1166 return True |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1167 return False |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1168 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1169 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1170 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
|
1171 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
|
1172 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1173 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1174 def version(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1175 return self._version |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1176 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1177 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1178 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
|
1179 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1180 """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
|
1181 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1182 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
|
1183 |
|
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 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1186 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
|
1187 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1188 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
|
1189 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
|
1190 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1191 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
|
1192 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
|
1193 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1194 Raises: |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
1195 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
|
1196 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1197 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1198 _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
|
1199 _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
|
1200 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1201 # 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
|
1202 if isinstance(address, int): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1203 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
|
1204 self._ip = address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1205 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1206 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1207 # 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
|
1208 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1209 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
|
1210 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
|
1211 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1212 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1213 # 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
|
1214 # 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
|
1215 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
|
1216 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
|
1217 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1218 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1219 def packed(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1220 """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
|
1221 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
|
1222 |
|
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
|
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_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
|
1225 """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
|
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 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
|
1229 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
|
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 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1233 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
|
1234 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @property |
|
86579
365fd677856f
#17400: fix documentation, add cache to is_global and correctly handle 100.64.0.0/10
Peter Moody <python@hda3.com>
parents:
86552
diff
changeset
|
1236 @functools.lru_cache() |
|
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
|
1237 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
|
1238 """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
|
1239 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 Returns: |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1241 A boolean, True if the address is reserved per |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1242 iana-ipv4-special-registry. |
|
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
|
1243 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 """ |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1245 return (self in IPv4Network('0.0.0.0/8') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1246 self in IPv4Network('10.0.0.0/8') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1247 self in IPv4Network('127.0.0.0/8') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1248 self in IPv4Network('169.254.0.0/16') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1249 self in IPv4Network('172.16.0.0/12') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1250 self in IPv4Network('192.0.0.0/29') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1251 self in IPv4Network('192.0.0.170/31') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1252 self in IPv4Network('192.0.2.0/24') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1253 self in IPv4Network('192.168.0.0/16') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1254 self in IPv4Network('198.18.0.0/15') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1255 self in IPv4Network('198.51.100.0/24') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1256 self in IPv4Network('203.0.113.0/24') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1257 self in IPv4Network('240.0.0.0/4') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1258 self in IPv4Network('255.255.255.255/32')) |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1259 |
|
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
|
1260 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @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
|
1262 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
|
1263 """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
|
1264 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1266 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
|
1267 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
|
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 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
|
1271 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
|
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_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
|
1275 """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
|
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 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
|
1279 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
|
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 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1283 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
|
1284 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1285 @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
|
1286 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
|
1287 """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
|
1288 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1289 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
|
1290 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
|
1291 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1292 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1293 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
|
1294 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
|
1295 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1296 @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
|
1297 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
|
1298 """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
|
1299 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1300 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
|
1301 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
|
1302 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1303 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1304 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
|
1305 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
|
1306 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1307 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1308 class IPv4Interface(IPv4Address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1309 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1310 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
|
1311 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
|
1312 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
|
1313 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
|
1314 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
|
1315 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1316 |
|
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
|
1317 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
|
1318 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
|
1319 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1320 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
|
1321 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
|
1322 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1323 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
|
1324 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
|
1325 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1326 def __str__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1327 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
|
1328 self.network.prefixlen) |
|
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 __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
|
1331 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
|
1332 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
|
1333 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
|
1334 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
|
1335 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
|
1336 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
|
1337 # 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
|
1338 # 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
|
1339 # 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
|
1340 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
|
1341 |
|
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
|
1342 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
|
1343 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
|
1344 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
|
1345 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
|
1346 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
|
1347 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
|
1348 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
|
1349 # 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
|
1350 # 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
|
1351 return False |
|
77084
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 def __hash__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1354 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
|
1355 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1356 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1357 def ip(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1358 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
|
1359 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1360 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1361 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
|
1362 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
|
1363 self._prefixlen) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1364 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1365 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1366 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
|
1367 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
|
1368 self.netmask) |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1369 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1370 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1371 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
|
1372 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
|
1373 self.hostmask) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1374 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1375 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1376 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
|
1377 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1378 """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
|
1379 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1380 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
|
1381 .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
|
1382 .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
|
1383 .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
|
1384 .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
|
1385 .prefixlen: 27 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1386 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1387 """ |
|
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
|
1388 # 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
|
1389 _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
|
1390 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1391 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
|
1392 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1393 """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
|
1394 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1395 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1396 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
|
1397 '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
|
1398 '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
|
1399 '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
|
1400 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
|
1401 '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
|
1402 '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
|
1403 '192.0.2.1/32' |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81772
diff
changeset
|
1404 are also functionally equivalent. That is to say, failing to |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1405 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
|
1406 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1407 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
|
1408 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
|
1409 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
|
1410 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
|
1411 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
|
1412 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
|
1413 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1414 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
|
1415 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
|
1416 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1417 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
|
1418 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
|
1419 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1420 Raises: |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
1421 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
|
1422 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
|
1423 an IPv4 address. |
|
77295
facdca62aa68
Issue #14814: minor spelling fixes
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77284
diff
changeset
|
1424 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
|
1425 supplied. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1426 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1427 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1428 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1429 _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
|
1430 _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
|
1431 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1432 # 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
|
1433 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1434 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
|
1435 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
|
1436 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
|
1437 #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
|
1438 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1439 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1440 # 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
|
1441 if isinstance(address, int): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1442 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
|
1443 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
|
1444 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
|
1445 #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
|
1446 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1447 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1448 # 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
|
1449 # 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
|
1450 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
|
1451 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
|
1452 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1453 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
|
1454 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
|
1455 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1456 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
|
1457 # 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
|
1458 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
|
1459 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
|
1460 addr[1])) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1461 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
|
1462 self.netmask = IPv4Address( |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1463 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
|
1464 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
|
1465 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
|
1466 % addr[1]) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1467 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1468 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
|
1469 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1470 # 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
|
1471 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
|
1472 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
|
1473 % addr[1]) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1474 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
|
1475 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
|
1476 self._prefixlen)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1477 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1478 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
|
1479 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
|
1480 self._prefixlen)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1481 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1482 if strict: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1483 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
|
1484 self.network_address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1485 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
|
1486 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
|
1487 int(self.netmask)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1488 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1489 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
|
1490 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
|
1491 |
|
86598
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1492 @property |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1493 @functools.lru_cache() |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1494 def is_global(self): |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1495 """Test if this address is allocated for public networks. |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1496 |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1497 Returns: |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1498 A boolean, True if the address is not reserved per |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1499 iana-ipv4-special-registry. |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1500 |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1501 """ |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1502 return (not (self.network_address in IPv4Network('100.64.0.0/10') and |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1503 self.broadcast_address in IPv4Network('100.64.0.0/10')) and |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1504 not self.is_private) |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1505 |
|
b9623fa5a0dd
#17400: correct handling of 100.64.0.0/10, fixing the docs and updating NEWS
Peter Moody <python@hda3.com>
parents:
86579
diff
changeset
|
1506 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1507 |
|
77281
0eb63de72e96
#14814: Remove 2.x's new-style classes syntax from ipaddress
Hynek Schlawack <hs@ox.cx>
parents:
77274
diff
changeset
|
1508 class _BaseV6: |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1509 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1510 """Base IPv6 object. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1511 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1512 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
|
1513 addresses and networks. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1514 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1515 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1516 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1517 _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
|
1518 _HEXTET_COUNT = 8 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1519 _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
|
1520 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1521 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
|
1522 self._version = 6 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1523 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
|
1524 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1525 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
|
1526 """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
|
1527 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1528 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1529 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
|
1530 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1531 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1532 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
|
1533 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1534 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1535 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
|
1536 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1537 """ |
|
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
|
1538 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
|
1539 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
|
1540 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1541 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
|
1542 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1543 # 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
|
1544 _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
|
1545 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
|
1546 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
|
1547 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
|
1548 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1549 # 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
|
1550 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
|
1551 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
|
1552 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
|
1553 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
|
1554 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
|
1555 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
|
1556 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
|
1557 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1558 # 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
|
1559 # 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
|
1560 # 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
|
1561 _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
|
1562 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
|
1563 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
|
1564 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
|
1565 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1566 # 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
|
1567 # 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
|
1568 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
|
1569 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
|
1570 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
|
1571 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
|
1572 # 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
|
1573 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
|
1574 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
|
1575 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
|
1576 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1577 # 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
|
1578 # 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
|
1579 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
|
1580 # 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
|
1581 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
|
1582 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
|
1583 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
|
1584 parts_hi -= 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1585 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
|
1586 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
|
1587 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
|
1588 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
|
1589 parts_lo -= 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1590 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
|
1591 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
|
1592 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
|
1593 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
|
1594 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
|
1595 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
|
1596 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
|
1597 else: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1598 # 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
|
1599 # 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
|
1600 # for that. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1601 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
|
1602 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
|
1603 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
|
1604 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
|
1605 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
|
1606 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
|
1607 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
|
1608 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
|
1609 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
|
1610 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
|
1611 parts_lo = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1612 parts_skipped = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1613 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1614 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1615 # 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
|
1616 ip_int = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1617 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
|
1618 ip_int <<= 16 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1619 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
|
1620 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
|
1621 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
|
1622 ip_int <<= 16 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1623 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
|
1624 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
|
1625 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
|
1626 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
|
1627 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1628 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
|
1629 """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
|
1630 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1631 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1632 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
|
1633 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1634 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1635 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
|
1636 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1637 Raises: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
1638 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
|
1639 [0..FFFF]. |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1640 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1641 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1642 # 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
|
1643 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
|
1644 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
|
1645 # 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
|
1646 # 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
|
1647 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
|
1648 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
|
1649 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
|
1650 # 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
|
1651 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
|
1652 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1653 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
|
1654 """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
|
1655 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1656 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
|
1657 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
|
1658 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
|
1659 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
|
1660 the IPv6 address. |
|
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 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1663 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
|
1664 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1665 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1666 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
|
1667 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1668 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1669 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
|
1670 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
|
1671 doublecolon_start = -1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1672 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
|
1673 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
|
1674 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
|
1675 doublecolon_len += 1 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1676 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
|
1677 # 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
|
1678 doublecolon_start = index |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1679 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
|
1680 # 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
|
1681 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
|
1682 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
|
1683 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1684 doublecolon_len = 0 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1685 doublecolon_start = -1 |
|
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 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
|
1688 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
|
1689 best_doublecolon_len) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1690 # 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
|
1691 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
|
1692 hextets += [''] |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1693 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
|
1694 # 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
|
1695 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
|
1696 hextets = [''] + hextets |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1697 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1698 return hextets |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1699 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1700 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
|
1701 """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
|
1702 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1703 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1704 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
|
1705 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1706 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1707 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
|
1708 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1709 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1710 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
|
1711 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1712 """ |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1713 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
|
1714 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
|
1715 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1716 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
|
1717 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
|
1718 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1719 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
|
1720 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
|
1721 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1722 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
|
1723 return ':'.join(hextets) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1724 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1725 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
|
1726 """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
|
1727 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1728 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1729 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
|
1730 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1731 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1732 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
|
1733 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1734 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1735 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
|
1736 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
|
1737 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
|
1738 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
|
1739 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1740 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
|
1741 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1742 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
|
1743 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
|
1744 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
|
1745 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
|
1746 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
|
1747 return ':'.join(parts) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1748 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1749 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1750 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
|
1751 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
|
1752 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1753 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1754 def version(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1755 return self._version |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1756 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1757 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1758 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
|
1759 |
|
77117
f4f2139202c5
Issue #14814: minor improvements as suggested by Hynek Schlawack
Sandro Tosi <sandro.tosi@gmail.com>
parents:
77116
diff
changeset
|
1760 """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
|
1761 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1762 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
|
1763 """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
|
1764 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1765 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1766 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
|
1767 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1768 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
|
1769 IPv6Address('2001:db8::') == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1770 IPv6Address(42540766411282592856903984951653826560) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1771 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1772 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
|
1773 IPv6Address('2001:db8::') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1774 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1775 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1776 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
|
1777 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1778 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1779 _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
|
1780 _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
|
1781 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1782 # 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
|
1783 if isinstance(address, int): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1784 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
|
1785 self._ip = address |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1786 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1787 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1788 # 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
|
1789 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
1790 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
|
1791 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
|
1792 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1793 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1794 # 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
|
1795 # 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
|
1796 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
|
1797 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
|
1798 |
|
77360
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1799 @property |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1800 def packed(self): |
|
cca2a1cc9598
#14814: ipaddress: refactor dup code, minor janitoring, bump coverage
Hynek Schlawack <hs@ox.cx>
parents:
77345
diff
changeset
|
1801 """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
|
1802 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
|
1803 |
|
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
|
1804 @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
|
1805 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
|
1806 """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
|
1807 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1809 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
|
1810 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
|
1811 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1814 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
|
1815 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @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
|
1817 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
|
1818 """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
|
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 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
|
1821 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
|
1822 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
|
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 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1826 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
|
1827 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
|
1828 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
|
1829 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
|
1830 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
|
1831 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
|
1832 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
|
1833 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1835 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @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
|
1837 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
|
1838 """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
|
1839 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1841 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
|
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 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
|
1845 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
|
1846 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @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
|
1848 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
|
1849 """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
|
1850 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1852 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
|
1853 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
|
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 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
|
1856 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
|
1857 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 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
|
1860 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
|
1861 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 @property |
|
86579
365fd677856f
#17400: fix documentation, add cache to is_global and correctly handle 100.64.0.0/10
Peter Moody <python@hda3.com>
parents:
86552
diff
changeset
|
1863 @functools.lru_cache() |
|
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
|
1864 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
|
1865 """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
|
1866 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 Returns: |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1868 A boolean, True if the address is reserved per |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1869 iana-ipv6-special-registry. |
|
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
|
1870 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly 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 """ |
|
86552
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1872 return (self in IPv6Network('::1/128') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1873 self in IPv6Network('::/128') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1874 self in IPv6Network('::ffff:0:0/96') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1875 self in IPv6Network('100::/64') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1876 self in IPv6Network('2001::/23') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1877 self in IPv6Network('2001:2::/48') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1878 self in IPv6Network('2001:db8::/32') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1879 self in IPv6Network('2001:10::/28') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1880 self in IPv6Network('fc00::/7') or |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1881 self in IPv6Network('fe80::/10')) |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1882 |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1883 @property |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1884 def is_global(self): |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1885 """Test if this address is allocated for public networks. |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1886 |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1887 Returns: |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1888 A boolean, true if the address is not reserved per |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1889 iana-ipv6-special-registry. |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1890 |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1891 """ |
|
2e8dd5c240b7
#17400; ipaddress should make it easy to identify rfc6598 addresses
Peter Moody <python@hda3.com>
parents:
85221
diff
changeset
|
1892 return not self.is_private |
|
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
|
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 @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
|
1895 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
|
1896 """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
|
1897 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1898 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
|
1899 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
|
1900 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
|
1901 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1902 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1903 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
|
1904 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1905 @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
|
1906 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
|
1907 """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
|
1908 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1909 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
|
1910 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
|
1911 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
|
1912 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1913 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1914 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
|
1915 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1916 @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
|
1917 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
|
1918 """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
|
1919 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1920 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
|
1921 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
|
1922 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
|
1923 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1924 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1925 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
|
1926 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
|
1927 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
|
1928 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1929 @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
|
1930 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
|
1931 """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
|
1932 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1933 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
|
1934 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
|
1935 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
|
1936 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
|
1937 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1938 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1939 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
|
1940 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
|
1941 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
|
1942 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
|
1943 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1944 @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
|
1945 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
|
1946 """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
|
1947 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1948 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
|
1949 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
|
1950 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
|
1951 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1952 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
1953 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
|
1954 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
|
1955 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
|
1956 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1957 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1958 class IPv6Interface(IPv6Address): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1959 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1960 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
|
1961 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
|
1962 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
|
1963 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
|
1964 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
|
1965 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1966 |
|
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
|
1967 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
|
1968 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
|
1969 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
|
1970 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
|
1971 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
|
1972 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
|
1973 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1974 def __str__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1975 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
|
1976 self.network.prefixlen) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1977 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1978 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
|
1979 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
|
1980 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
|
1981 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
|
1982 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
|
1983 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
|
1984 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
|
1985 # 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
|
1986 # 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
|
1987 # 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
|
1988 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
|
1989 |
|
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
|
1990 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
|
1991 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
|
1992 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
|
1993 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
|
1994 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
|
1995 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
|
1996 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
|
1997 # 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
|
1998 # 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
|
1999 return False |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2000 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2001 def __hash__(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2002 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
|
2003 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2004 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2005 def ip(self): |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2006 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
|
2007 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2008 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2009 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
|
2010 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
|
2011 self._prefixlen) |
|
77084
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 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2014 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
|
2015 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
|
2016 self.netmask) |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
2017 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2018 @property |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2019 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
|
2020 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
|
2021 self.hostmask) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2022 |
|
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
|
2023 @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
|
2024 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
|
2025 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
|
2026 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2027 @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
|
2028 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
|
2029 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
|
2030 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2031 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2032 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
|
2033 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2034 """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
|
2035 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2036 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
|
2037 .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
|
2038 .hostmask: IPv6Address('::f') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2039 .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
|
2040 .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
|
2041 .prefixlen: 124 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2042 |
|
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 |
|
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
|
2045 # 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
|
2046 _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
|
2047 |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2048 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
|
2049 """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
|
2050 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2051 Args: |
|
77159
63d2c9affb11
#14814: Some PEP8 adjustments and dead code weeding
Hynek Schlawack <hs@ox.cx>
parents:
77117
diff
changeset
|
2052 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
|
2053 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
|
2054 '2001:db8::/128' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2055 '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
|
2056 '2001:db8::' |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2057 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
|
2058 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
|
2059 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
|
2060 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2061 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
|
2062 IPv6Network('2001:db8::') == |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2063 IPv6Network(42540766411282592856903984951653826560) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2064 or, more generally |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2065 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
|
2066 IPv6Network('2001:db8::') |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2067 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2068 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
|
2069 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
|
2070 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
|
2071 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2072 Raises: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2073 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
|
2074 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
|
2075 an IPv6 address. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2076 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
|
2077 supplied. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2078 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2079 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2080 _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
|
2081 _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
|
2082 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2083 # 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
|
2084 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
|
2085 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
|
2086 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
|
2087 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
|
2088 return |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2089 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2090 # 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
|
2091 if isinstance(address, bytes): |
|
77987
18c1d4d60bdf
Issue 14814: Further error case testing coverage and cleanups
Nick Coghlan <ncoghlan@gmail.com>
parents:
77971
diff
changeset
|
2092 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
|
2093 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
|
2094 self.netmask = IPv6Address(self._ALL_ONES) |
|
77383
4aeb5b9b62d7
#14814: Remove redundant code from ipaddress.IPv6Network
Hynek Schlawack <hs@ox.cx>
parents:
77360
diff
changeset
|
2095 return |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2096 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2097 # 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
|
2098 # 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
|
2099 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
|
2100 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2101 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
|
2102 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2103 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
|
2104 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
|
2105 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
|
2106 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
|
2107 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
|
2108 % addr[1]) |
|
77084
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2109 else: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2110 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
|
2111 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2112 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
|
2113 if strict: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2114 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
|
2115 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
|
2116 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
|
2117 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
|
2118 int(self.netmask)) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2119 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2120 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
|
2121 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
|
2122 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2123 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
|
2124 """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
|
2125 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2126 Args: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2127 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
|
2128 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2129 Returns: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2130 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
|
2131 netmask. |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2132 |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2133 """ |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2134 try: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2135 prefixlen = int(prefixlen) |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2136 except ValueError: |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2137 return False |
|
82a649047272
Issue #14814: addition of the ipaddress module (stage 1 - code and tests)
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2138 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
|
2139 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2140 @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
|
2141 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
|
2142 """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
|
2143 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2144 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
|
2145 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
|
2146 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
|
2147 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2148 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
|
2149 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
|
2150 |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2151 """ |
|
b513ad06d1fa
Issue 14814: Docs work showed some more cases of networks pretending to be addresses and highlighted the weird approach to implementing the 'is_whatever' properties. Impl now illustrates far more clearly that networks have a property if both their network and broadcast addresses have that property
Nick Coghlan <ncoghlan@gmail.com>
parents:
78426
diff
changeset
|
2152 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
|
2153 self.broadcast_address.is_site_local) |
