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.

Author dabeaz
Recipients dabeaz
Date 2012-09-14.15:05:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1347635130.32.0.649142152247.issue15944@psf.upfronthosting.co.za>
In-reply-to
Content
I've been playing with the interaction of ctypes and memoryviews and am curious about intended behavior.  Consider the following:

>>> import ctypes
>>> d = ctypes.c_double()
>>> m = memoryview(d)
>>> m.ndim
0
>>> m.shape
()
>>> m.readonly
False
>>> m.itemsize
8
>>>

As you can see, you have a memory view for the ctypes double object.  However, the fact that it has a 0-dimension and no shape seems to cause all sorts of weird behavior.  For instance, indexing and slicing don't work:

>>> m[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: invalid indexing of 0-dim memory
>>> m[:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: invalid indexing of 0-dim memory
>>> 

As such, you can't really seem to do anything interesting with the resulting memory view.  For example, you can't pull data out of it.  Nor can you overwrite the contents (i.e., replacing the contents with an 8-byte byte string).

Attempting to cast the memory view to something else doesn't work either.

>>> d = ctypes.c_double()
>>> m = memoryview(d)
>>> m2 = m.cast('c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: memoryview: source format must be a native single character format prefixed with an optional '@'
>>> 

I must be missing something really obvious here.  Is there no way to get access to the memory behind a ctypes object?
History
Date User Action Args
2012-09-14 15:05:30dabeazsetrecipients: + dabeaz
2012-09-14 15:05:30dabeazsetmessageid: <1347635130.32.0.649142152247.issue15944@psf.upfronthosting.co.za>
2012-09-14 15:05:29dabeazlinkissue15944 messages
2012-09-14 15:05:29dabeazcreate