Python ctypes.addressof() Examples
The following are 30
code examples of ctypes.addressof().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ctypes
, or try the search function
.
Example #1
Source File: codec.py From HdrHistogram_py with Apache License 2.0 | 6 votes |
def encode(self): '''Compress the associated encodable payload, prepend the header then encode with base64 if requested Returns: the b64 encoded wire encoding of the histogram (as a string) or the compressed payload (as a string, if b64 wrappinb is disabled) ''' # only compress the first non zero buckets # if histogram is empty we do not encode any counter if self.histogram.total_count: relevant_length = \ self.histogram.get_counts_array_index(self.histogram.max_value) + 1 else: relevant_length = 0 cpayload = self.payload.compress(relevant_length) if self.b64_wrap: self.header.length = len(cpayload) header_str = ctypes.string_at(addressof(self.header), ext_header_size) return base64.b64encode(header_str + cpayload) return cpayload
Example #2
Source File: _base.py From training_results_v0.6 with Apache License 2.0 | 6 votes |
def ctypes2numpy_shared(cptr, shape): """Convert a ctypes pointer to a numpy array The result numpy array shares the memory with the pointer Parameters ---------- cptr : ctypes.POINTER(mx_float) pointer to the memory region shape : tuple shape of target ndarray Returns ------- out : numpy_array A numpy array : numpy array """ if not isinstance(cptr, ctypes.POINTER(mx_float)): raise RuntimeError('expected float pointer') size = 1 for s in shape: size *= s dbuffer = (mx_float * size).from_address(ctypes.addressof(cptr.contents)) return np.frombuffer(dbuffer, dtype=np.float32).reshape(shape)
Example #3
Source File: etw.py From pywintrace with Apache License 2.0 | 6 votes |
def __init__(self, event_property, event_filters): """ Initializes an ENABLE_TRACE_PARAMETERS structure. :param event_property: Property to enable. See https://msdn.microsoft.com/en-us/library/windows/desktop/dd392306(v=vs.85).aspx :param event_filters: List of EVENT_FILTER_DESCRIPTOR structures """ self._props = ct.pointer(et.ENABLE_TRACE_PARAMETERS()) filter_buf_size = ct.sizeof(ep.EVENT_FILTER_DESCRIPTOR) * len(event_filters) # noinspection PyCallingNonCallable filter_buf = (ct.c_char * filter_buf_size)() # copy contents to buffer for i in range(len(event_filters)): ct.memmove(ct.cast(ct.addressof(filter_buf) + (ct.sizeof(ep.EVENT_FILTER_DESCRIPTOR) * i), ct.c_void_p), ct.byref(event_filters[i]), ct.sizeof(ep.EVENT_FILTER_DESCRIPTOR)) self._props.contents.Version = et.ENABLE_TRACE_PARAMETERS_VERSION_2 self._props.contents.EnableProperty = event_property self._props.contents.ControlFlags = 0 self._props.contents.EnableFilterDesc = ct.cast(ct.pointer(filter_buf), ct.POINTER(ep.EVENT_FILTER_DESCRIPTOR)) self._props.contents.FilterDescCount = len(event_filters)
Example #4
Source File: windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def _get_ipv4_routing_table(self): routing_table = [] with self._get_forward_table() as p_forward_table: forward_table = p_forward_table.contents table = ctypes.cast( ctypes.addressof(forward_table.table), ctypes.POINTER(Win32_MIB_IPFORWARDROW * forward_table.dwNumEntries)).contents for row in table: destination = Ws2_32.inet_ntoa( row.dwForwardDest).decode() netmask = Ws2_32.inet_ntoa( row.dwForwardMask).decode() gateway = Ws2_32.inet_ntoa( row.dwForwardNextHop).decode() routing_table.append(( destination, netmask, gateway, row.dwForwardIfIndex, row.dwForwardMetric1)) return routing_table
Example #5
Source File: evntprov.py From pywintrace with Apache License 2.0 | 6 votes |
def __init__(self, match_any, match_all, level, filter_in, names): struct_size = ((sum([len(name) for name in names]) * ct.sizeof(wt.CHAR)) + (ct.sizeof(wt.CHAR) * len(names))) +\ ct.sizeof(EVENT_FILTER_EVENT_NAME) self._buf = (ct.c_char * struct_size)() self._props = ct.cast(ct.pointer(self._buf), ct.POINTER(EVENT_FILTER_EVENT_NAME)) self._props.contents.MatchAnyKeyword = match_any self._props.contents.MatchAllKeyword = match_all self._props.contents.Level = level self._props.contents.FilterIn = filter_in self._props.contents.NameCount = len(names) str_off = 0 for i in range(len(names)): ct.memmove(ct.cast(ct.addressof(self._buf) + ct.sizeof(EVENT_FILTER_EVENT_NAME) + str_off, ct.c_void_p), names[i], len(names[i])) str_off += len(names[i]) + ct.sizeof(wt.CHAR)
Example #6
Source File: gdbstub.py From nightmare with GNU General Public License v2.0 | 6 votes |
def platformParseBinaryPe(self, filename, baseaddr, normname): # If we're on windows, fake out the PE header and use dbghelp #if platform.system() in ['Microsoft', 'Windows']: if False: # FIXME this code is stolen and should be a function! import vtrace.platforms.win32 as vt_win32 fakepe = self.readMemory(baseaddr, 1024) tfile = tempfile.NamedTemporaryFile(delete=False) tfilename = tfile.name import ctypes pebuf = ctypes.create_string_buffer(fakepe) try: try: tfile.write(fakepe) tfile.close() #parser = vt_win32.Win32SymbolParser(-1, tfilename, baseaddr) parser = vt_win32.Win32SymbolParser(-1, None, ctypes.addressof(pebuf)) parser.parse() parser.loadSymsIntoTrace(self, normname) finally: os.unlink(tfilename) except Exception, e: print e
Example #7
Source File: basic.py From lambda-packs with MIT License | 6 votes |
def __get_eval_info(self): """Get inner evaluation count and names.""" if self.__need_reload_eval_info: self.__need_reload_eval_info = False out_num_eval = ctypes.c_int(0) # Get num of inner evals _safe_call(_LIB.LGBM_BoosterGetEvalCounts( self.handle, ctypes.byref(out_num_eval))) self.__num_inner_eval = out_num_eval.value if self.__num_inner_eval > 0: # Get name of evals tmp_out_len = ctypes.c_int(0) string_buffers = [ctypes.create_string_buffer(255) for i in range_(self.__num_inner_eval)] ptr_string_buffers = (ctypes.c_char_p * self.__num_inner_eval)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_BoosterGetEvalNames( self.handle, ctypes.byref(tmp_out_len), ptr_string_buffers)) if self.__num_inner_eval != tmp_out_len.value: raise ValueError("Length of eval names doesn't equal with num_evals") self.__name_inner_eval = \ [string_buffers[i].value.decode() for i in range_(self.__num_inner_eval)] self.__higher_better_inner_eval = \ [name.startswith(('auc', 'ndcg@', 'map@')) for name in self.__name_inner_eval]
Example #8
Source File: basic.py From lambda-packs with MIT License | 6 votes |
def feature_name(self): """Get names of features. Returns ------- result : list List with names of features. """ num_feature = self.num_feature() # Get name of features tmp_out_len = ctypes.c_int(0) string_buffers = [ctypes.create_string_buffer(255) for i in range_(num_feature)] ptr_string_buffers = (ctypes.c_char_p * num_feature)(*map(ctypes.addressof, string_buffers)) _safe_call(_LIB.LGBM_BoosterGetFeatureNames( self.handle, ctypes.byref(tmp_out_len), ptr_string_buffers)) if num_feature != tmp_out_len.value: raise ValueError("Length of feature names doesn't equal with num_feature") return [string_buffers[i].value.decode() for i in range_(num_feature)]
Example #9
Source File: test_hdrhistogram.py From HdrHistogram_py with Apache License 2.0 | 6 votes |
def test_zz_encode_errors(): with pytest.raises(TypeError): encode() with pytest.raises(TypeError): encode(None, None, 0, 0) src_array = (c_uint16 * ARRAY_SIZE)() src_array_addr = addressof(src_array) dst_len = 9 * ARRAY_SIZE # negative length with pytest.raises(ValueError): encode(src_array_addr, -1, sizeof(c_uint16), 0, dst_len) # dest length too small with pytest.raises(ValueError): encode(src_array_addr, ARRAY_SIZE, 4, 0, 4) # invalid word size with pytest.raises(ValueError): encode(src_array_addr, ARRAY_SIZE, 3, 0, 0) # Null dest ptr with pytest.raises(ValueError): encode(src_array_addr, ARRAY_SIZE, 4, 0, dst_len)
Example #10
Source File: base.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def ctypes2numpy_shared(cptr, shape): """Convert a ctypes pointer to a numpy array. The resulting NumPy array shares the memory with the pointer. Parameters ---------- cptr : ctypes.POINTER(mx_float) pointer to the memory region shape : tuple Shape of target `NDArray`. Returns ------- out : numpy_array A numpy array : numpy array. """ if not isinstance(cptr, ctypes.POINTER(mx_float)): raise RuntimeError('expected float pointer') size = 1 for s in shape: size *= s dbuffer = (mx_float * size).from_address(ctypes.addressof(cptr.contents)) return np.frombuffer(dbuffer, dtype=np.float32).reshape(shape)
Example #11
Source File: test_hdrhistogram.py From HdrHistogram_py with Apache License 2.0 | 6 votes |
def check_zz_identity(src_array, int_type, min_nz_index, max_nz_index, total_count, offset): dst_len = (sizeof(int_type) + 1) * ARRAY_SIZE dst = (c_uint8 * (offset + dst_len))() varint_len = encode(addressof(src_array), ARRAY_SIZE, sizeof(int_type), addressof(dst) + offset, dst_len) varint_string = string_at(dst, varint_len + offset) dst_array = (int_type * ARRAY_SIZE)() res = decode(varint_string, offset, addressof(dst_array), ARRAY_SIZE, sizeof(int_type)) assert res['total'] == total_count if total_count: assert res['min_nonzero_index'] == min_nz_index assert res['max_nonzero_index'] == max_nz_index for index in range(ARRAY_SIZE): assert dst_array[index] == src_array[index] # A large positive value that can fit 16-bit signed
Example #12
Source File: pdraw.py From olympe with BSD 3-Clause "New" or "Revised" License | 6 votes |
def unref(self): """ This function decrements the reference counter of the underlying buffer(s) """ try: res = od.vbuf_unref(self._buf) if res != 0: self.logger.error("vbuf_unref unpacked frame error: {} {} {}".format( self._media_id, os.strerror(-res), ctypes.addressof(self._buf.contents) )) finally: if self._yuv_packed_buffer: res = od.vbuf_unref(self._yuv_packed_buffer) if res != 0: self.logger.error("vbuf_unref packed frame error: {} {} {}".format( self._media_id, os.strerror(-res), ctypes.addressof(self._buf.contents) ))
Example #13
Source File: _process_win32.py From Computable with MIT License | 6 votes |
def arg_split(commandline, posix=False, strict=True): """Split a command line's arguments in a shell-like manner. This is a special version for windows that use a ctypes call to CommandLineToArgvW to do the argv splitting. The posix paramter is ignored. If strict=False, process_common.arg_split(...strict=False) is used instead. """ #CommandLineToArgvW returns path to executable if called with empty string. if commandline.strip() == "": return [] if not strict: # not really a cl-arg, fallback on _process_common return py_arg_split(commandline, posix=posix, strict=strict) argvn = c_int() result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn)) result_array_type = LPCWSTR * argvn.value result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))] retval = LocalFree(result_pointer) return result
Example #14
Source File: spi.py From rpi3-webiopi with Apache License 2.0 | 6 votes |
def xfer(self, txbuff=None): length = len(txbuff) if PYTHON_MAJOR >= 3: _txbuff = bytes(txbuff) _txptr = ctypes.create_string_buffer(_txbuff) else: _txbuff = str(bytearray(txbuff)) _txptr = ctypes.create_string_buffer(_txbuff) _rxptr = ctypes.create_string_buffer(length) data = struct.pack("QQLLHBBL", #64 64 32 32 16 8 8 32 b = 32B ctypes.addressof(_txptr), ctypes.addressof(_rxptr), length, self.speed, 0, #delay self.bits, 0, # cs_change, 0 # pad ) fcntl.ioctl(self.fd, SPI_IOC_MESSAGE(len(data)), data) _rxbuff = ctypes.string_at(_rxptr, length) return bytearray(_rxbuff)
Example #15
Source File: codec.py From HdrHistogram_py with Apache License 2.0 | 5 votes |
def add(self, other_encoder): add_array(addressof(self.get_counts()), addressof(other_encoder.get_counts()), self.histogram.counts_len, self.histogram.word_size)
Example #16
Source File: backend_ctypes.py From teleport with Apache License 2.0 | 5 votes |
def _convert_to_address(self, BClass): if getattr(BClass, '_BItem', None) is self.__class__: return ctypes.addressof(self._blob) else: return CTypesData._convert_to_address(self, BClass)
Example #17
Source File: test_hdrhistogram.py From HdrHistogram_py with Apache License 2.0 | 5 votes |
def test_add_array_errors(): with pytest.raises(TypeError): add_array() with pytest.raises(TypeError): add_array(100) with pytest.raises(TypeError): add_array(None, None, 0, 0) src_array = (c_uint16 * ARRAY_SIZE)() # negative length with pytest.raises(ValueError): add_array(addressof(src_array), addressof(src_array), -1, sizeof(c_uint16)) # invalid word size with pytest.raises(ValueError): add_array(addressof(src_array), addressof(src_array), 0, 0)
Example #18
Source File: backend_ctypes.py From teleport with Apache License 2.0 | 5 votes |
def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob))
Example #19
Source File: test_hdrhistogram.py From HdrHistogram_py with Apache License 2.0 | 5 votes |
def check_zz_encode(int_type): src_array = (int_type * ARRAY_SIZE)() src_array_addr = addressof(src_array) dst_len = 9 * ARRAY_SIZE dst_array = (c_uint8 * dst_len)() dst_array_addr = addressof(dst_array) res = encode(src_array_addr, ARRAY_SIZE, sizeof(int_type), dst_array_addr, dst_len) # should be 1 byte set to 0x13 (10 zeros => value = -10, or 0x13 in zigzag # encoding assert res == 1 assert dst_array[0] == 0x13 # last counter set to 1 # the encoded result should be 2 bytes long # 0x11 (9 zeros => -9 coded as 17) # 0x02 (1 is coded as 2) src_array[ARRAY_SIZE - 1] = 1 res = encode(src_array_addr, ARRAY_SIZE, sizeof(int_type), dst_array_addr, dst_len) assert res == 2 assert dst_array[0] == 0x11 assert dst_array[1] == 0x02 # all counters set to 1, we should get a zigzag encoded of # 10 bytes all set to 0x02 (in zigzag encoding 1 is coded as 2) for index in range(ARRAY_SIZE): src_array[index] = 1 res = encode(src_array_addr, ARRAY_SIZE, sizeof(int_type), dst_array_addr, dst_len) assert res == ARRAY_SIZE for index in range(ARRAY_SIZE): assert dst_array[index] == 2
Example #20
Source File: backend_ctypes.py From teleport with Apache License 2.0 | 5 votes |
def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob))
Example #21
Source File: backend_ctypes.py From SwiftKitten with MIT License | 5 votes |
def write_variable(self, BType, name, value): new_ctypes_obj = BType._to_ctypes(value) ctypes_obj = BType._ctype.in_dll(self.cdll, name) ctypes.memmove(ctypes.addressof(ctypes_obj), ctypes.addressof(new_ctypes_obj), ctypes.sizeof(BType._ctype))
Example #22
Source File: backend_ctypes.py From oss-ftp with MIT License | 5 votes |
def write_variable(self, BType, name, value): new_ctypes_obj = BType._to_ctypes(value) ctypes_obj = BType._ctype.in_dll(self.cdll, name) ctypes.memmove(ctypes.addressof(ctypes_obj), ctypes.addressof(new_ctypes_obj), ctypes.sizeof(BType._ctype))
Example #23
Source File: backend_ctypes.py From SwiftKitten with MIT License | 5 votes |
def _convert_to_address(self, BClass): if getattr(BClass, '_BItem', None) is self.__class__: return ctypes.addressof(self._blob) else: return CTypesData._convert_to_address(self, BClass)
Example #24
Source File: backend_ctypes.py From SwiftKitten with MIT License | 5 votes |
def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob))
Example #25
Source File: backend_ctypes.py From SwiftKitten with MIT License | 5 votes |
def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob))
Example #26
Source File: backend_ctypes.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def write_variable(self, BType, name, value): new_ctypes_obj = BType._to_ctypes(value) ctypes_obj = BType._ctype.in_dll(self.cdll, name) ctypes.memmove(ctypes.addressof(ctypes_obj), ctypes.addressof(new_ctypes_obj), ctypes.sizeof(BType._ctype))
Example #27
Source File: backend_ctypes.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _convert_to_address(self, BClass): if getattr(BClass, '_BItem', None) is self.__class__: return ctypes.addressof(self._blob) else: return CTypesData._convert_to_address(self, BClass)
Example #28
Source File: backend_ctypes.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob))
Example #29
Source File: backend_ctypes.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob))
Example #30
Source File: geometry.py From bioforum with MIT License | 5 votes |
def __repr__(self): "Short-hand representation because WKT may be very large." return '<%s object at %s>' % (self.geom_type, hex(addressof(self.ptr))) # Pickling support