Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b83819f2
Commit
b83819f2
authored
May 02, 2009
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make py3k compat code explicitly on
parent
6bf1900e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
59 deletions
+37
-59
Lib/ipaddr.py
Lib/ipaddr.py
+11
-26
Lib/test/test_ipaddr.py
Lib/test/test_ipaddr.py
+26
-33
No files found.
Lib/ipaddr.py
View file @
b83819f2
...
...
@@ -193,17 +193,6 @@ def collapse_address_list(addresses):
sorted
(
addresses
,
key
=
BaseIP
.
_get_networks_key
))
# Test whether this Python implementation supports byte objects that
# are not identical to str ones.
# We need to exclude platforms where bytes == str so that we can
# distinguish between packed representations and strings, for example
# b'12::' (the IPv4 address 49.50.58.58) and '12::' (an IPv6 address).
try
:
_compat_has_real_bytes
=
bytes
!=
str
except
NameError
:
# <Python2.6
_compat_has_real_bytes
=
False
class
BaseIP
(
object
):
"""A generic IP object.
...
...
@@ -591,13 +580,11 @@ class IPv4(BaseIP):
raise
IPv4IpValidationError
(
ipaddr
)
return
# Constructing from a packed address
if
_compat_has_real_bytes
:
if
isinstance
(
ipaddr
,
bytes
)
and
len
(
ipaddr
)
==
4
:
self
.
ip
=
struct
.
unpack
(
'!I'
,
ipaddr
)[
0
]
self
.
_prefixlen
=
32
self
.
netmask
=
self
.
_ALL_ONES
return
if
isinstance
(
ipaddr
,
bytes
)
and
len
(
ipaddr
)
==
4
:
self
.
ip
=
struct
.
unpack
(
'!I'
,
ipaddr
)[
0
]
self
.
_prefixlen
=
32
self
.
netmask
=
self
.
_ALL_ONES
return
# Assume input argument to be string or any object representation
# which converts into a formatted IP prefix string.
...
...
@@ -930,14 +917,12 @@ class IPv6(BaseIP):
raise
IPv6IpValidationError
(
ipaddr
)
return
# Constructing from a packed address
if
_compat_has_real_bytes
:
if
isinstance
(
ipaddr
,
bytes
)
and
len
(
ipaddr
)
==
16
:
tmp
=
struct
.
unpack
(
'!QQ'
,
ipaddr
)
self
.
ip
=
(
tmp
[
0
]
<<
64
)
|
tmp
[
1
]
self
.
_prefixlen
=
128
self
.
netmask
=
self
.
_ALL_ONES
return
if
isinstance
(
ipaddr
,
bytes
)
and
len
(
ipaddr
)
==
16
:
tmp
=
struct
.
unpack
(
'!QQ'
,
ipaddr
)
self
.
ip
=
(
tmp
[
0
]
<<
64
)
|
tmp
[
1
]
self
.
_prefixlen
=
128
self
.
netmask
=
self
.
_ALL_ONES
return
# Assume input argument to be string or any object representation
# which converts into a formatted IP prefix string.
...
...
Lib/test/test_ipaddr.py
View file @
b83819f2
...
...
@@ -22,12 +22,6 @@ import unittest
import
ipaddr
# Compatibility function to cast str to bytes objects
if
ipaddr
.
_compat_has_real_bytes
:
_cb
=
lambda
bytestr
:
bytes
(
bytestr
,
'charmap'
)
else
:
_cb
=
str
class
IpaddrUnitTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
@@ -99,26 +93,25 @@ class IpaddrUnitTest(unittest.TestCase):
self
.
assertEqual
(
ipaddr
.
IP
(
self
.
ipv4
.
ip
).
version
,
4
)
self
.
assertEqual
(
ipaddr
.
IP
(
self
.
ipv6
.
ip
).
version
,
6
)
if
ipaddr
.
_compat_has_real_bytes
:
# on python3+
def
testIpFromPacked
(
self
):
ip
=
ipaddr
.
IP
self
.
assertEqual
(
self
.
ipv4
.
ip
,
ip
(
_cb
(
'
\
x01
\
x02
\
x03
\
x04
'
)).
ip
)
self
.
assertEqual
(
ip
(
'255.254.253.252'
),
ip
(
_cb
(
'
\
xff
\
xfe
\
xfd
\
xfc
'
)))
self
.
assertRaises
(
ValueError
,
ipaddr
.
IP
,
_cb
(
'
\
x00
'
*
3
))
self
.
assertRaises
(
ValueError
,
ipaddr
.
IP
,
_cb
(
'
\
x00
'
*
5
))
self
.
assertEqual
(
self
.
ipv6
.
ip
,
ip
(
_cb
(
'
\
x20
\
x01
\
x06
\
x58
\
x02
\
x2a
\
xca
\
xfe
'
'
\
x02
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x01
'
)).
ip
)
self
.
assertEqual
(
ip
(
'ffff:2:3:4:ffff::'
),
ip
(
_cb
(
'
\
xff
\
xff
\
x00
\
x02
\
x00
\
x03
\
x00
\
x04
'
+
'
\
xff
\
xff
'
+
'
\
x00
'
*
6
)))
self
.
assertEqual
(
ip
(
'::'
),
ip
(
_cb
(
'
\
x00
'
*
16
)))
self
.
assertRaises
(
ValueError
,
ip
,
_cb
(
'
\
x00
'
*
15
))
self
.
assertRaises
(
ValueError
,
ip
,
_cb
(
'
\
x00
'
*
17
))
def
testIpFromPacked
(
self
):
ip
=
ipaddr
.
IP
self
.
assertEqual
(
self
.
ipv4
.
ip
,
ip
(
b'
\
x01
\
x02
\
x03
\
x04
'
).
ip
)
self
.
assertEqual
(
ip
(
'255.254.253.252'
),
ip
(
b'
\
xff
\
xfe
\
xfd
\
xfc
'
))
self
.
assertRaises
(
ValueError
,
ipaddr
.
IP
,
b'
\
x00
'
*
3
)
self
.
assertRaises
(
ValueError
,
ipaddr
.
IP
,
b'
\
x00
'
*
5
)
self
.
assertEqual
(
self
.
ipv6
.
ip
,
ip
(
b'
\
x20
\
x01
\
x06
\
x58
\
x02
\
x2a
\
xca
\
xfe
'
b'
\
x02
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x01
'
).
ip
)
self
.
assertEqual
(
ip
(
'ffff:2:3:4:ffff::'
),
ip
(
b'
\
xff
\
xff
\
x00
\
x02
\
x00
\
x03
\
x00
\
x04
'
+
b'
\
xff
\
xff
'
+
b'
\
x00
'
*
6
))
self
.
assertEqual
(
ip
(
'::'
),
ip
(
b'
\
x00
'
*
16
))
self
.
assertRaises
(
ValueError
,
ip
,
b'
\
x00
'
*
15
)
self
.
assertRaises
(
ValueError
,
ip
,
b'
\
x00
'
*
17
)
def
testGetIp
(
self
):
self
.
assertEqual
(
self
.
ipv4
.
ip
,
16909060
)
...
...
@@ -404,17 +397,17 @@ class IpaddrUnitTest(unittest.TestCase):
def
testPacked
(
self
):
self
.
assertEqual
(
self
.
ipv4
.
packed
,
_cb
(
'
\
x01
\
x02
\
x03
\
x04
'
)
)
b'
\
x01
\
x02
\
x03
\
x04
'
)
self
.
assertEqual
(
ipaddr
.
IPv4
(
'255.254.253.252'
).
packed
,
_cb
(
'
\
xff
\
xfe
\
xfd
\
xfc
'
)
)
b'
\
xff
\
xfe
\
xfd
\
xfc
'
)
self
.
assertEqual
(
self
.
ipv6
.
packed
,
_cb
(
'
\
x20
\
x01
\
x06
\
x58
\
x02
\
x2a
\
xca
\
xfe
'
'
\
x02
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x01
'
)
)
b'
\
x20
\
x01
\
x06
\
x58
\
x02
\
x2a
\
xca
\
xfe
'
+
b'
\
x02
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x01
'
)
self
.
assertEqual
(
ipaddr
.
IPv6
(
'ffff:2:3:4:ffff::'
).
packed
,
_cb
(
'
\
xff
\
xff
\
x00
\
x02
\
x00
\
x03
\
x00
\
x04
\
xff
\
xff
'
+
'
\
x00
'
*
6
)
)
b
'
\
xff
\
xff
\
x00
\
x02
\
x00
\
x03
\
x00
\
x04
\
xff
\
xff
'
+
b'
\
x00
'
*
6
)
self
.
assertEqual
(
ipaddr
.
IPv6
(
'::1:0:0:0:0'
).
packed
,
_cb
(
'
\
x00
'
*
6
+
'
\
x00
\
x01
'
+
'
\
x00
'
*
8
)
)
b'
\
x00
'
*
6
+
b'
\
x00
\
x01
'
+
b'
\
x00
'
*
8
)
def
testIpStrFromPrefixlen
(
self
):
ipv4
=
ipaddr
.
IPv4
(
'1.2.3.4/24'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment