python / cpython

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

`object`-backed `memoryview`'s `tolist` errors #85395

Open 20b13c20-bc9c-43ca-8fb6-9d24ce334500 opened 4 years ago

20b13c20-bc9c-43ca-8fb6-9d24ce334500 commented 4 years ago
BPO 41223
Nosy @sweeneyde, @jakirkham

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: ```python assignee = None closed_at = None created_at = labels = ['interpreter-core', 'type-feature', '3.11'] title = "`object`-backed `memoryview`'s `tolist` errors" updated_at = user = 'https://github.com/jakirkham' ``` bugs.python.org fields: ```python activity = actor = 'Dennis Sweeney' assignee = 'none' closed = False closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'jakirkham' dependencies = [] files = [] hgrepos = [] issue_num = 41223 keywords = [] message_count = 2.0 messages = ['373175', '402802'] nosy_count = 2.0 nosy_names = ['Dennis Sweeney', 'jakirkham'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue41223' versions = ['Python 3.11'] ```

20b13c20-bc9c-43ca-8fb6-9d24ce334500 commented 4 years ago

When working with an object-backed memoryview, it seems we are unable to coerce it to a list. This would be useful as it would provide a way to get the underlying object's into something a bit easier to work with.

In [1]: import numpy                                                            

In [2]: a = numpy.array(["abc", "def", "ghi"], dtype=object)                    

In [3]: m = memoryview(a)                                                       

In [4]: m.tolist()                                                              
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-4-42400a31add8> in <module>
----> 1 m.tolist()

NotImplementedError: memoryview: format O not supported
sweeneyde commented 3 years ago

Can you describe your use-case more? In particular, why use memoryview(a).tolist() instead of a.tolist()? Or some other numpy operations like a.flat or a.view()? Or even numpy.array(memoryview(a)) to recover a numpy array from a memoryview?

If this were implemented and stdlib memoryview objects supported some kind of object 'O' format, would list(memoryview(b"\0\0\0\0\0\0\0\0").cast('O')) dereference a null pointer and segfault?