335 questions
1
vote
1
answer
2k
views
How to release GIL after Py_Initialize?
I am trying to embed python in a GUI application, and so I have a the python interpreter initialized in the main thread of the application, and I may launch python scripts from another one.
I read ...
1
vote
1
answer
330
views
Interfacing a cpython extension with a C struct
I am trying to create a python extension to interface python with my C software. Mainly I need to made a big struct from C software accessible and modifiable in python, so I would like to create a ...
0
votes
1
answer
344
views
_PyMem_DebugRawFree: bad ID: Allocated using API '', verified using API 'o'
My program crashed with the following message from CPython
Debug memory block at address p=0x8a6e80: API ''
0 bytes originally requested
The 7 pad bytes at p-7 are not all FORBIDDENBYTE (0xfd):...
1
vote
0
answers
676
views
Testing a vscode extension that depends on another one
I'm trying to write a test for an extension. The extension uses the python extension (ms-python.python).
Therefore I put this in my package.json:
"extensionDependencies": [
"ms-...
2
votes
0
answers
1k
views
Calling Numpy functions in C++ using Pybind11
After experimenting with Pybind11 to create C++ extensions for Python, I was hoping to write C++ sections for computationally heavy parts of my code. In my particular case, I would like to work with ...
1
vote
1
answer
2k
views
Sorting imports fails on Python VSCode extension
Background
I have an anaconda environment that has Python 3.7 installed. I have a file with some imports out of order which I want VScode to order when pressing CRTL+s.
However, instead or ordering ...
4
votes
1
answer
2k
views
Identifying Packages Using Python's Stable ABI
I have a Python package containing a number of C/C++ extensions built as a single wheel. I'm trying to understand how to ensure the wheel and shared libraries it contains correctly advertise that ...
1
vote
0
answers
173
views
f2py convert subroutine with argument has intent of inout
Through f2py -c sorting.f90 -m sortF, I get sortF.cpython-37m-x86_64-linux-gnu.so which is great.
sorting.f90
module sorting
use iso_fortran_env, only: i4 => int32, i8 => int64
implicit ...
2
votes
0
answers
569
views
Reasons for "Segmentation fault (core dumped)" when using Python extension and FFmpeg
I want to write a Python C extension that includes a function convertVideo() that converts a video from one format to another making use of FFmpeg 3.4.8 (the libav* libaries). The code of the ...
14
votes
1
answer
936
views
Defining a Python enum in a C extension - am I doing this right?
I'm working on a Python C extension and I would like to expose a custom enum (as in: a class inheriting from enum.Enum) that would be entirely defined in C.
It turned out to not be a trivial task and ...
0
votes
0
answers
478
views
Python bdist_wheel + install works but sdist + install fails due to PEP 517
I'm working on a Python package, that wraps a C++ library using Pybind11 + cmake, the code is available at https://github.com/bayesmix-dev/pybmix
Since I'm working on Linux, in order to distribute it ...
0
votes
0
answers
64
views
Why should we almost always reassign an object members before decrementing their reference counts?
I was reading through this tutorial in order to learn how to write C extension module to define new types. At some point the following tp_init implementation was given:
static int
Custom_init(...
0
votes
1
answer
302
views
Importing python modules and extension modules with the same name
Let's say I built a package with a Python module and an extension module in it, but with the identical name
mypackage
|
+-- __init__.py
|
+-- mymodule.py
|
+-- mymodule.cpython-39-x86_64-linux-gnu.so
...
0
votes
0
answers
829
views
Use c++ python extension in google colab
I've made a python extension with c++ that runs in visual studio. I have 4 files from this:
BeeLib.exp
BeeLib.lib
BeeLib.pdb
BeeLib.pyd
I want to use these in google colab like I do in visual studio,...
3
votes
1
answer
392
views
How to define a Python metaclass from C extension?
In pure Python, it is relatively simple to define and use a metaclass.
class Meta(type):
def __new__(cls, name, bases, dict):
x = super().__new__(cls, name, bases, dict)
print(&...
1
vote
1
answer
230
views
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.
*/...
1
vote
1
answer
167
views
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 ...
0
votes
0
answers
135
views
How to allow my custom object to be pickled?
I created a bit sequence custom type in C
typedef struct {
PyObject_VAR_HEAD
uint8_t data[1];
} BitStream;
static PyTypeObject BitStreamType = {
PyVarObject_HEAD_INIT(NULL, 0)
....
0
votes
1
answer
519
views
How to reduce size of my custom python type?
I created my own custom Python type. It stores an internal array of bytes. Even when there's no bytes in the array, the object takes up 32 bytes. How can I reduce that?
Here's how I defined my type:
...
-1
votes
1
answer
2k
views
Python Extension for visual studio code not showing error or warning messages
When i write python code on my visual studio code editor i can't find any warnings or errors.
I installed python extension,
I also change the version of that extension..
I also read many ...
1
vote
1
answer
82
views
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 ...
1
vote
1
answer
3k
views
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 ...
1
vote
1
answer
148
views
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 ...
5
votes
1
answer
3k
views
How to create Python Stub Files and where to put?
I have a compiled a Python extension.
The resulting binary mylib.so file can be imported in the Python script and works fine.
Now I am wondering how to write the interface stub file mylib.pyi such, ...
9
votes
1
answer
6k
views
How to structure and distribute Pybind11 extension with stubs?
I'm trying to create and distribute (with pip) a Python package that has Python code, and C++ code compiled to a .pyd file with Pybind11 (using Visual Studio 2019). I also want to include .pyi stub ...
0
votes
1
answer
154
views
Is there a way to version a Python C extension?
I have a python3 distribution that has one package and also a C extension. I would like to be able to specify the version of the C extension during imports in the tell.py script, but cannot seem to ...
0
votes
1
answer
346
views
C-extension in Python - pyObject called Py_DECREF ,reference is 0,but memory leak
this is my code.
PyObject *dataPyParams = PyList_New(0);
for (int i = 0; i < figdata.dataSetList.size(); i++)
{
PyObject *pyParams = PyList_New(0);
for (int j = 0; j < figdata....
0
votes
1
answer
528
views
Why GCC-compiled Python libraries compatible across compiler versions while MSVC ones are not?
I am adding some Python packages (from pip) to a 3rd-party application's Python interpreter on both Linux and Windows. In the Linux release of their application they compiled Python against GCC 4.xx, ...
4
votes
0
answers
1k
views
Accessing a c++ vector/array from python via cython
I am wrapping a c++ program via cython and would like to make a vector from this program available in Python.
I have written a getter method for the vector, which works fine except that it returns a ...
0
votes
1
answer
1k
views
Building Python extension fails ("kernel32.lib" cannot be opened | "x64" conflicts with target machine type "x86")
I am trying to install the python extension line-profiler via pip on a fresh installation of Anaconda3. As the package requires a compiler, I have installed the Visual Studio Build Tools 2019 along ...
0
votes
1
answer
966
views
How to access/retrieve member variable of PyObject from within python extension
The function PyObject_Call() is used to call a method on a PyObject. But how to get/read a member variable of a PyObject?
0
votes
1
answer
111
views
How to combine own C-extension in own Python Package
I created own Python Package in which I want to add own Python C Extension, because in Python Package I'm importing this C-Ext.
I want to install it from local files, not pypi.
I have dist files of C ...
7
votes
1
answer
4k
views
Including and distributing third party libraries with a Python C extension
I'm building a C Python extension which makes use of a "third party" library— in this case, one that I've built using a separate build process and toolchain. Call this library libplumbus....
0
votes
0
answers
139
views
Creating a new timer_t object after deleting a previous one doesn't work
I'll try to keep it as short as possible.
I'm making a python application which uses a C Extension (hw_timer).
This extension is in charge of creating a timer.
The Python application starts by calling ...
4
votes
2
answers
4k
views
Convert std::string to PyObject in C++ in Python3
I am trying to convert std::string to PyObject.
std::string st = jsp.updateRoot(people, people);
PyObject* pValue = PyBytes_AsString(st);
It is not working using the above method.
How can I convert?
1
vote
0
answers
181
views
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){
...
1
vote
1
answer
258
views
copy and deepcopy in Python C API
How does one define copy and deepcopy methods for a Python type defined in a C extension?
Looking at the documentation, there doesn't appear to be a tp_ slot for these methods.
0
votes
0
answers
245
views
How to call a python method that returns a queue from C++ using boost (non-sclar type error)?
I have a method in python that's implemented like this :
class MyClass:
def __init__(self, i, j, k):
self._i = i
self._j = j
self._k = k
def generate_queue(self, max, ...
1
vote
1
answer
386
views
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 ...
0
votes
0
answers
33
views
Python C++ module causing a core dump when accessing the same PyObject* attribute more than once [duplicate]
So I've finally managed to import the C module into python. However, I'm getting a core dump when I try to access the attribute of an object more than once. Here's the code:
node.h:
#pragma once
#...
0
votes
0
answers
47
views
dynamic module does not define init function error when importing .so file compiled with boost python (Python 3.7) [duplicate]
This might have already been posted somewhere else but I couldn't find anything that worked. I have the following libraries / tools installed :
boost:
boost-python36.x86_64
boost-python36-devel.x86_64
...
4
votes
4
answers
7k
views
error while installing python extensions " can't open file 'directory + filename': [Errno 2] No such file or directory "
I tried to install mypy extension in vs code but it keeps showing me this error below
C:\Users\Yourusername\Dev\django_project_boilerplate\env\Scripts\python.exe: can't open file 'c:UsersYourusername....
1
vote
1
answer
61
views
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 <Python.h>
PyObject *value;
static PyObject* pyref_store(PyObject *self, ...
1
vote
0
answers
227
views
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 ...
1
vote
1
answer
238
views
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 ...
1
vote
0
answers
161
views
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 ...
2
votes
2
answers
264
views
Call `+=` on a PyObject in C++
I'm writing a Python module in C++.
At a certain point, I need to add a PyObject of an arbitrary type to another one. In other words, do the same as a += b would do in Python. But I haven't been able ...
0
votes
1
answer
984
views
Package C source files along with a Python package
I have a C library I am writing, and my goal is to be able to package and distribute that C library via a python package on PyPI. The concept is, it's a combination of Python code, an extension module,...
0
votes
0
answers
237
views
What is the C-API interface to create built-in array object in Python?
I am working on a Python C-extension code. Currently, a 1D C-array is converted to a Python List. Now I need to convert an ND-array, described by a 1) data type, 2) shape (as a 1D integer vector, for ...
0
votes
1
answer
1k
views
Correct way to use PyArray_SimpleNewFromData?
I am trying to create a numpy array in a python extension.
The call of PyArray_SimpleNewFromData gives me a segfault.
I am trying to fix it for hours, and now I am the point where I have no more idea ...