python-c-api

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

心已入冬 提交于 2020-06-24 00:01:05
问题 Assume we have the type Noddy as defined in the tutorial on writing C extension modules for Python. Now we want to create a derived type, overwriting only the __new__() method of Noddy . Currently I use the following approach (error checking stripped for readability): PyTypeObject *BrownNoddyType = (PyTypeObject *)PyType_Type.tp_alloc(&PyType_Type, 0); BrownNoddyType->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; BrownNoddyType->tp_name = "noddy.BrownNoddy"; BrownNoddyType->tp_doc =

Building a Python-C-Extension on Windows with a debug Python installation

为君一笑 提交于 2020-06-12 11:41:28
问题 If I build CPython from source on Windows I encounter problems when I want to pip install a package that contains a C-Extension. It seems like the error happens while linking the libraries. For example when installing cython (but it also crashes with the same error on other C extension packages): LINK : fatal error LNK1104: cannot open file 'python38.lib' error: command 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\link.exe' failed

How to include (script-built) libraries with package installation?

浪尽此生 提交于 2020-06-01 03:23:26
问题 I am making a Python package that has a C++-extension module and someone else's shared library that it requires. I want everything installable via pip . My current setup.py file works when I use pip install -e . but when I don't use develop mode (e.i. omit the -e ) I get "cannot open shared object file" when importing the module in Python. I believe the reason is that setuptools doesn't consider the shared library to be part of my package, so the relative link to the library is broken during

How to include (script-built) libraries with package installation?

天大地大妈咪最大 提交于 2020-06-01 03:23:13
问题 I am making a Python package that has a C++-extension module and someone else's shared library that it requires. I want everything installable via pip . My current setup.py file works when I use pip install -e . but when I don't use develop mode (e.i. omit the -e ) I get "cannot open shared object file" when importing the module in Python. I believe the reason is that setuptools doesn't consider the shared library to be part of my package, so the relative link to the library is broken during

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 can convert PyObject variable to Mat in c++ opencv code

跟風遠走 提交于 2020-05-01 09:16:52
问题 I have a c++ facerecognition code and a python code in opencv. In python code i read frames from a robot and i want to send this fram to my c++ code. I use this link tho call python function in c++ function. my c++ function is embed.cpp: #include <Python.h> #include <stdlib.h> #include "opencv2/core/core.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <fstream> #include <sstream> #include </home/nao/Desktop/python/boost_1_61_0

How can convert PyObject variable to Mat in c++ opencv code

假装没事ソ 提交于 2020-05-01 09:15:27
问题 I have a c++ facerecognition code and a python code in opencv. In python code i read frames from a robot and i want to send this fram to my c++ code. I use this link tho call python function in c++ function. my c++ function is embed.cpp: #include <Python.h> #include <stdlib.h> #include "opencv2/core/core.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <fstream> #include <sstream> #include </home/nao/Desktop/python/boost_1_61_0

Unhandled exception at multiarray.pyd the 2nd time the program runs

爱⌒轻易说出口 提交于 2020-04-30 07:30:44
问题 I'm making a .dll plug-in in c++ and embedding python 2.7 in it. Everything worked fine with simple .py programs until I imported a large program. The strangest thing is that the program runs with no problem the first time, but the second time an exception is raised: Unhandled exception at 0x6731ADA1 (multiarray.pyd) in EuroScope.exe: 0xC0000005: Access violation writing location 0x00000001. (The Lib/Dll folders and modules are all copied to the .exe folder) I've searched the web and there

How to deal with PyCapsule type inside Python

╄→尐↘猪︶ㄣ 提交于 2020-03-20 11:59:33
问题 I'm trying to pass the object from QtGui.QWidget.effectiveWinId() to win32gui.SetWindowLong() effectiveWinId() is returning: <capsule object NULL at 0x027C9BF0> <class 'PyCapsule'> and SetWindowLong() expects a PyHANDLE (doc says it "should" accept an integer also) TypeError: The object is not a PyHANDLE object So my question is how do I grab the value out of a PyCapsule object and or check if its NULL? It seems PyCapsule is all internal API to the C code. Also I found this bug that does

How to deal with PyCapsule type inside Python

☆樱花仙子☆ 提交于 2020-03-20 11:59:09
问题 I'm trying to pass the object from QtGui.QWidget.effectiveWinId() to win32gui.SetWindowLong() effectiveWinId() is returning: <capsule object NULL at 0x027C9BF0> <class 'PyCapsule'> and SetWindowLong() expects a PyHANDLE (doc says it "should" accept an integer also) TypeError: The object is not a PyHANDLE object So my question is how do I grab the value out of a PyCapsule object and or check if its NULL? It seems PyCapsule is all internal API to the C code. Also I found this bug that does