本文整理汇总了Python中ctypes.Array方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.Array方法的具体用法?Python ctypes.Array怎么用?Python ctypes.Array使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.Array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: synchronized
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def synchronized(obj, lock=None):
assert not isinstance(obj, SynchronizedBase), 'object already synchronized'
if isinstance(obj, ctypes._SimpleCData):
return Synchronized(obj, lock)
elif isinstance(obj, ctypes.Array):
if obj._type_ is ctypes.c_char:
return SynchronizedString(obj, lock)
return SynchronizedArray(obj, lock)
else:
cls = type(obj)
try:
scls = class_cache[cls]
except KeyError:
names = [field[0] for field in cls._fields_]
d = dict((name, make_property(name)) for name in names)
classname = 'Synchronized' + cls.__name__
scls = class_cache[cls] = type(classname, (SynchronizedBase,), d)
return scls(obj, lock)
#
# Functions for pickling/unpickling
#
示例2: synchronized
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def synchronized(obj, lock=None, ctx=None):
assert not isinstance(obj, SynchronizedBase), 'object already synchronized'
ctx = ctx or get_context()
if isinstance(obj, ctypes._SimpleCData):
return Synchronized(obj, lock, ctx)
elif isinstance(obj, ctypes.Array):
if obj._type_ is ctypes.c_char:
return SynchronizedString(obj, lock, ctx)
return SynchronizedArray(obj, lock, ctx)
else:
cls = type(obj)
try:
scls = class_cache[cls]
except KeyError:
names = [field[0] for field in cls._fields_]
d = dict((name, make_property(name)) for name in names)
classname = 'Synchronized' + cls.__name__
scls = class_cache[cls] = type(classname, (SynchronizedBase,), d)
return scls(obj, lock, ctx)
#
# Functions for pickling/unpickling
#
示例3: synchronized
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def synchronized(obj, lock=None, ctx=None):
assert not isinstance(obj, SynchronizedBase), 'object already synchronized'
ctx = ctx or get_context()
if isinstance(obj, ctypes._SimpleCData):
return Synchronized(obj, lock, ctx)
elif isinstance(obj, ctypes.Array):
if obj._type_ is ctypes.c_char:
return SynchronizedString(obj, lock, ctx)
return SynchronizedArray(obj, lock, ctx)
else:
cls = type(obj)
try:
scls = class_cache[cls]
except KeyError:
names = [field[0] for field in cls._fields_]
d = {name: make_property(name) for name in names}
classname = 'Synchronized' + cls.__name__
scls = class_cache[cls] = type(classname, (SynchronizedBase,), d)
return scls(obj, lock, ctx)
#
# Functions for pickling/unpickling
#
示例4: add_dline
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def add_dline(buf: ctypes.Array, cell_width: int, position: int, thickness: int, cell_height: int) -> None:
a = min(position - thickness, cell_height - 1)
b = min(position, cell_height - 1)
top, bottom = min(a, b), max(a, b)
deficit = 2 - (bottom - top)
if deficit > 0:
if bottom + deficit < cell_height:
bottom += deficit
elif bottom < cell_height - 1:
bottom += 1
if deficit > 1:
top -= deficit - 1
else:
top -= deficit
top = max(0, min(top, cell_height - 1))
bottom = max(0, min(bottom, cell_height - 1))
for y in {top, bottom}:
ctypes.memset(ctypes.addressof(buf) + (cell_width * y), 255, cell_width)
示例5: add_curl
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def add_curl(buf: ctypes.Array, cell_width: int, position: int, thickness: int, cell_height: int) -> None:
max_x, max_y = cell_width - 1, cell_height - 1
xfactor = 2.0 * pi / max_x
half_height = max(thickness // 2, 1)
def add_intensity(x: int, y: int, val: int) -> None:
y += position
y = min(y, max_y)
idx = cell_width * y + x
buf[idx] = min(255, buf[idx] + val)
# Ensure all space at bottom of cell is used
if position + half_height < max_y:
position += max_y - (position + half_height)
if position + half_height > max_y:
position -= position + half_height - max_y
# Use the Wu antialias algorithm to draw the curve
# cosine waves always have slope <= 1 so are never steep
for x in range(cell_width):
y = half_height * cos(x * xfactor)
y1, y2 = floor(y), ceil(y)
i1 = int(255 * abs(y - y1))
add_intensity(x, y1, 255 - i1)
add_intensity(x, y2, i1)
示例6: __new__
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def __new__(cls, shape, dtype=None, buffer=None, offset=None, strides=None,
order=None):
# init buffer
if buffer is None:
assert offset is None
assert strides is None
size = int(np.prod(shape))
nbytes = size * np.dtype(dtype).itemsize
# this is the part that can be passed between processes
shmem = mp.RawArray(ctypes.c_char, nbytes)
offset = 0
elif isinstance(buffer, ctypes.Array):
# restoring from a pickle
shmem = buffer
else:
raise ValueError(
f"{cls.__name__} does not support specifying custom "
f" buffers, but was given {buffer!r}")
# init array
obj = np.ndarray.__new__(cls, shape, dtype=dtype, buffer=shmem,
offset=offset, strides=strides, order=order)
obj._shmem = shmem
return obj
示例7: c_uint8_ptr
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def c_uint8_ptr(data):
if byte_string(data) or isinstance(data, _Array):
return data
elif isinstance(data, _buffer_type):
obj = _py_object(data)
buf = _Py_buffer()
_PyObject_GetBuffer(obj, byref(buf), _PyBUF_SIMPLE)
try:
buffer_type = c_ubyte * buf.len
return buffer_type.from_address(buf.buf)
finally:
_PyBuffer_Release(byref(buf))
else:
raise TypeError("Object type %s cannot be passed to C code" % type(data))
# ---
示例8: test_create_cluster_notify_port
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def test_create_cluster_notify_port(self, notif_filters,
exp_notif_filters_len=1,
notif_port_h=None,
notif_key=None):
self._mock_ctypes()
self._ctypes.Array = ctypes.Array
self._clusapi_utils.create_cluster_notify_port_v2(
mock.sentinel.cluster_handle,
notif_filters,
notif_port_h,
notif_key)
exp_notif_key_p = self._ctypes.byref(notif_key) if notif_key else None
exp_notif_port_h = notif_port_h or w_const.INVALID_HANDLE_VALUE
self._mock_run.assert_called_once_with(
self._clusapi.CreateClusterNotifyPortV2,
exp_notif_port_h,
mock.sentinel.cluster_handle,
self._ctypes.byref(notif_filters),
self._ctypes.c_ulong(exp_notif_filters_len),
exp_notif_key_p,
**self._clusapi_utils._open_handle_check_flags)
示例9: test_indices
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def test_indices(self):
a = self.a
self.assertEqual(a[0, 0], 0)
self.assertEqual(a[19, 0], 0)
self.assertEqual(a[0, 14], 0)
self.assertEqual(a[19, 14], 0)
self.assertEqual(a[5, 8], 0)
a[0, 0] = 12
a[5, 8] = 99
self.assertEqual(a[0, 0], 12)
self.assertEqual(a[5, 8], 99)
self.assertRaises(IndexError, a.__getitem__, (-1, 0))
self.assertRaises(IndexError, a.__getitem__, (0, -1))
self.assertRaises(IndexError, a.__getitem__, (20, 0))
self.assertRaises(IndexError, a.__getitem__, (0, 15))
self.assertRaises(ValueError, a.__getitem__, 0)
self.assertRaises(ValueError, a.__getitem__, (0, 0, 0))
a = Array((3,), 'i', 4)
a[1] = 333
self.assertEqual(a[1], 333)
示例10: native
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def native(type_, value):
if isinstance(value, type_):
return value
if sys.version_info < (3,) and type_ == int and isinstance(value, int_types):
return value
if isinstance(value, ctypes.Array) and value._type_ == ctypes.c_byte:
return ctypes.string_at(ctypes.addressof(value), value._length_)
return type_(value.value)
示例11: test_vec_creates_ctypes_array
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def test_vec_creates_ctypes_array():
data = ugl.vec([10, 20, 30])
assert isinstance(data, ctypes.Array)
示例12: default
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def default(self, obj):
if isinstance(obj, (SkinningBone, SkinnedImage, skinnedmesh.SkinnedMesh)):
d = obj.__dict__.copy()
for ignore in JSON_IGNORES + getattr(obj, "jsonIgnore", []):
if ignore in d:
del d[ignore]
return d
elif isinstance(obj, euclid.Vector3):
return (obj.x, obj.y, obj.z)
elif isinstance(obj, ctypes.Array):
return list(obj)
return json.JSONEncoder.default(self, obj)
示例13: Array
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
示例14: reduce_ctype
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def reduce_ctype(obj):
assert_spawning(obj)
if isinstance(obj, ctypes.Array):
return rebuild_ctype, (obj._type_, obj._wrapper, obj._length_)
else:
return rebuild_ctype, (type(obj), obj._wrapper, None)
示例15: syscall_enter
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import Array [as 别名]
def syscall_enter(pid, regs, mem_fd=None):
syscall_num = getattr(regs, defs.FUNCTION_REG)
syscall_name = defs.SYSCALL_NUMBERS.get(
syscall_num, '<{}>'.format(syscall_num))
signature = defs.SYSCALLS.get(syscall_name, _unknown_syscall)
args = []
for i, param in enumerate(signature.params):
raw_value = getattr(regs, defs.ARGS_REGS[i])
ptype = param.type
if ptype.ptr_indirection or issubclass(ptype.ctype, ctypes.Array):
if raw_value != 0:
value = memory.read_c_type_ptr(
pid, raw_value, ptype.ctype, ptype.ptr_indirection, mem_fd)
else:
value = None
else:
value = ptype.ctype(raw_value).value
arg = syscalldef.SysCallArg(name=param.name, type=param.type,
raw_value=raw_value, value=value)
args.append(arg)
syscall = syscalldef.SysCall(
name=syscall_name, args=args, result=None, pid=pid)
return syscall