Message224395
For the record, I have been (ab?)using the “c_char_p” type to get the address of immutable byte strings without copying memory:
>>> from ctypes import *
>>> data = b"123\x00abc"
>>> pubyte = cast(c_char_p(data), POINTER(c_ubyte))
>>> address = addressof(pubyte.contents)
>>> string_at(address, 7)
b'123\x00abc'
>>> # 2nd instance has same address, rather than memory copy:
... cast(c_char_p(data), c_void_p).value == address
True
In to the documentation, “c_char_p” is only defined for zero-terminated strings, but it seems to also work for strings with embedded zero bytes. However it does not work for “bytearray” objects (“from_buffer” should work instead), nor memory views. |
|
Date |
User |
Action |
Args |
2014-07-31 11:30:47 | martin.panter | set | recipients:
+ martin.panter, theller, vstinner, meador.inge, santoso.wijaya, benrg |
2014-07-31 11:30:47 | martin.panter | set | messageid: <1406806247.79.0.0915292007301.issue11427@psf.upfronthosting.co.za> |
2014-07-31 11:30:47 | martin.panter | link | issue11427 messages |
2014-07-31 11:30:47 | martin.panter | create | |
|