Message224447
Extending byref to support bytes and str objects sounds reasonable.
Here's another workaround to pass a bytes object with an offset:
from ctypes import *
from ctypes.util import find_library
class offset:
def __init__(self, arg, offset):
self.arg = arg
self.offset = offset
class my_char_p(c_char_p):
@classmethod
def from_param(cls, arg):
if isinstance(arg, offset):
t = cast(arg.arg, POINTER(c_char * 0))[0]
carg = byref(t, arg.offset)
else:
carg = super().from_param(arg)
return carg
atoi = CDLL(find_library('c')).atoi
atoi.argtypes = [my_char_p]
>>> atoi(b'12345')
12345
>>> atoi(offset(b'12345', 1))
2345
>>> atoi(offset(b'12345', 3))
45
You can also convert bytearray, memoryview, and array.array objects from_param. If the object's buffer is writable you can use a ctypes type's from_buffer method to create the C arg. This takes an optional offset argument. Otherwise use from_buffer_copy or cast. |
|
Date |
User |
Action |
Args |
2014-07-31 21:46:41 | eryksun | set | recipients:
+ eryksun, theller, amaury.forgeotdarc, belopolsky, meador.inge, benrg, BreamoreBoy, martin.panter |
2014-07-31 21:46:41 | eryksun | set | messageid: <1406843201.86.0.187177948868.issue11429@psf.upfronthosting.co.za> |
2014-07-31 21:46:41 | eryksun | link | issue11429 messages |
2014-07-31 21:46:41 | eryksun | create | |
|