python-c-api

Modifying or Reraising Python error in C API

帅比萌擦擦* 提交于 2019-12-22 21:16:29
问题 I have a bit of code that tries to parse an object as an integer: long val = PyLong_AsLong(obj); if(val == -1 && PyErr_Occurred()) { return -1; } Here obj is a vanilla PyObject * , and PyLong_AsLong raises a very generic TypeError if obj is not an integer. I would like to transform the error message into something a bit more informative, so I would like to either modify the existing error object, or to reraise it. My current solution is to do this: long val = PyLong_AsLong(obj); if(val == -1

Modifying or Reraising Python error in C API

馋奶兔 提交于 2019-12-22 21:16:04
问题 I have a bit of code that tries to parse an object as an integer: long val = PyLong_AsLong(obj); if(val == -1 && PyErr_Occurred()) { return -1; } Here obj is a vanilla PyObject * , and PyLong_AsLong raises a very generic TypeError if obj is not an integer. I would like to transform the error message into something a bit more informative, so I would like to either modify the existing error object, or to reraise it. My current solution is to do this: long val = PyLong_AsLong(obj); if(val == -1

Call a Python function from within a C program

99封情书 提交于 2019-12-21 23:03:07
问题 I have an application in C and at some point I need to solve a non-linear optimization problem. Unfortunately AFAIK there are very limited resources to do that in C (please let me know otherwise). However it is quite simple to do it in Python, e.g. scipy.optimize.minimize. While I was trying to do that I encountered some of what it seems to be very frequent pitfalls, e.g. Python.h not found, module not loading, segmentation fault on function call, etc. What is a quick and easy first-timer’s

Build a PyObject* from a C function?

烂漫一生 提交于 2019-12-21 08:15:09
问题 I am embedding Python in a C++ library which I am making. I would like users to be able to pass C functions in the form of function pointers PyObject* (fpFunc*)(PyObject*,PyObject*); so that I can use those functions in the embedded Python. So I have a function pointer and I know that it is possible to put this function as a module's method using a PyMethodDef struct and passing it to Py_InitModule("module", ModMethods); and thus obtaining a PyObject* module which I can easily grab functions

Unable to import a custom DLL in python

依然范特西╮ 提交于 2019-12-21 06:19:10
问题 I am trying to expose a C++ class to python with boost::python , so I am going through this tutorial. I created a visual studio .dll project, with this source code: #include <boost/python.hpp> using namespace boost::python; struct World { void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; }; BOOST_PYTHON_MODULE(hello) { class_<World>("World") .def("greet", &World::greet) .def("set", &World::set) ; } And I built it as a 64-bit dll. The next step

Nested Python C Extensions/Modules?

风格不统一 提交于 2019-12-21 04:53:14
问题 How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so that it imported via "import mymodule.bar"? (Sorry if this is poorly phrased, I wasn't sure what the proper term for it was.) I tried the following in setup.py, but it doesn't seem to work: from distutils.core import setup, Extension setup(name='mymodule', version='1.0', author='Me', ext_modules=[Extension('mymodule', [

What does error_already_set in Boost.python do and how to handle exceptions similarly in Python C API

北城以北 提交于 2019-12-19 19:45:24
问题 I have been working on a project where I want to remove the boost dependencies and replace it with the Python C API. I spent some time understanding the Python C API and I saw this catch (error_already_set const &) I read the boost docs but it explains where it is used. But I want to know why it is needed and how can I achieve the same functionality using the native Python C api. 回答1: Boost throws error_already_set when a Python error has occurred. So if you see code like this: try { bp::exec

How to use setuptools packages and ext_modules with the same name?

帅比萌擦擦* 提交于 2019-12-19 11:40:18
问题 I got the following file structure for my Python C Extension project: . ├── setup.py ├── source ├── cppimplementation │ └── fastfile.cpp └── fastfilepackage ├── __init__.py └── version.py And I use the following setup.py file: from setuptools import setup, Extension setup( name= 'fastfilepackage', version= '0.1.1', package_dir = { '': 'source', }, packages = [ 'fastfilepackage', ], ext_modules= [ Extension( 'fastfilepackage', [ 'source/cppimplementation/fastfile.cpp', ] ) ], ) I install them

Error when Importing tensorflow in embedded python in c++

China☆狼群 提交于 2019-12-19 10:42:39
问题 My question is regarding embedding Python 3.5 interpreter in a C++ program to receive an image from C++, and use it as an input for my trained tensorflow model. When I import tensorflow library in my python code I get an error (other libraries work fine). The simplified code is as follows: #include <string> #include <windows.h> #include <stdio.h> int main() { Py_InitializeEx(1); PyObject* sysPath = PySys_GetObject((char*)"path"); PyObject* curDir = PyUnicode_FromString("."); PyList_Append

What is the PyClass_New equivalent in Python 3?

允我心安 提交于 2019-12-19 03:10:25
问题 Previously I have created some Python classes using C API. When I'm going to build the old project with Python 3+ it gives following compile error PyClass_New was not declared in this scope Py_InitModule was not declared in this scope What are the equivalents? PyObject *pClassDic = PyDict_New(); PyObject *pClassName = PyBytes_FromString("MyClass"); PyObject *pClass = PyClass_New(NULL, pClassDic, pClassName); 回答1: If you don't want to go through the hassle of setting up your object and type