python-c-extension

Python C Extension: PyEval_GetLocals() returns NULL

落爺英雄遲暮 提交于 2019-12-24 07:38:10
问题 I need to read local variables from Python in C/C++. When I try to PyEval_GetLocals , I get a NULL. This happens although Python is initialized. The following is a minimal example. #include <iostream> #include <Python.h> Py_Initialize(); PyRun_SimpleString("a=5"); PyObject *locals = PyEval_GetLocals(); std::cout<<locals<<std::endl; //prints NULL (prints 0) Py_Finalize(); In the manual, it says that it returns NULL if no frame is running, but... there's a frame running! What am I doing wrong?

embedding python error on initialization

不问归期 提交于 2019-12-23 22:07:41
问题 when im running C code to call python functions, there's error on Py_Initialize() The error is ImportError: No module named site. Ive tried to put Py_SetProgramName(argv[0]) but it doesnt work. The cmd call is cInterfacePython Test.py multiply 3 2 (exe is cInterfacePython) 回答1: I had to muck about a bit with the PATH env-var as well as PYTHONPATH to make things work better when embedding. Py_SetProgramName is not important, it's mostly for internal reference etc... So, I suggest you find

How to create a .dylib C extension on mac os x with distutils and/or setuptools?

邮差的信 提交于 2019-12-23 12:37:20
问题 I need to make a C extension with distutils (and/or setuptools) that can be used BOTH dynamically at runtime, and at compile time (for different purposes). This isn't a problem on Linux, but it's an issue on OS X. By default, distutils creates a "bundle" on OS X (and names it .so), which can be used at runtime, but NOT at compile time. I need to make a .dylib, which can be linked to at compile time, and I need to do it within a setup.py that I can package for PyPI. No matter what I try,

Return the include and runtime lib directories from within Python

99封情书 提交于 2019-12-23 10:39:57
问题 Lets say I want to use gcc from the command line in order to compile a C extension of Python. I'd structure the call something like this: gcc -o applesauce.pyd -I C:/Python35/include -L C:/Python35/libs -l python35 applesauce.c I noticed that the -I , -L , and -l options are absolutely necessary, or else you will get an error that looks something like this. These commands tell gcc where to look for the headers ( -I ), where to look for the static libraries ( -L ), and which static library to

How to have PyPI package install header files for C extension with distutils/setuptools?

心已入冬 提交于 2019-12-22 09:20:12
问题 We have a package (rebound) up on PyPI that includes a C extension . The relevant part of the setup.py file looks like this (simplified): libreboundmodule = Extension('librebound', sources = [ 'src/rebound.c'], include_dirs = ['src'],) Additional libraries need access to rebound.h, but when one runs pip install rebound it doesn't install rebound.h anywhere. How can we get distutils/setuptools to install rebound.h somewhere along with all the python modules? We're hoping that we can have pip

segmentation fault - core dump in python C-extension

一世执手 提交于 2019-12-22 08:09:20
问题 I am writing a c-extension for python. As you can see below, the aim of the code is to calculate the euclidean-dist of two vectors. the first param n is the dimension of the vectors, the second , the third param is the two list of float. I call the function in python like this: import cutil cutil.c_euclidean_dist(2,[1.0,1,0],[0,0]) and it worked well, return the right result. but if i do it for more than 100 times(the dimension is 1*1000), it will cause segmentation fault - core dump: #!/usr

How to make a copy of a python module at runtime?

时光怂恿深爱的人放手 提交于 2019-12-17 18:26:34
问题 I need to make a copy of a socket module to be able to use it and to have one more socket module monkey-patched and use it differently. Is this possible? I mean to really copy a module, namely to get the same result at runtime as if I've copied socketmodule.c , changed the initsocket() function to initmy_socket() , and installed it as my_socket extension. 回答1: You can always do tricks like importing a module then deleting it from sys.modules or trying to copy a module. However, Python already

How can I assert from Python C code?

社会主义新天地 提交于 2019-12-13 13:03:32
问题 I'm writing a Python class in C and I want to put assertions in my debug code. assert.h suits me fine. This only gets put in debug compiles so there's no chance of an assert failure impacting a user of the Python code*. I'm trying to divide my 'library' code (which should be separate to the code linked against Python) so I can use it from other C code. My Python methods are therefore thinnish wrappers around my pure-C code. So I can't do this in my 'library' code: if (black == white) { PyErr

“…can't figure out the architecture type of…” problem when compiling Python C-extension with gcc

此生再无相见时 提交于 2019-12-13 11:57:49
问题 I just upgraded from Snow Leopard to Lion, and an old python c-extension that I had to update didn't want to compile properly. I don't really know what to do here. Anyone who could help me out so it compiles ok? It compiled just fine back in Snow Leopard. Home:folder Me$ python setup.py build running build running build_ext building 'ccookies' extension gcc -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.7

How do I create a fixed-length, mutable array of Python objects in Cython?

有些话、适合烂在心里 提交于 2019-12-12 07:59:43
问题 I need to have an array of python objects to be used in creating a trie datastructure. I need a structure that will be fixed-length like a tuple and mutable like a list. I don't want to use a list because I want to be able to ensure that the list is exactly the right size (if it starts allocating extra elements, the memory overhead could add up very quickly as the trie grows larger). Is there a way to do this? I tried creating an array of objects: cdef class TrieNode: cdef object members[32]