Building a ctypes-“based” C library with distutils

The distutils documentation here states that: A C extension for CPython is a shared library (e.g. a .so file on Linux, .pyd on Windows), which exports an initialization function. So the only difference regarding a plain shared library seems to be the initialization function (besides a sensible file naming convention I don’t think you have … Read more

Safer way to expose a C-allocated memory buffer using numpy/ctypes?

You have to keep a reference to your Wrapper while any numpy array exists. Easiest way to achieve this, is to save this reference in a attribute of the ctype-buffer: class MyWrapper(object): def __init__(self, n=10): # buffer allocated by external library self.size = n self.addr = libc.malloc(C.sizeof(C.c_int) * n) def __del__(self): # buffer freed by … Read more

Calling C functions in Python

You should call C from Python by writing a ctypes wrapper. Cython is for making python-like code run faster, ctypes is for making C functions callable from python. What you need to do is the following: Write the C functions you want to use. (You probably did this already) Create a shared object (.so, for … Read more

Why is printf() giving a strange output in python?

Those extra ‘1’s at the end of each number are the return value from printf, which returns the number of chars that it prints. The return value of a function called in the interactive interpreter is automatically printed (unless it’s None). In fact, the interactive interpreter prints any non-None expression that isn’t assigned. And of … Read more

Access memory address in python

Have a look at ctypes.string_at. Here’s an example. It dumps the raw data structure of a CPython integer. from ctypes import string_at from sys import getsizeof a = 0x7fff print(string_at(id(a),getsizeof(a)).hex()) Output: 0200000000000000d00fbeaafe7f00000100000000000000ff7f0000 Note that this works with the CPython implementation because id() happens to return the virtual memory address of a Python object, but this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)