PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

Your understanding is correct: invoking PyEval_InitThreads does, among other things, acquire the GIL. In a correctly written Python/C application, this is not an issue because the GIL will be unlocked in time, either automatically or manually. If the main thread goes on to run Python code, there is nothing special to do, because Python interpreter … Read more

ImportError: dynamic module does not define init function (initfizzbuzz)

The error also occurs, when using boost::python, if the module name is different to the compiled .so file name. For example: hello.cpp #include <boost/python/module.hpp> #include <boost/python/def.hpp> using namespace std; using namespace boost::python; int helloWorld(){ cout << “Hello world!” << endl; return 0; } BOOST_PYTHON_MODULE(libhello) { def(“hello_world”, helloWorld); } compilation command: g++ -fpic -shared -o libfoo.so … Read more

Py_INCREF/DECREF: When

First, read this more carefully, specifically the last paragraph, http://docs.python.org/extending/extending.html#ownership-rules Easy way to think about it is thinking about the reference counts. Your first statement is correct. If you create a new Python object (say PyLong) then it already has a reference count of 1. This is fine if you’re going to return it but … Read more

How to create a generator/iterator with the Python C API?

Below is a simple implementation of module spam with one function myiter(int) returning iterator: import spam for i in spam.myiter(10): print i prints numbers from 0 to 9. It is simpler then your case but shows main points: defining object with standard __iter__() and next() methods, and implementing iterator behaviour including raising StopIteration when appropriate. … Read more

Create an object using Python’s C API

Call PyObject_New(), followed by PyObject_Init(). EDIT: The best way is to call the class object, just like in Python itself: /* Pass two arguments, a string and an int. */ PyObject *argList = Py_BuildValue(“si”, “hello”, 42); /* Call the class object. */ PyObject *obj = PyObject_CallObject((PyObject *) &pyfoo_T, argList); /* Release the argument list. */ … Read more

How to dynamically create a derived type in the Python C-API

I encountered the same problem when I was modifying an extension to be compatible with Python 3, and found this page when I was trying to solve it. I did eventually solve it by reading the source code for the Python interpreter, PEP 0384 and the documentation for the C-API. Setting the Py_TPFLAGS_HEAPTYPE flag tells … Read more

Calling a python method from C/C++, and extracting its return value

As explained before, using PyRun_SimpleString seems to be a bad idea. You should definitely use the methods provided by the C-API (http://docs.python.org/c-api/). Reading the introduction is the first thing to do to understand the way it works. First, you have to learn about PyObject that is the basic object for the C API. It can … Read more

Python (and Python C API): __new__ versus __init__ [duplicate]

The difference mainly arises with mutable vs immutable types. __new__ accepts a type as the first argument, and (usually) returns a new instance of that type. Thus it is suitable for use with both mutable and immutable types. __init__ accepts an instance as the first argument and modifies the attributes of that instance. This is … Read more

fatal error: Python.h: No such file or directory

Looks like you haven’t properly installed the header files and static libraries for python dev. Use your package manager to install them system-wide. For apt (Ubuntu, Debian…): sudo apt-get install python-dev # for python2.x installs sudo apt-get install python3-dev # for python3.x installs For yum (CentOS, RHEL…): sudo yum install python-devel # for python2.x installs … Read more

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