Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1030)

Side by Side Diff: Lib/ipaddress.py

Issue 14814: Implement PEP 3144 (the ipaddress module)
Patch Set: Created 11 months, 3 weeks ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2007 Google Inc. 1 # Copyright 2007 Google Inc.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """A fast, lightweight IPv4/IPv6 manipulation library in Python. 4 """A fast, lightweight IPv4/IPv6 manipulation library in Python.
5 5
6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses 6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses
7 and networks. 7 and networks.
8 8
9 """ 9 """
10 10
(...skipping 1972 matching lines...) Expand 10 before | Expand all | Expand 10 after
1983 _BaseV6.__init__(self, address) 1983 _BaseV6.__init__(self, address)
1984 _BaseNetwork.__init__(self, address) 1984 _BaseNetwork.__init__(self, address)
1985 1985
1986 # Efficient constructor from integer. 1986 # Efficient constructor from integer.
1987 if isinstance(address, int): 1987 if isinstance(address, int):
1988 if address < 0 or address > self._ALL_ONES: 1988 if address < 0 or address > self._ALL_ONES:
1989 raise AddressValueError(address) 1989 raise AddressValueError(address)
1990 self.network_address = IPv6Address(address) 1990 self.network_address = IPv6Address(address)
1991 self._prefixlen = self._max_prefixlen 1991 self._prefixlen = self._max_prefixlen
1992 self.netmask = IPv6Address(self._ALL_ONES) 1992 self.netmask = IPv6Address(self._ALL_ONES)
1993 if strict:
1994 if (IPv6Address(int(self.network_address) &
1995 int(self.netmask)) != self.network_address):
1996 raise ValueError('%s has host bits set' % str(self))
1997 self.network_address = IPv6Address(int(self.network_address) & 1993 self.network_address = IPv6Address(int(self.network_address) &
1998 int(self.netmask)) 1994 int(self.netmask))
Nick Coghlan 2012/06/08 14:50:18 The second assignment of network_address can go aw
1999 return 1995 return
2000 1996
2001 # Constructing from a packed address 1997 # Constructing from a packed address
2002 if isinstance(address, bytes) and len(address) == 16: 1998 if isinstance(address, bytes) and len(address) == 16:
2003 tmp = struct.unpack('!QQ', address) 1999 tmp = struct.unpack('!QQ', address)
2004 self.network_address = IPv6Address((tmp[0] << 64) | tmp[1]) 2000 self.network_address = IPv6Address((tmp[0] << 64) | tmp[1])
2005 self._prefixlen = self._max_prefixlen 2001 self._prefixlen = self._max_prefixlen
2006 self.netmask = IPv6Address(self._ALL_ONES) 2002 self.netmask = IPv6Address(self._ALL_ONES)
2007 if strict: 2003 self.network_address = IPv6Address(int(self.network_address) &
Nick Coghlan 2012/06/08 14:50:18 As above - with the netmask set to all ones, no ne
2008 if (IPv6Address(int(self.network_address) & 2004 int(self.netmask))
2009 int(self.netmask)) != self.network_address): 2005 return
2010 raise ValueError('%s has host bits set' % str(self))
2011 self.network_address = IPv6Address(int(self.network_address) &
2012 int(self.netmask))
2013 return
2014 2006
2015 # Assume input argument to be string or any object representation 2007 # Assume input argument to be string or any object representation
2016 # which converts into a formatted IP prefix string. 2008 # which converts into a formatted IP prefix string.
2017 addr = str(address).split('/') 2009 addr = str(address).split('/')
2018 2010
2019 if len(addr) > 2: 2011 if len(addr) > 2:
2020 raise AddressValueError(address) 2012 raise AddressValueError(address)
2021 2013
2022 self.network_address = IPv6Address(self._ip_int_from_string(addr[0])) 2014 self.network_address = IPv6Address(self._ip_int_from_string(addr[0]))
2023 2015
(...skipping 25 matching lines...) Expand all
2049 Returns: 2041 Returns:
2050 A boolean, True if the prefix represents a valid IPv6 2042 A boolean, True if the prefix represents a valid IPv6
2051 netmask. 2043 netmask.
2052 2044
2053 """ 2045 """
2054 try: 2046 try:
2055 prefixlen = int(prefixlen) 2047 prefixlen = int(prefixlen)
2056 except ValueError: 2048 except ValueError:
2057 return False 2049 return False
2058 return 0 <= prefixlen <= self._max_prefixlen 2050 return 0 <= prefixlen <= self._max_prefixlen
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7