Skip to content

gh-95081: Allow shallow and deep copying of memoryview object#95082

Closed
corona10 wants to merge 9 commits into
python:mainfrom
corona10:gh-95081
Closed

gh-95081: Allow shallow and deep copying of memoryview object#95082
corona10 wants to merge 9 commits into
python:mainfrom
corona10:gh-95081

Conversation

@corona10
Copy link
Copy Markdown
Member

@corona10 corona10 commented Jul 21, 2022

@corona10
Copy link
Copy Markdown
Member Author

Need to discuss before reviewing this PR.

Comment thread Lib/test/test_memoryview.py Outdated
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f81be8a

Even if property object is allowed for copy operation, property object does not allow to be pickled.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> 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>

@corona10 corona10 removed needs backport to 3.10 only security fixes needs backport to 3.11 only security fixes labels Jul 21, 2022
@corona10
Copy link
Copy Markdown
Member Author

corona10 commented Jul 21, 2022

I am not sure to add backport patch even if this operation is allowed. cc @serhiy-storchaka

Comment thread Lib/copy.py Outdated
@corona10 corona10 changed the title gh-95081: Allow shallow and deep copying of memoryview object [WIP] gh-95081: Allow shallow and deep copying of memoryview object Jul 21, 2022
@corona10 corona10 changed the title [WIP] gh-95081: Allow shallow and deep copying of memoryview object gh-95081: Allow shallow and deep copying of memoryview object Jul 21, 2022
@corona10
Copy link
Copy Markdown
Member Author

@serhiy-storchaka gentle ping :)

Comment thread Lib/copy.py
d[types.MethodType] = _deepcopy_method

def _deepcopy_memoryview(x, memo, deepcopy=deepcopy):
y = memoryview(deepcopy(x.obj, memo))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can fail in several ways:

  • Doesn't preserver readonly state. For example, copying memoryview(bytearray(b"hi")).toreadonly() will create an MV that's not readonly. This is fixable though.
  • Memoryviews created through PyMemoryView_FromBuffer or PyMemoryView_FromMemory may not have an underlying obj, and cannot be constructed directly from Python, so this will not copy them faithfully. I don't see a way to fix this.

@corona10 corona10 closed this Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants