python-extensions

How can i declare a Boost.Python C++ Class with PyObjects

邮差的信 提交于 2021-02-17 05:20:53
问题 i want to write a c++ class with PyObjects to access them from Python to reference them with an existing Python object instance. In short words i want to store/manage Python object instances in the C++ class. For example: struct Var { PyObject *Test_1; PyObject *Test_2; }; #include <boost/python.hpp> using namespace boost::python; BOOST_PYTHON_MODULE(Var) { class_<Var>("Var", init<std::string>()) .def_readwrite("Test_1", &Var::value) .def_readwrite("Test_2", &Var::value) ; } in python i want

Python extension in C - Metaclass

被刻印的时光 ゝ 提交于 2021-02-08 10:21:06
问题 I have the following python code: class Meta(type): def __call__(cls, *args, **kwargs): obj = type.__call__(cls, *args, **kwargs) # Only do checks for subclasses if cls.__name__ == 'Parent': return obj required_attrs = ['x'] for ra in required_attrs: if ra not in dir(obj): fmt = 'Subclasses of Parent must define the %s attribute' raise NotImplementedError(fmt % ra) return obj class Parent(metaclass=Meta): pass class Child(Parent): def __init__(self): self.x = True Meta is used only to require

Python extension in C - Metaclass

佐手、 提交于 2021-02-08 10:19:42
问题 I have the following python code: class Meta(type): def __call__(cls, *args, **kwargs): obj = type.__call__(cls, *args, **kwargs) # Only do checks for subclasses if cls.__name__ == 'Parent': return obj required_attrs = ['x'] for ra in required_attrs: if ra not in dir(obj): fmt = 'Subclasses of Parent must define the %s attribute' raise NotImplementedError(fmt % ra) return obj class Parent(metaclass=Meta): pass class Child(Parent): def __init__(self): self.x = True Meta is used only to require

`PyTuple_Pack` segfault

谁说胖子不能爱 提交于 2021-01-20 04:57:07
问题 I have a function foo in a Python Extension Module that should return a tuple of ints to Python. This can be easily done using Py_BuildValue : static PyObject* foo(PyObject* self, PyObject* args) { int a = 0; int b = 0; /* calculations and stuff */ PyObject* out = Py_BuildValue("(iii)", a, b, a+b); Py_INCREF(out); return out; } Instead of Py_BuildValue , I want to use PyTuple_Pack , which ensures that the return value is indeed a tuple. The Python C API documentation says that PyTuple_Pack(3,

`PyTuple_Pack` segfault

假如想象 提交于 2021-01-20 04:56:25
问题 I have a function foo in a Python Extension Module that should return a tuple of ints to Python. This can be easily done using Py_BuildValue : static PyObject* foo(PyObject* self, PyObject* args) { int a = 0; int b = 0; /* calculations and stuff */ PyObject* out = Py_BuildValue("(iii)", a, b, a+b); Py_INCREF(out); return out; } Instead of Py_BuildValue , I want to use PyTuple_Pack , which ensures that the return value is indeed a tuple. The Python C API documentation says that PyTuple_Pack(3,

Can python-C++ extension get a C++ object and call its member function?

限于喜欢 提交于 2020-08-24 06:43:07
问题 I am writing a python/C++ application, that will call methods in C++ extension from python. Say my C++ has a class: class A { private: int _i; public: A(int i){_i=i;} int get_i(){return _i;} } A a=A(); It there anyway that python can get a object in C++ and call its member function, i.e.: import cpp_extension a=cpp_extension.A() print a.get_i() Any reference to general reading is also appreciated. 回答1: Yes. You can create a Python C++ extension where your C++ objects will be visible from

What is a PyObject in Python?

给你一囗甜甜゛ 提交于 2020-07-15 10:05:14
问题 Short version I recently came across some Python code in which the return type for a function was specified as PyObject in the documentation. What is a PyObject ? Detailed version I am not a C/C++ programmer, but when I ran into PyObject in the documentation linked above, Google taught me that PyObject is a Python object as defined using the Python/C API. Specifically, the API documentation defines PyObject as follows: All object types are extensions of this type. This is a type which