python / cpython

The Python programming language
https://www.python.org
Other
62.6k stars 30.03k forks source link

deepcopy memoryview raises TypeError #95081

Open stuaxo opened 2 years ago

stuaxo commented 2 years ago

Bug report

Calling deepcopy on a memoryview raises a TypeError, this looks similar to bug 82474 https://github.com/python/cpython/issues/82474

This also breaks BinaryField when using Django Polymorphic

https://github.com/django-polymorphic/django-polymorphic/issues/524 Your environment

>>> a = memoryview(b'123')
>>> from copy import deepcopy
>>> deepcopy(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle 'memoryview' object
corona10 commented 2 years ago

This issue looks like related to https://bugs.python.org/issue22995 and it was intended not to support deepcopy/copy. But I may need to understand the difference between https://github.com/python/cpython/issues/82474 case.

corona10 commented 2 years ago

@serhiy-storchaka Is there any reason to prohibit copy.deepcopy/copy operations for memoryview object while allowing property object?

>>> import pickle
>>> import copy
>>> a = property()
>>> pickle.dumps(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot pickle 'property' object
>>> copy.copy(a)
<property object at 0x101017230>
>>> copy.deepcopy(a)
<property object at 0x101017230>
serhiy-storchaka commented 2 years ago

There are no reasons to prohibit shallow and deep copying of memoryview objects (or I do not know them). And I have not found any mention of memoryview objects in https://bugs.python.org/issue22995. Could you explain what did you mean? Did you mean some other issue?

By default, shallow and deep copying use the same mechanism as pickling. If pickling works, copying works automatically, if pickling is impossible, copying does not work by default. But if it is possible to make a copy, it can be implemented explicitly using other mechanisms.

corona10 commented 2 years ago

There are no reasons to prohibit shallow and deep copying of memoryview objects (or I do not know them). And I have not found any mention of memoryview objects in https://bugs.python.org/issue22995. Could you explain what did you mean? Did you mean some other issue?

I found the related patch from that issue, Can you please take a look at this comment? https://github.com/python/cpython/pull/95082#discussion_r926287551

corona10 commented 2 years ago

By default, shallow and deep copying use the same mechanism as pickling. If pickling works, copying works automatically, if pickling is impossible, copying does not work by default. But if it is possible to make a copy, it can be implemented explicitly using other mechanisms.

Okay Thank you for explain :)