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
3e2b29dc
Commit
3e2b29dc
authored
Sep 06, 2018
by
Tal Einat
Committed by
GitHub
Sep 06, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-30977: make uuid.UUID use __slots__ (GH-9078)
Co-Authored-By: Wouter Bolsterlee.
parent
874809ea
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
2 deletions
+88
-2
Lib/test/test_uuid.py
Lib/test/test_uuid.py
+60
-0
Lib/uuid.py
Lib/uuid.py
+26
-2
Misc/NEWS.d/next/Library/2018-09-06-10-07-46.bpo-30977.bP661V.rst
...S.d/next/Library/2018-09-06-10-07-46.bpo-30977.bP661V.rst
+2
-0
No files found.
Lib/test/test_uuid.py
View file @
3e2b29dc
...
...
@@ -4,8 +4,10 @@ import builtins
import
contextlib
import
io
import
os
import
pickle
import
shutil
import
subprocess
import
sys
py_uuid
=
support
.
import_fresh_module
(
'uuid'
,
blocked
=
[
'_uuid'
])
c_uuid
=
support
.
import_fresh_module
(
'uuid'
,
fresh
=
[
'_uuid'
])
...
...
@@ -311,6 +313,64 @@ class BaseTestUUID:
node2
=
self
.
uuid
.
getnode
()
self
.
assertEqual
(
node1
,
node2
,
'%012x != %012x'
%
(
node1
,
node2
))
def
_setup_for_pickle
(
self
):
orig_uuid
=
sys
.
modules
.
get
(
'uuid'
)
sys
.
modules
[
'uuid'
]
=
self
.
uuid
def
restore_uuid_module
():
if
orig_uuid
is
not
None
:
sys
.
modules
[
'uuid'
]
=
orig_uuid
else
:
del
sys
.
modules
[
'uuid'
]
self
.
addCleanup
(
restore_uuid_module
)
def
test_pickle_roundtrip
(
self
):
self
.
_setup_for_pickle
()
u
=
self
.
uuid
.
UUID
(
'12345678123456781234567812345678'
)
self
.
assertEqual
(
u
,
pickle
.
loads
(
pickle
.
dumps
(
u
)))
def
test_unpickle_previous_python_versions
(
self
):
self
.
_setup_for_pickle
()
u
=
self
.
uuid
.
UUID
(
'12345678123456781234567812345678'
)
# Python 2.7 protocol 0-2 pickles of u
py27_pickles
=
[
b'ccopy_reg
\
n
_reconstructor
\
n
p0
\
n
(cuuid
\
n
UUID
\
n
p1
\
n
c__builtin__
\
n
ob'
b'ject
\
n
p2
\
n
Ntp3
\
n
Rp4
\
n
(dp5
\
n
S
\
'
int
\
'
\
n
p6
\
n
L24197857161011715162171'
b'839636988778104L
\
n
sb.'
,
b'ccopy_reg
\
n
_reconstructor
\
n
q
\
x00
(cuuid
\
n
UUID
\
n
q
\
x01
c__builtin__
\
n
'
b'object
\
n
q
\
x02
Ntq
\
x03
Rq
\
x04
}q
\
x05
U
\
x03
intq
\
x06
L2419785716101171516'
b'2171839636988778104L
\
n
sb.'
,
b'
\
x80
\
x02
cuuid
\
n
UUID
\
n
q
\
x00
)
\
x81
q
\
x01
}q
\
x02
U
\
x03
intq
\
x03
\
x8a
\
x10
xV'
b'4
\
x12
xV4
\
x12
xV4
\
x12
xV4
\
x12
sb.'
,
]
# Python 3.6 protocol 0-4 pickles of u
py36_pickles
=
[
b'ccopy_reg
\
n
_reconstructor
\
n
p0
\
n
(cuuid
\
n
UUID
\
n
p1
\
n
c__builtin__
\
n
ob'
b'ject
\
n
p2
\
n
Ntp3
\
n
Rp4
\
n
(dp5
\
n
Vint
\
n
p6
\
n
L241978571610117151621718396'
b'36988778104L
\
n
sb.'
,
b'ccopy_reg
\
n
_reconstructor
\
n
q
\
x00
(cuuid
\
n
UUID
\
n
q
\
x01
c__builtin__
\
n
'
b'object
\
n
q
\
x02
Ntq
\
x03
Rq
\
x04
}q
\
x05
X
\
x03
\
x00
\
x00
\
x00
intq
\
x06
L2419785'
b'7161011715162171839636988778104L
\
n
sb.'
,
b'
\
x80
\
x02
cuuid
\
n
UUID
\
n
q
\
x00
)
\
x81
q
\
x01
}q
\
x02
X
\
x03
\
x00
\
x00
\
x00
intq'
b'
\
x03
\
x8a
\
x10
xV4
\
x12
xV4
\
x12
xV4
\
x12
xV4
\
x12
sb.'
,
b'
\
x80
\
x03
cuuid
\
n
UUID
\
n
q
\
x00
)
\
x81
q
\
x01
}q
\
x02
X
\
x03
\
x00
\
x00
\
x00
intq'
b'
\
x03
\
x8a
\
x10
xV4
\
x12
xV4
\
x12
xV4
\
x12
xV4
\
x12
sb.'
,
b'
\
x80
\
x04
\
x95
0
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x8c
\
x04
uuid
\
x94
\
x8c
\
x04
'
b'UUID
\
x94
\
x93
\
x94
)
\
x81
\
x94
}
\
x94
\
x8c
\
x03
int
\
x94
\
x8a
\
x10
xV4
\
x12
xV4'
b'
\
x12
xV4
\
x12
xV4
\
x12
sb.'
,
]
for
pickled
in
py27_pickles
+
py36_pickles
:
unpickled
=
pickle
.
loads
(
pickled
)
self
.
assertEqual
(
unpickled
,
u
)
# is_safe was added in 3.7. When unpickling values from older
# versions, is_safe will be missing, so it should be set to
# SafeUUID.unknown.
self
.
assertEqual
(
unpickled
.
is_safe
,
self
.
uuid
.
SafeUUID
.
unknown
)
# bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
# need not necessarily be 48 bits (e.g., EUI-64).
def
test_uuid1_eui64
(
self
):
...
...
Lib/uuid.py
View file @
3e2b29dc
...
...
@@ -118,6 +118,8 @@ class UUID:
uuid_generate_time_safe(3).
"""
__slots__
=
(
'int'
,
'is_safe'
)
def
__init__
(
self
,
hex
=
None
,
bytes
=
None
,
bytes_le
=
None
,
fields
=
None
,
int
=
None
,
version
=
None
,
*
,
is_safe
=
SafeUUID
.
unknown
):
...
...
@@ -201,8 +203,30 @@ class UUID:
# Set the version number.
int
&=
~
(
0xf000
<<
64
)
int
|=
version
<<
76
self
.
__dict__
[
'int'
]
=
int
self
.
__dict__
[
'is_safe'
]
=
is_safe
object
.
__setattr__
(
self
,
'int'
,
int
)
object
.
__setattr__
(
self
,
'is_safe'
,
is_safe
)
def
__getstate__
(
self
):
d
=
{
attr
:
getattr
(
self
,
attr
)
for
attr
in
self
.
__slots__
}
# is_safe is a SafeUUID instance. Return just its value, so that
# it can be unpickled in older Python versions without SafeUUID.
d
[
'is_safe'
]
=
d
[
'is_safe'
].
value
return
d
def
__setstate__
(
self
,
state
):
# is_safe was added in 3.7
state
.
setdefault
(
'is_safe'
,
SafeUUID
.
unknown
.
value
)
for
attr
in
self
.
__slots__
:
value
=
state
[
attr
]
# for is_safe, restore the SafeUUID from the stored value
if
attr
==
'is_safe'
:
try
:
value
=
SafeUUID
(
value
)
except
ValueError
:
value
=
SafeUUID
.
unknown
object
.
__setattr__
(
self
,
attr
,
value
)
def
__eq__
(
self
,
other
):
if
isinstance
(
other
,
UUID
):
...
...
Misc/NEWS.d/next/Library/2018-09-06-10-07-46.bpo-30977.bP661V.rst
0 → 100644
View file @
3e2b29dc
Make uuid.UUID use ``__slots__`` to reduce its memory footprint. Based on
original patch by Wouter Bolsterlee.
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