python-c-extension

Loading python pickled object in C

别等时光非礼了梦想. 提交于 2021-02-08 05:45:36
问题 I know pickles can be easily loaded into python using import pickle p = pickle.load(open("file.pkl")) I was wondering how to load the same pickle file in pyx/C code in python? I couldn't find the method to directly load it. Perhaps a solution would be to load in python and pass reference to object in C? 回答1: The easy answer would be to just compile your code with Cython. Everything there will be done automatically. In context of the Python C API, you could easily replicate this code with

Loading python pickled object in C

丶灬走出姿态 提交于 2021-02-08 05:45:23
问题 I know pickles can be easily loaded into python using import pickle p = pickle.load(open("file.pkl")) I was wondering how to load the same pickle file in pyx/C code in python? I couldn't find the method to directly load it. Perhaps a solution would be to load in python and pass reference to object in C? 回答1: The easy answer would be to just compile your code with Cython. Everything there will be done automatically. In context of the Python C API, you could easily replicate this code with

How do you compile Python C/C++ extensions for different OS/versions of Python?

帅比萌擦擦* 提交于 2021-02-07 06:17:52
问题 I have noticed that several mature Python libraries have precompiled versions for most architectures (Win32/Win-amd64/MacOS) and versions of Python. What is the standard way to cross-compile your extensions for different environments? Wine? Virtual machines? Crowd sourcing? 回答1: We use Virtual Machines and a Hudson server. We have a Virtual Machine for each architecture we support (generally compiling doesn't stretch the resources allocated to them, so a VM is fine). I guess the configuration

How do you compile Python C/C++ extensions for different OS/versions of Python?

你离开我真会死。 提交于 2021-02-07 06:16:05
问题 I have noticed that several mature Python libraries have precompiled versions for most architectures (Win32/Win-amd64/MacOS) and versions of Python. What is the standard way to cross-compile your extensions for different environments? Wine? Virtual machines? Crowd sourcing? 回答1: We use Virtual Machines and a Hudson server. We have a Virtual Machine for each architecture we support (generally compiling doesn't stretch the resources allocated to them, so a VM is fine). I guess the configuration

True *args and **kwargs in Python C extension

狂风中的少年 提交于 2020-12-05 12:15:28
问题 I am developing Python 3 C extension. Can I get the equivalent or arbitrary positional or keyword arguments? For instance, in Python, I can write: def fun(name, parent, *args, **kwargs): # do something with name and parent # do something with args and kwargs pass But I cannot find of a simple equivalent in C. While we can perfectly write functions with PyObject* args and PyObject* kwargs , I cannot easily "parse out" name and parent from whichever (args/kwargs) it came. Take: static PyObject*

ctypes.ArgumentError: Don't know how to convert parameter

痴心易碎 提交于 2020-05-27 01:59:48
问题 I define a function in C library as follows: int* Test(char *str1,int id1,char *str2,float val,float *ls) I want to use it in python so I write the following python code: str1 = 'a' str2 = 'b' id1 = 0 val = 1.0 system('g++ -c -fPIC libtraj.cpp -o test.o') system('g++ -shared -Wl,-soname,test.so -o test.so test.o') lib = cdll.LoadLibrary('./test.so') num_item = 100000 ls = (ctypes.c_float * num_item)() lib.Test.restype = ndpointer(dtype=ctypes.c_int, shape=(num_item,)) lib.Test.argtypes = [c

What is the “correct” way to pass a boolean to a Python C extension?

纵然是瞬间 提交于 2020-05-10 03:43:29
问题 This is a simple example from the python documentation (http://docs.python.org/extending/extending.html): static PyObject * spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) return NULL; sts = system(command); return Py_BuildValue("i", sts); } If I want to pass an additional boolean parameter to the function - what's the "correct" way to do it? There doesn't seem to be a bool option to pass to PyArg_ParseTuple(). So I

How to wrap a C pointer and length in a new-style buffer object in Cython?

南笙酒味 提交于 2020-04-06 02:39:09
问题 I'm writing a Python 2.7 extension module in Cython. How do I create a Python object implementing the new-style buffer interface that wraps a chunk of memory given to me by a C library? The chunk of memory is just a string of bytes, not a structure or multidimensional array. I'm given a const void * pointer and a length, and some details about how long the pointer stays valid. I can't copy the memory—that would kill performance for my application. With the old-style buffer objects I could

easy_install fails on error “Couldn't find setup script” after binary upload?

╄→尐↘猪︶ㄣ 提交于 2020-01-13 08:11:11
问题 After uploading a binary distribution of my Python C extension with python setup.py bdist upload , easy_install [my-package-name] fails on "error: Couldn't find a setup script in /tmp/easy_install/package-name-etc-etc". What am I doing wrong? 回答1: This may not be related to your specific problem, but I am providing this information in case it is helpful to others. I hit exactly this error when running easy_install xyz . The problem turned out to be that I had a subdirectory named xyz in the

easy_install fails on error “Couldn't find setup script” after binary upload?

断了今生、忘了曾经 提交于 2020-01-13 08:10:09
问题 After uploading a binary distribution of my Python C extension with python setup.py bdist upload , easy_install [my-package-name] fails on "error: Couldn't find a setup script in /tmp/easy_install/package-name-etc-etc". What am I doing wrong? 回答1: This may not be related to your specific problem, but I am providing this information in case it is helpful to others. I hit exactly this error when running easy_install xyz . The problem turned out to be that I had a subdirectory named xyz in the