ctypes

Why cpython exposes 'PyTuple_SetItem' as C-API if tuple is immutable by design?

Why cpython exposes 'PyTuple_SetItem' as C-API if tuple is immutable by design? Question: Tuple in python is immutable by design, so if we try to mutate a tuple object python emits following TypeError which make sense. >>> a = (1, 2, 3) >>> a[0] = 12 Traceback (most recent call last): File "<stdin>", line 1, …

Total answers: 2

How can I create this struct?

How can I create this struct? Question: I want to create a struct like this: import ctypes class MyStruct(ctypes.Structure): _fields_ = [(‘field1’, /* size of 16 bytes */), (‘field2’, /* size of 4 bytes */) (‘field3’, /* size of 8 bytes */)] What is the types that i need to write here for these sizes …

Total answers: 1

how do we typecast a large C structure from a DLL to a Python ctypes.Structure

how do we typecast a large C structure from a DLL to a Python ctypes.Structure Question: I’ve got a pointer to an XML document structure generated through libxml2. How do I cast it to my Python class xmlDoc(ctypes.Structure):? Here’s my Python code: # libxml2 is a software library for parsing XML documents. ”’ This lib …

Total answers: 1

Sending numpy array (image) to c++ library with ctypes

Sending numpy array (image) to c++ library with ctypes Question: I’m trying to make a python program that can run Photoshop Plugins by using this DLL library from Spetric – https://github.com/spetric/Photoshop-Plugin-Host I need to send an image loaded by opencv to the dll (while I can read c++ I cannot program in it and have …

Total answers: 1

ctypes struct containing arrays and manipulating

ctypes struct containing arrays and manipulating Question: I’m trying to use ctypes in my python code test.pyto pass the array to test.c and do some calculation and bring the result to python code. Here is ‘test.c’ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "test.h" void test(int N, complex *v){ int k; for(k=0; k<N; k++){ v[k].Re …

Total answers: 2

Sending big amount of bytes from Swift to Python with types and @_cdecl

Sending big amount of bytes from Swift to Python with types and @_cdecl Question: I have two simple functions, one pass to another array of UInts. When I pass small array of 20 UInts function works, but when I pass 21576 Uints function returns small amount of bites, why is it happened? I checked UnsafeMutablePointer<UInt8> …

Total answers: 2

p4a Cannot Import Module _ctypes

p4a Cannot Import Module _ctypes Question: I am an extreme beginner with kivy and python-for-android. I’ve created a kivy tutorial app and have been trying to create the apk using p4a. I believe I’ve figured out the dependencies and command line requirements but I am currently stuck with a failure due to "_ctypes module not …

Total answers: 1

bytearray throwing error for python class object when ctype structure present as class attributes

bytearray throwing error for python class object when ctype structure present as class attributes Question: I was able to convert ctype class object into bytearray() using below method – from ctypes import * class get_log_input_payload(Structure): _pack_ = 1 _fields_ = ( ("log_identifier",c_uint8*16), ("offset",c_uint32), ("length",c_uint32) ) payload = get_log_input_payload() b_array = bytearray(payload) but when we used …

Total answers: 1

When I write an int64 type number to the function, a different number is returned

When I write an int64 type number to the function, a different number is returned Question: I converted the golang code to c code and called it from python. but when the function should return a number close to the number I wrote inside, it returns a very different number. main.py import ctypes library = …

Total answers: 1