Questions tagged [python-extensions]

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.

323 questions
1
vote
1 answer

PyLongObject memory leak

There is a memory leak in my encode method. static const size_t _bits_per_digit = sizeof(digit) * CHAR_BIT; /** * Returns a Python int storing the bitstream of the Elias gamma encoded input list. */ static PyObject * encode(PyObject *self,…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
1
vote
1 answer

Use PyObject_Realloc correctly

https://github.com/python/cpython/blob/master/Include/objimpl.h#L83 says PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory at p. PyObject_Realloc doesn't free the memory. I've noticed a memory leak in my program which uses…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
1
vote
1 answer

safely handle huge numbers in python extension

Since Python 3 there's no upper limit on the size of an int. I'd like to deal with huge ints of 150+ decimal digits. This is far larger than a unsigned long long is guaranteed to be, so I don't think I can rely on PyLong_AsLongLong and manipulate…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
1
vote
1 answer

How to build py3-none-any wheels for a project with an optional C extension?

msgpack includes an optional cython extension. Some users of the package want py3-none-any wheels of msgpack. I'm trying to figure out how to make it possible to build wheels both with and without the optional extension.
1
vote
1 answer

Extending python with non python aware C library?

I would like to create a module that uses a non python aware C library and ctypes. According to this documentation about creating a C extension to python, I’m required to provide a module initialization function with the signature PyObject*…
chmike
  • 20,922
  • 21
  • 83
  • 106
1
vote
1 answer

Exposing a new type to python at run time

I have gone through and defined my new types, stored them in a pytypeobject and called the following functions (after initializing the interpreter): PyType_Ready(); //this takes my defined typed PyModule_AddObject(); //this adds the defined type to…
DaneEC117
  • 227
  • 3
  • 10
1
vote
0 answers

Initializing an python object from C++ with boost/python returns None

I have a python module that I imported to C++ like this : .cpp file: boost::python::object mod; void init_mod(boost::python::object o){ mod = o; } // some code BOOST_PYTHON_MODULE(MyModule){ def("init_mod", &init_mod); // for the other…
qwerty_99
  • 640
  • 5
  • 20
1
vote
1 answer

How to return a pointer to a C++ object to python using boost if the class of that object is declared in another boost module?

Okay, maybe I'm not finding an answer because I'm not sure how to word it, but I have a class called Info and another class called Packet, both are compiled into Python extensions using boost, and I would like to return a pointer to an Info object…
qwerty_99
  • 640
  • 5
  • 20
1
vote
1 answer

Trying to understand how reference count works in Python extensions

In my efforts to understand how reference count works in python extensions, I built the following module: #include PyObject *value; static PyObject* pyref_store(PyObject *self, PyObject* args) { PyArg_ParseTuple(args, "O", &value); …
bees
  • 11
  • 3
1
vote
0 answers

How to correctly implement a python iterator in C++?

As a part of a Python module I'm writing in C++ I need to make a type containing an std::vector iterable in Python. I added a tp_iter function to the type containing the vector that creates an iterator object which has a tp_iternext. The iterator…
SU3
  • 5,064
  • 3
  • 35
  • 66
1
vote
1 answer

How to convert a Python deque to a C++ container?

I have to pass a Python deque to a C++ function but I can't find any documentation on how to convert a deque to a C++ container (for this case, either a deque or a vector would do). Searched everywhere but I couldn't find a solution. There's gotta…
qwerty_99
  • 640
  • 5
  • 20
1
vote
0 answers

How fast is PyObject_CallMethod compared to pure python?

I'm trying to optimize some python code by isolating part of the code into a C++ extension. However, since the code is heavily object-oriented, I'll need to set some attributes as PyObject*. My question is how fast is this compared to pure python…
qwerty_99
  • 640
  • 5
  • 20
1
vote
0 answers

Is using the C code to create Python extensions UB in C++ due to lifetime?

There are many tutorials out there on how to create C extensions of Python which introduce a new type. One example: https://docs.python.org/3.5/extending/newtypes.html This usually boils down to creating a struct like: struct Example { …
Flamefire
  • 5,313
  • 3
  • 35
  • 70
1
vote
2 answers

How to instantiate a custom object from within a C module?

I'm having trouble creating an instance of a class (type) I wrote from within the C module. I've written a minimal, self-contained example that illustrates the point. Just copy-paste the three files spam.c, spamtest.py, and setup.py to a directory…
musbur
  • 567
  • 4
  • 16
1
vote
1 answer

Compiling python3 C extensions

I wrote the following code for my CPython extension : #include static PyObject *greeting(PyObject* self) { return Py_BuildValue("s" , "Hello python modules!!!"); } static char *my_docstring = "This is a sample docstring"; static…
Parsa Mousavi
  • 1,052
  • 1
  • 13
  • 31