@@ -532,6 +532,30 @@ def _prefix_from_ip_string(cls, ip_str):
532
532
except ValueError :
533
533
cls ._report_invalid_netmask (ip_str )
534
534
535
+ @classmethod
536
+ def _split_addr_prefix (cls , address ):
537
+ """Helper function to parse address of Network/Interface.
538
+
539
+ Arg:
540
+ address: Argument of Network/Interface.
541
+
542
+ Returns:
543
+ (addr, prefix) tuple.
544
+ """
545
+ # a packed address or integer
546
+ if isinstance (address , (bytes , int )):
547
+ return address , cls ._max_prefixlen
548
+
549
+ if not isinstance (address , tuple ):
550
+ # Assume input argument to be string or any object representation
551
+ # which converts into a formatted IP prefix string.
552
+ address = _split_optional_netmask (address )
553
+
554
+ # Constructing from a tuple (addr, [mask])
555
+ if len (address ) > 1 :
556
+ return address
557
+ return address [0 ], cls ._max_prefixlen
558
+
535
559
def __reduce__ (self ):
536
560
return self .__class__ , (str (self ),)
537
561
@@ -1381,32 +1405,13 @@ def is_link_local(self):
1381
1405
class IPv4Interface (IPv4Address ):
1382
1406
1383
1407
def __init__ (self , address ):
1384
- if isinstance (address , (bytes , int )):
1385
- IPv4Address .__init__ (self , address )
1386
- self .network = IPv4Network (self ._ip )
1387
- self ._prefixlen = self ._max_prefixlen
1388
- return
1389
-
1390
- if isinstance (address , tuple ):
1391
- IPv4Address .__init__ (self , address [0 ])
1392
- if len (address ) > 1 :
1393
- self ._prefixlen = int (address [1 ])
1394
- else :
1395
- self ._prefixlen = self ._max_prefixlen
1396
-
1397
- self .network = IPv4Network (address , strict = False )
1398
- self .netmask = self .network .netmask
1399
- self .hostmask = self .network .hostmask
1400
- return
1401
-
1402
- addr = _split_optional_netmask (address )
1403
- IPv4Address .__init__ (self , addr [0 ])
1404
-
1405
- self .network = IPv4Network (address , strict = False )
1406
- self ._prefixlen = self .network ._prefixlen
1408
+ addr , mask = self ._split_addr_prefix (address )
1407
1409
1410
+ IPv4Address .__init__ (self , addr )
1411
+ self .network = IPv4Network ((addr , mask ), strict = False )
1408
1412
self .netmask = self .network .netmask
1409
1413
self .hostmask = self .network .hostmask
1414
+ self ._prefixlen = self .network ._prefixlen
1410
1415
1411
1416
def __str__ (self ):
1412
1417
return '%s/%d' % (self ._string_from_ip_int (self ._ip ),
@@ -1511,24 +1516,9 @@ def __init__(self, address, strict=True):
1511
1516
an IPv4 address.
1512
1517
ValueError: If strict is True and a network address is not
1513
1518
supplied.
1514
-
1515
1519
"""
1516
1520
_BaseNetwork .__init__ (self , address )
1517
-
1518
- # Constructing from a packed address or integer
1519
- if isinstance (address , (int , bytes )):
1520
- addr = address
1521
- mask = self ._max_prefixlen
1522
- # Constructing from a tuple (addr, [mask])
1523
- elif isinstance (address , tuple ):
1524
- addr = address [0 ]
1525
- mask = address [1 ] if len (address ) > 1 else self ._max_prefixlen
1526
- # Assume input argument to be string or any object representation
1527
- # which converts into a formatted IP prefix string.
1528
- else :
1529
- args = _split_optional_netmask (address )
1530
- addr = self ._ip_int_from_string (args [0 ])
1531
- mask = args [1 ] if len (args ) == 2 else self ._max_prefixlen
1521
+ addr , mask = self ._split_addr_prefix (address )
1532
1522
1533
1523
self .network_address = IPv4Address (addr )
1534
1524
self .netmask , self ._prefixlen = self ._make_netmask (mask )
@@ -2061,28 +2051,13 @@ def sixtofour(self):
2061
2051
class IPv6Interface (IPv6Address ):
2062
2052
2063
2053
def __init__ (self , address ):
2064
- if isinstance (address , (bytes , int )):
2065
- IPv6Address .__init__ (self , address )
2066
- self .network = IPv6Network (self ._ip )
2067
- self ._prefixlen = self ._max_prefixlen
2068
- return
2069
- if isinstance (address , tuple ):
2070
- IPv6Address .__init__ (self , address [0 ])
2071
- if len (address ) > 1 :
2072
- self ._prefixlen = int (address [1 ])
2073
- else :
2074
- self ._prefixlen = self ._max_prefixlen
2075
- self .network = IPv6Network (address , strict = False )
2076
- self .netmask = self .network .netmask
2077
- self .hostmask = self .network .hostmask
2078
- return
2054
+ addr , mask = self ._split_addr_prefix (address )
2079
2055
2080
- addr = _split_optional_netmask (address )
2081
- IPv6Address .__init__ (self , addr [0 ])
2082
- self .network = IPv6Network (address , strict = False )
2056
+ IPv6Address .__init__ (self , addr )
2057
+ self .network = IPv6Network ((addr , mask ), strict = False )
2083
2058
self .netmask = self .network .netmask
2084
- self ._prefixlen = self .network ._prefixlen
2085
2059
self .hostmask = self .network .hostmask
2060
+ self ._prefixlen = self .network ._prefixlen
2086
2061
2087
2062
def __str__ (self ):
2088
2063
return '%s/%d' % (self ._string_from_ip_int (self ._ip ),
@@ -2191,24 +2166,9 @@ def __init__(self, address, strict=True):
2191
2166
an IPv6 address.
2192
2167
ValueError: If strict was True and a network address was not
2193
2168
supplied.
2194
-
2195
2169
"""
2196
2170
_BaseNetwork .__init__ (self , address )
2197
-
2198
- # Constructing from a packed address or integer
2199
- if isinstance (address , (int , bytes )):
2200
- addr = address
2201
- mask = self ._max_prefixlen
2202
- # Constructing from a tuple (addr, [mask])
2203
- elif isinstance (address , tuple ):
2204
- addr = address [0 ]
2205
- mask = address [1 ] if len (address ) > 1 else self ._max_prefixlen
2206
- # Assume input argument to be string or any object representation
2207
- # which converts into a formatted IP prefix string.
2208
- else :
2209
- args = _split_optional_netmask (address )
2210
- addr = self ._ip_int_from_string (args [0 ])
2211
- mask = args [1 ] if len (args ) == 2 else self ._max_prefixlen
2171
+ addr , mask = self ._split_addr_prefix (address )
2212
2172
2213
2173
self .network_address = IPv6Address (addr )
2214
2174
self .netmask , self ._prefixlen = self ._make_netmask (mask )
0 commit comments