本文整理汇总了Python中ctypes.c_char_p方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.c_char_p方法的具体用法?Python ctypes.c_char_p怎么用?Python ctypes.c_char_p使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.c_char_p方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def verify(self, hash, sig): # pylint: disable=redefined-builtin
"""Verify a DER signature"""
if not sig:
return False
# New versions of OpenSSL will reject non-canonical DER signatures. de/re-serialize first.
norm_sig = ctypes.c_void_p(0)
_ssl.d2i_ECDSA_SIG(ctypes.byref(norm_sig), ctypes.byref(ctypes.c_char_p(sig)), len(sig))
derlen = _ssl.i2d_ECDSA_SIG(norm_sig, 0)
if derlen == 0:
_ssl.ECDSA_SIG_free(norm_sig)
return False
norm_der = ctypes.create_string_buffer(derlen)
_ssl.i2d_ECDSA_SIG(norm_sig, ctypes.byref(ctypes.pointer(norm_der)))
_ssl.ECDSA_SIG_free(norm_sig)
# -1 = error, 0 = bad sig, 1 = good
return _ssl.ECDSA_verify(0, hash, len(hash), norm_der, derlen, self.k) == 1
示例2: hamming_klcs
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def hamming_klcs(seqs):
"""
Implementation of k-LCS as described in Christian Blichmann's thesis "Automatisierte Signaturgenerierung fuer Malware-Staemme" on page 52.
This algorithm will not forcibly find THE longest common subsequence among all sequences, as the subsequence returned by the 2-LCS algorithm
might not be the optimal one from the set of longest common subsequences.
:see: https://static.googleusercontent.com/media/www.zynamics.com/en//downloads/blichmann-christian--diplomarbeit--final.pdf
:param seqs: List of sequences
:return: A shared subsequence between the input sequences. Not necessarily the longest one, and only one of several that might exist.
"""
c_seqs_type = c_char_p * len(seqs)
c_seqs = c_seqs_type()
c_seqs[:] = seqs
c_lens_type = c_size_t * len(seqs)
c_lens = c_lens_type()
c_lens[:] = [len(seq) for seq in seqs]
result = create_string_buffer("\0" * min(len(seq) for seq in seqs))
result_len = c_size_t(len(result))
ret = _lib.hamming_klcs_c(c_seqs, c_lens, len(seqs), result, byref(result_len))
if ret == 0:
return result[:result_len.value]
else:
raise RuntimeError("lcs returned error code %d" % ret)
示例3: imdecode
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def imdecode(str_img, flag=1):
"""Decode image from str buffer.
Wrapper for cv2.imdecode that uses mx.nd.NDArray
Parameters
----------
str_img : str
str buffer read from image file
flag : int
same as flag for cv2.imdecode
Returns
-------
img : NDArray
decoded image in (width, height, channels)
with BGR color channel order
"""
hdl = NDArrayHandle()
check_call(_LIB.MXCVImdecode(ctypes.c_char_p(str_img),
mx_uint(len(str_img)),
flag, ctypes.byref(hdl)))
return mx.nd.NDArray(hdl)
示例4: write
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def write(self, buf):
"""Inserts a string buffer as a record.
Example usage:
----------
>>> record = mx.recordio.MXRecordIO('tmp.rec', 'w')
>>> for i in range(5):
... record.write('record_%d'%i)
>>> record.close()
Parameters
----------
buf : string (python2), bytes (python3)
Buffer to write.
"""
assert self.writable
check_call(_LIB.MXRecordIOWriterWriteRecord(self.handle,
ctypes.c_char_p(buf),
ctypes.c_size_t(len(buf))))
示例5: c_str
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def c_str(string):
"""Create ctypes char * from a Python string.
Parameters
----------
string : string type
Python string.
Returns
-------
str : c_char_p
A char pointer that can be passed to C API.
Examples
--------
>>> x = mx.base.c_str("Hello, World")
>>> print x.value
Hello, World
"""
return ctypes.c_char_p(string)
示例6: c_str_array
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def c_str_array(strings):
"""Create ctypes const char ** from a list of Python strings.
Parameters
----------
strings : list of string
Python strings.
Returns
-------
(ctypes.c_char_p * len(strings))
A const char ** pointer that can be passed to C API.
"""
arr = (ctypes.c_char_p * len(strings))()
arr[:] = strings
return arr
示例7: name
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def name(self):
"""Gets name string from the symbol, this function only works for non-grouped symbol.
Returns
-------
value : str
The name of this symbol, returns ``None`` for grouped symbol.
"""
ret = ctypes.c_char_p()
success = ctypes.c_int()
check_call(_LIB.MXSymbolGetName(
self.handle, ctypes.byref(ret), ctypes.byref(success)))
if success.value != 0:
return py_str(ret.value)
else:
return None
示例8: list_attr
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def list_attr(self, recursive=False):
"""Gets all attributes from the symbol.
Example
-------
>>> data = mx.sym.Variable('data', attr={'mood': 'angry'})
>>> data.list_attr()
{'mood': 'angry'}
Returns
-------
ret : Dict of str to str
A dictionary mapping attribute keys to values.
"""
if recursive:
raise DeprecationWarning("Symbol.list_attr with recursive=True has been deprecated. "
"Please use attr_dict instead.")
size = mx_uint()
pairs = ctypes.POINTER(ctypes.c_char_p)()
f_handle = _LIB.MXSymbolListAttrShallow
check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs)))
return {py_str(pairs[i * 2]): py_str(pairs[i * 2 + 1]) for i in range(size.value)}
示例9: list_arguments
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def list_arguments(self):
"""Lists all the arguments in the symbol.
Example
-------
>>> a = mx.sym.var('a')
>>> b = mx.sym.var('b')
>>> c = a + b
>>> c.list_arguments
['a', 'b']
Returns
-------
args : list of string
List containing the names of all the arguments required to compute the symbol.
"""
size = ctypes.c_uint()
sarr = ctypes.POINTER(ctypes.c_char_p)()
check_call(_LIB.MXSymbolListArguments(
self.handle, ctypes.byref(size), ctypes.byref(sarr)))
return [py_str(sarr[i]) for i in range(size.value)]
示例10: set_monitor_callback
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def set_monitor_callback(self, callback):
"""Install callback for monitor.
Parameters
----------
callback : function
Takes a string and an NDArrayHandle.
Examples
--------
>>> def mon_callback(*args, **kwargs):
>>> print("Do your stuff here.")
>>>
>>> texe.set_monitor_callback(mon_callback)
"""
cb_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p, NDArrayHandle, ctypes.c_void_p)
self._monitor_callback = cb_type(_monitor_callback_wrapper(callback))
check_call(_LIB.MXExecutorSetMonitorCallback(
self.handle,
self._monitor_callback,
None))
示例11: __init__
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def __init__(self):
# Locate libnss and try loading it
self.NSS = None
self.load_libnss()
SlotInfoPtr = ct.POINTER(self.PK11SlotInfo)
SECItemPtr = ct.POINTER(self.SECItem)
self._set_ctypes(ct.c_int, "NSS_Init", ct.c_char_p)
self._set_ctypes(ct.c_int, "NSS_Shutdown")
self._set_ctypes(SlotInfoPtr, "PK11_GetInternalKeySlot")
self._set_ctypes(None, "PK11_FreeSlot", SlotInfoPtr)
self._set_ctypes(ct.c_int, "PK11_CheckUserPassword", SlotInfoPtr, ct.c_char_p)
self._set_ctypes(ct.c_int, "PK11SDR_Decrypt", SECItemPtr, SECItemPtr, ct.c_void_p)
self._set_ctypes(None, "SECITEM_ZfreeItem", SECItemPtr, ct.c_int)
# for error handling
self._set_ctypes(ct.c_int, "PORT_GetError")
self._set_ctypes(ct.c_char_p, "PR_ErrorToName", ct.c_int)
self._set_ctypes(ct.c_char_p, "PR_ErrorToString", ct.c_int, ct.c_uint32)
示例12: __init__
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def __init__(self, args):
dir_path = os.path.dirname(os.path.realpath(__file__))
self.lib = ctypes.CDLL('%s/build/dll/libs2v.so' % dir_path)
self.lib.GetGraphStruct.restype = ctypes.c_void_p
self.lib.PrepareBatchGraph.restype = ctypes.c_int
self.lib.PrepareMeanField.restype = ctypes.c_int
self.lib.PrepareLoopyBP.restype = ctypes.c_int
self.lib.NumEdgePairs.restype = ctypes.c_int
if sys.version_info[0] > 2:
args = [arg.encode() for arg in args] # str -> bytes for each element in args
arr = (ctypes.c_char_p * len(args))()
arr[:] = args
self.lib.Init(len(args), arr)
self.batch_graph_handle = ctypes.c_void_p(self.lib.GetGraphStruct())
示例13: get_jl_lib
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def get_jl_lib(conf) -> JuliaPreLoad:
jl = conf["julia"]
lib_path = jl["lib"]
sys_image = jl["image"]
binary = jl["bin"]
lib = ctypes.PyDLL(lib_path, ctypes.RTLD_GLOBAL)
lib.jl_eval_string.argtypes = [ctypes.c_char_p]
lib.jl_eval_string.restype = ctypes.c_void_p
try:
init = lib.jl_init_with_image
except AttributeError:
init = lib.jl_init_with_image__threading
return JuliaPreLoad(
lambda: init(binary.encode(), sys_image.encode()), lib)
示例14: _createShader
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def _createShader(self, strings, shadertype):
# create the shader handle
shader = gl.glCreateShader(shadertype)
# convert the source strings into a ctypes pointer-to-char array, and upload them
# this is deep, dark, dangerous black magick - don't try stuff like this at home!
strings = tuple(s.encode('ascii') for s in strings) # Nick added, for python3
src = (c_char_p * len(strings))(*strings)
gl.glShaderSource(shader, len(strings), cast(pointer(src), POINTER(POINTER(c_char))), None)
# compile the shader
gl.glCompileShader(shader)
# retrieve the compile status
compile_success = c_int(0)
gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS, byref(compile_success))
# if compilation failed, print the log
if compile_success:
gl.glAttachShader(self.id, shader)
else:
gl.glGetShaderiv(shader, gl.GL_INFO_LOG_LENGTH, byref(compile_success)) # retrieve the log length
buffer = create_string_buffer(compile_success.value) # create a buffer for the log
gl.glGetShaderInfoLog(shader, compile_success, None, buffer) # retrieve the log text
print(buffer.value) # print the log to the console
示例15: uiWindowTitle
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_char_p [as 别名]
def uiWindowTitle(window):
"""
Returns the window's title.
:param window: uiWindow
:return: string
"""
# Set return type
clibui.uiWindowTitle.restype = ctypes.c_char_p
title = clibui.uiWindowTitle(window)
return title.decode()
# - void uiWindowSetTitle(uiWindow *w, const char *title);