This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: memoryview does not support subclassing
Type: enhancement Stage: resolved
Components: C API Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Allow objects implemented in pure Python to export PEP 3118 buffers
View: 13797
Assigned To: Nosy List: mdehoon, methane, skrah
Priority: normal Keywords:

Created on 2020-07-12 14:23 by mdehoon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg373554 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2020-07-12 14:23
Currently memoryview does not support subclassing:

>>> class B(memoryview): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'memoryview' is not an acceptable base type


Subclassing memoryview can be useful when
- class A supports the buffer protocol;
- class B wraps class A and should support the buffer protocol provided by class A;
- class A does not support subclassing.

In this situation,

class B(memoryview):
    def __new__(cls, a):
        return super(B, cls).__new__(cls, a)

where a is an instance of class A, would let instances of B support the buffer protocol provided by a.


Is there any particular reason why memoryview does not support subclassing?
msg373601 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-07-13 12:56
Would you be more specific about why you need that feature on the Python-ideas mailing list or "Ideas" category on discuss.python.org?
msg373668 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2020-07-15 06:39
Thank you, I have posted an explanation in the "Ideas" category of discuss.python.org.
msg373693 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2020-07-15 13:26
If I understand this correctly, I think this may be a duplicate of #13797.


Something like a __getbuffer__ protocol would be needed. I'm not
saying we *should* go that route, just stating the requirements
from my perspective.
msg373697 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2020-07-15 14:13
You are correct, this is indeed a duplicate of #13797. My apologies for not finding this issue before opening a new one. If there are no objections, I will close this issue as a duplicate.
msg373705 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2020-07-15 17:36
Yes, let's make #13797 a superseder.
History
Date User Action Args
2022-04-11 14:59:33adminsetgithub: 85457
2020-07-15 17:38:42skrahsetstatus: open -> closed
superseder: Allow objects implemented in pure Python to export PEP 3118 buffers
resolution: duplicate
stage: resolved
2020-07-15 17:36:54skrahsetmessages: + msg373705
2020-07-15 14:13:53mdehoonsetmessages: + msg373697
2020-07-15 13:26:51skrahsetmessages: + msg373693
2020-07-15 06:39:52mdehoonsetmessages: + msg373668
2020-07-13 12:56:04methanesetnosy: + methane
messages: + msg373601
2020-07-12 16:08:26christian.heimessetversions: - Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9
2020-07-12 14:37:24xtreaksetnosy: + skrah
2020-07-12 14:23:24mdehooncreate