-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-11572: Make several minor improvements to copy module #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,24 +75,20 @@ def copy(x): | |
if copier: | ||
return copier(x) | ||
|
||
try: | ||
issc = issubclass(cls, type) | ||
except TypeError: # cls is not a class | ||
issc = False | ||
if issc: | ||
if issubclass(cls, type): | ||
# treat it as a regular class: | ||
return _copy_immutable(x) | ||
|
||
copier = getattr(cls, "__copy__", None) | ||
if copier: | ||
if copier is not None: | ||
return copier(x) | ||
|
||
reductor = dispatch_table.get(cls) | ||
if reductor: | ||
if reductor is not None: | ||
rv = reductor(x) | ||
else: | ||
reductor = getattr(x, "__reduce_ex__", None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing this line to
The test was added since the initial check-in of the module, so perhaps we can this leave as-is and add a note describing the behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets defer this change to a separate issue. It changes the behavior for the case when |
||
if reductor: | ||
if reductor is not None: | ||
rv = reductor(4) | ||
else: | ||
reductor = getattr(x, "__reduce__", None) | ||
|
@@ -146,26 +142,22 @@ def deepcopy(x, memo=None, _nil=[]): | |
cls = type(x) | ||
|
||
copier = _deepcopy_dispatch.get(cls) | ||
if copier: | ||
if copier is not None: | ||
y = copier(x, memo) | ||
else: | ||
try: | ||
issc = issubclass(cls, type) | ||
except TypeError: # cls is not a class (old Boost; see SF #502085) | ||
issc = 0 | ||
if issc: | ||
if issubclass(cls, type): | ||
y = _deepcopy_atomic(x, memo) | ||
else: | ||
copier = getattr(x, "__deepcopy__", None) | ||
if copier: | ||
if copier is not None: | ||
y = copier(memo) | ||
else: | ||
reductor = dispatch_table.get(cls) | ||
if reductor: | ||
rv = reductor(x) | ||
else: | ||
reductor = getattr(x, "__reduce_ex__", None) | ||
if reductor: | ||
if reductor is not None: | ||
rv = reductor(4) | ||
else: | ||
reductor = getattr(x, "__reduce__", None) | ||
|
@@ -198,10 +190,7 @@ def _deepcopy_atomic(x, memo): | |
d[complex] = _deepcopy_atomic | ||
d[bytes] = _deepcopy_atomic | ||
d[str] = _deepcopy_atomic | ||
try: | ||
d[types.CodeType] = _deepcopy_atomic | ||
except AttributeError: | ||
pass | ||
d[types.CodeType] = _deepcopy_atomic | ||
d[type] = _deepcopy_atomic | ||
d[types.BuiltinFunctionType] = _deepcopy_atomic | ||
d[types.FunctionType] = _deepcopy_atomic | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -510,11 +510,7 @@ def save(self, obj, save_persistent_id=True): | |
rv = reduce(obj) | ||
else: | ||
# Check for a class with a custom metaclass; treat as regular class | ||
try: | ||
issc = issubclass(t, type) | ||
except TypeError: # t is not a class (old Boost; see SF #502085) | ||
issc = False | ||
if issc: | ||
if issubclass(t, type): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is similar code in Python implementation of pickle. It should be synchronized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean another module or is there another Python implementation of the pickle module? (Or GitHub shows your comment in a wrong place?) I've changed both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems I thought that we still in |
||
self.save_global(obj) | ||
return | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know that
if copier is not None
is slower thanif copier
?But the change LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know that. How much slower than
if foo
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too much. But I think that having only None special is better that depending on the boolean value of the method. LGTM.