Skip to content
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

error when deep copying module is confusing #62395

Closed
mrjbq7 mannequin opened this issue Jun 11, 2013 · 4 comments
Closed

error when deep copying module is confusing #62395

mrjbq7 mannequin opened this issue Jun 11, 2013 · 4 comments
Labels
type-feature A feature request or enhancement

Comments

@mrjbq7
Copy link
Mannequin

mrjbq7 mannequin commented Jun 11, 2013

BPO 18195
Nosy @mrjbq7, @bitdancer, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-02-19.20:55:25.541>
created_at = <Date 2013-06-11.22:45:32.576>
labels = ['type-feature']
title = 'error when deep copying module is confusing'
updated_at = <Date 2017-02-19.20:55:25.540>
user = 'https://github.com/mrjbq7'

bugs.python.org fields:

activity = <Date 2017-02-19.20:55:25.540>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2017-02-19.20:55:25.541>
closer = 'serhiy.storchaka'
components = []
creation = <Date 2013-06-11.22:45:32.576>
creator = 'mrjbq7'
dependencies = []
files = []
hgrepos = []
issue_num = 18195
keywords = []
message_count = 4.0
messages = ['190996', '191002', '261006', '288164']
nosy_count = 3.0
nosy_names = ['mrjbq7', 'r.david.murray', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue18195'
versions = []

@mrjbq7
Copy link
Mannequin Author

mrjbq7 mannequin commented Jun 11, 2013

If you have a simple module (say "foo.py"):

$ cat foo.py
bar = 1

You get weird errors when trying to deep copy them (which I did by accident, not intentionally trying to deep copy modules):

Python 2.7.2:

>>> import foo
>>> import copy
>>> copy.deepcopy(foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 329, in _reconstruct
    y = callable(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 93, in __newobj__
    return cls.__new__(cls, *args)
TypeError: object.__new__(NotImplementedType) is not safe, use NotImplementedType.__new__()

Python 3.3.2:

>>> import foo
>>> import copy
>>> copy.deepcopy(foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/copy.py", line 174, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python3.3/copy.py", line 301, in _reconstruct
    y.__dict__.update(state)
AttributeError: 'NoneType' object has no attribute 'update'

I'm not expecting to be able to deep copy a module, but it would be really great if it is not possible for the error message to say something like "deepcopy doesn't work for modules" rather than two different funky tracebacks that don't really explain the problem...

Thanks,

@bitdancer
Copy link
Member

Well, we don't generally complicate the code to handle edge cases. That said, it might not be too complicated to add copy protocol methods to the module object which just raise a more useful error.

@serhiy-storchaka
Copy link
Member

Python 3.6:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/copy.py", line 97, in copy
    rv = reductor(4)
TypeError: can't pickle module objects

@serhiy-storchaka
Copy link
Member

This issue looks as enhancement rather than bug fix. Since it is fixed in 3.6 I'm closing it.

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Feb 19, 2017
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants