Unverified Kaydet (Commit) 54752533 authored tarafından Tal Einat's avatar Tal Einat Kaydeden (comit) GitHub

bpo-30977: rework code changes according to post-merge code review (GH-9106)

also mention the change and its consequences in What's New
üst 1f36bf60
...@@ -131,6 +131,10 @@ Optimizations ...@@ -131,6 +131,10 @@ Optimizations
objects (e.g. tuple, list, dict) size is reduced 4 or 8 bytes. objects (e.g. tuple, list, dict) size is reduced 4 or 8 bytes.
(Contributed by Inada Naoki in :issue:`33597`) (Contributed by Inada Naoki in :issue:`33597`)
* :class:`uuid.UUID` now uses ``__slots__`` to reduce its memory footprint.
Note that this means that instances can no longer be weak-referenced and
that arbitrary attributes can no longer be added to them.
Build and C API Changes Build and C API Changes
======================= =======================
...@@ -275,6 +279,9 @@ Changes in the Python API ...@@ -275,6 +279,9 @@ Changes in the Python API
* The function :func:`math.factorial` no longer accepts arguments that are not * The function :func:`math.factorial` no longer accepts arguments that are not
int-like. (Contributed by Pablo Galindo in :issue:`33083`.) int-like. (Contributed by Pablo Galindo in :issue:`33083`.)
* :class:`uuid.UUID` now uses ``__slots__``, therefore instances can no longer
be weak-referenced and attributes can no longer be added.
CPython bytecode changes CPython bytecode changes
------------------------ ------------------------
......
This diff is collapsed.
...@@ -207,26 +207,19 @@ class UUID: ...@@ -207,26 +207,19 @@ class UUID:
object.__setattr__(self, 'is_safe', is_safe) object.__setattr__(self, 'is_safe', is_safe)
def __getstate__(self): def __getstate__(self):
d = {attr: getattr(self, attr) for attr in self.__slots__} d = {'int': self.int}
if self.is_safe != SafeUUID.unknown:
# is_safe is a SafeUUID instance. Return just its value, so that # is_safe is a SafeUUID instance. Return just its value, so that
# it can be unpickled in older Python versions without SafeUUID. # it can be un-pickled in older Python versions without SafeUUID.
d['is_safe'] = d['is_safe'].value d['is_safe'] = self.is_safe.value
return d return d
def __setstate__(self, state): def __setstate__(self, state):
# is_safe was added in 3.7 object.__setattr__(self, 'int', state['int'])
state.setdefault('is_safe', SafeUUID.unknown.value) # is_safe was added in 3.7; it is also omitted when it is "unknown"
object.__setattr__(self, 'is_safe',
for attr in self.__slots__: SafeUUID(state['is_safe'])
value = state[attr] if 'is_safe' in state else SafeUUID.unknown)
# 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): def __eq__(self, other):
if isinstance(other, UUID): if isinstance(other, UUID):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment