335 questions
2
votes
1
answer
2k
views
How to find minimum versions of python interpreter and libs using CMake
I am building a C extension using pybind11 with CMake. I used to do this with:
find_package(Python3 3.7 REQUIRED)
find_package(PythonLibs 3.7 REQUIRED)
without any issues. Now I need python 3.8 and ...
0
votes
1
answer
1k
views
How to build a python package with a c extension as a wheel for docker
I am trying to build a python package with a c extension to be used in a Docker container.
I create a python wheel as follows:
python install bdist_wheel
and the package looks (more or less) like
...
2
votes
0
answers
530
views
What is the proper way to structure a Python package with extensions?
Question:
What is the proper way to structure and build a Python wheel with C extensions for deployment? Ideally, the user shouldn't need elevated privileges or be required to point environment ...
1
vote
0
answers
147
views
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 ...
8
votes
2
answers
10k
views
How to pass --debug to build_ext when invoking setup.py install?
When I execute a command python setup.py install or python setup.py develop it would execute build_ext command as one of the steps. How can I pass --debug option to it as if it was invoked as python ...
2
votes
3
answers
521
views
How to extend Python and make a C-package?
I embedded and extended Python 2.7 in my C application a while ago. Late on the train I am bringing it to Python 3, and a lot of initializations for the module registration changed for me.
Before I ...
4
votes
3
answers
8k
views
Debugging Pybind11 extension with Visual Studio Code (MacOS)
I've been using pybind11 recently, and now that I'm getting the hang of it, I'm thrilled with it. It's an awesome piece of work. The final piece of the tool puzzle for doing pybind11 is the debug part....
5
votes
1
answer
10k
views
CMake pybind11 cannot create target because another target with the same name already exists, how to bypass?
I have code which successfully runs. Its CmakeLists.txt is:
cmake_minimum_required(VERSION 3.15)
project(rotapro3)
set(CMAKE_CXX_STANDARD 14)
add_executable(rotapro3 main.cpp):
I want to use pybind ...
0
votes
0
answers
141
views
When I run my python code in VS code it displays the output in the Terminal tab and not the Output tab
Currently using VS Code version 1.43.2 and Python 3.8.1. I want to know how will I make my codes display in the Output section and NOT the Terminal section when I ran them
-1
votes
1
answer
42
views
(python) print keep saying 'None'
i'm making a some code for project, the all i want to do in test function is i want to print out
each of 'list_total[y]' values i input
input example
1 # forget this input for now,
1 # the lines ...
0
votes
1
answer
318
views
Proper way to call a different method from the same C-extension module?
I'm converting a pure-Python module to a C-extension to familiarize myself with the C API.
The Python implementation is as follows:
_CRC_TABLE_ = [0] * 256
def initialize_crc_table():
if ...
2
votes
0
answers
172
views
Cython .pxd not found after setting language_level parameter or compiler directive [duplicate]
I have a what looks like this in some directory.
.
├── Makefile
├── mod
│ ├── advancedmath.cpp
│ ├── advancedmath.py
│ ├── __init__.cpp
│ ├── __init__.py
│ ├── main.cpp
│ ├── main.pxd
...
1
vote
2
answers
622
views
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 ...
1
vote
1
answer
192
views
Compiling python3 C extensions
I wrote the following code for my CPython extension :
#include <Python.h>
static PyObject *greeting(PyObject* self)
{
return Py_BuildValue("s" , "Hello python modules!!!");
}
static ...
11
votes
2
answers
807
views
Create Python C extension using MacOS 10.15 (Catalina) that is backwards compatible (MacOS10.9+)
How can I create a Python C extension wheel for MacOS that is backwards compatible (MacOS 10.9+) using MacOS 10.15?
This is what I have so far:
export MACOSX_DEPLOYMENT_TARGET=10.9
python -m pip ...
1
vote
0
answers
534
views
Passing double arrays from Python to C++ extensions
I understand a basic C++ function wrapped for Python looks like this:
int square(int n)
{
return n*n;
}
static PyObject* square_wrapper(PyObject* self, PyObject* args)
{
int n = 0;
if(!...
1
vote
1
answer
3k
views
Passing byte string from Python to C
I am writing a python extension in C and I am trying to pass a bytes object to my function. Obviously the 's' token is for strings; I have tried 'O', 'N', and a few others with no luck. Is there a ...
0
votes
1
answer
1k
views
Linting in Visual Studio Code Python stymies real time error detection
I've used other editors (Spyder) where linting is real time, and it's incredibly useful to catch coding errors in real time. I'm not necessarily looking for real time, but even after I save, I have to ...
0
votes
1
answer
193
views
Getting a SIGSEGV when calling python3 extension module function operating a Py_buffer
I'm toying around with Python C extension modules and I've got this simple function:
static PyObject *e_parse_metadata(PyObject *self, PyObject *args) {
Py_buffer buf;
if(!PyArg_ParseTuple(...
5
votes
1
answer
2k
views
Cython C-level interface of package: *.pxd files are not found
In a nutshell
I try to compile a cython extension called extension2 that cimports a file extension from a self-created package. When building extension2, I get the error that extension.pxd is not ...
0
votes
1
answer
350
views
How can I force setuptools detect that my package is platform specific? [duplicate]
I'm working on improving the setup.py script for an open source package that supports various platforms.
On Linux, the package defines a setuptools.Extension for some C code that needs to be built ...
0
votes
1
answer
125
views
Python application leaking memory, but Valgrind says no
I have a long-running Python app which scans many megabytes of files in a loop every few minutes. Over the course of a day I see that it gobbles up gigybates of memory, and in the end I have to kill ...
0
votes
1
answer
25
views
How to build an extension in Python debugging environment?
I'm trying to debug a Python extension that I wrote. In order to enable some memory debugging features I downloaded and built a Python version, which worked just fine. Now I'm trying to build my own ...
1
vote
1
answer
434
views
Why does this Py_DECREF cause a segfault?
I'm chasing an annoying segfault bug in a Python extension. Drilling down to the core I first created a standalone C version of the extension, and while trying to further reduce the problem, I've ...
1
vote
1
answer
381
views
Need guidance regarding reference counting
I'm chasing a memory leak that seems to come from a long-running process which contains a C extension that I wrote. I've been poring over the code and the Extensions docs and I'm sure it's correct but ...
1
vote
0
answers
74
views
"corrupted double-linked list" after C++ python extension program terminates
I am developing a program that extends python with C++ code, and it looks like I'm missing something in initialization or cleanup. The symptom is that when the program terminates it prints out ...
4
votes
2
answers
961
views
INCREF needed when returning argument from Python C Extension function?
This question is pretty simple but will help cement my understanding. I know that arguments to C extension functions are guaranteed to be live references for the duration of the C code (unless ...
0
votes
0
answers
49
views
I had installed python extension (windows) but unable to see python in the right menu in integrated terminal(VS Code)
I had installed python extension (windows) but unable to see python in the right menu in integrated terminal(VS Code). I can only see powershell unlike the tutor(Mosh Hamedani) has python.please see ...
6
votes
0
answers
889
views
Step from pdb in gdb when debugging c extension
I'm developing a C(++) extension for python. I have a library in C that I wrap using Swig. Unfortunately I have some error that I would like to debug that is within the C extension. My program makes ...
0
votes
1
answer
204
views
Is Python GIL needed if extension is single threaded
I am writing a C++ extension for my Python application. I understand that Python GIL is used to prevent multiple thread accessing PyObject at the same time. However, my questions is my extension code ...
3
votes
1
answer
335
views
python3 C extension module with cp1252 encoded string
I am writing a Python3 extension module for an existing C++ library which returns a string that appears to be in cp1252 encoding. The C++ function signature is
int get_name(std::string& name);
...
1
vote
1
answer
425
views
How would I create a Python library that is extended by using C?
My goal is to create a Python library that I can import into any of my projects, and I wish to have the library made faster by the use of the C language (similar to how NumPy works).
However, after ...
0
votes
1
answer
113
views
Ship Python Extension that required dynamic linking
I am writing a c extension (called my_ext) and I successfully compiled the source code to a .so file. However, my_ext depends another dynamic library installed by pip. So the link directory looks like ...
3
votes
1
answer
349
views
Segmentation fault in C python extension
I'm writing a C extension module for python3.7. I have a simple structure as PyObject:
typedef struct {
PyObject_HEAD
double attacker;
double victim;
double game_hardness;
int ...
0
votes
1
answer
2k
views
How to convert PyObject to UTF-8 string?
I'm trying to pass a string value to a Python function and get a utf-8 string value. The function retrieves Japanese characters and returns characters in another language.
I used ctypes.windll.user32....
5
votes
1
answer
468
views
Python C Extension Missing Function
While following a C extension for Python tutorial, my module seems to missing its contents. While building and importing the module have no problem, using the function in the module fails. I am using ...
2
votes
1
answer
413
views
Is the import order of extensions in module filenames guaranteed in Python?
Experimentally, I verified that when a compiled extension.pyd (or .so) and plain extension.py both exist in the same directory, the .pyd file gets imported first ; the .py is only imported if the .pyd ...
1
vote
1
answer
222
views
Why does PyArg_ParseTuple always return false?
My PyFLoat_ParseTuple call always return false, with correct call from python side.
I am working on wrapping my C++ for Python and using python extension.
I had a successful project, and generating ...
3
votes
0
answers
800
views
How to compile a C extension using clang or ICC?
I am trying to compile a python module using a compiler other than 'gcc', specifically 'icc' (for efficiency reasons).
I tried forcing the compilation setting 'cc' to 'clang' but still that did not ...
0
votes
3
answers
3k
views
Does `pip install` respect the compiler version used to build Python?
Does pip install always build extension modules with the same compiler that was used to compile the current Python version?
For example, this blog post explained that numpy package uses C code, which ...
1
vote
1
answer
2k
views
Can't correctly convert Numpy array to c array for Python Extension (c++)
I'm developing a Python extension in c++. I'm am really rusty in c++ however and don't have the necessary experience to figure this out it seems. I'm trying to read in numpy arrays, do the ...
-1
votes
1
answer
420
views
re-run Python file in visual studio code
I am working on data science using python and visual studio code but every time i run the code even the single line file starts from the begging which again upload the data and give the result which ...
0
votes
0
answers
860
views
PyMemoryView_FromMemory Losing Data
I'm currently writing some code that utilizes boost.python. I want to convert an std::vector<unsigned char> to a Python bytes-like object, (I just need to expose the data to Python). The ...
0
votes
1
answer
638
views
Why relasing and acquiring GIL within two threads causes an application crash?
I have developed a Python extenstion using C++. The only function of this module is something like this:
static PyObject *TestModule_API1(PyObject *self, PyObject *args)
{
PyThreadState *_save;
...
3
votes
1
answer
779
views
How to pass a large string from Python to a C++ extension method efficiently?
Introduction
I am working on project where a lot of textual data needs to be processed. Many quite big (hundreds of MB) text files. The python is the requirement (don't ask why). I want to use C++ ...
1
vote
0
answers
777
views
How to free memory early for a PyObject?
I have a large (numpy ndarray) object whose memory I want to free as soon as I am finished with it in my Python extension (or I will quickly run out of memory). How can I safely do this?
It seems ...
1
vote
2
answers
2k
views
Creating .so file using ctypes when opencv is included in C++ code
I am trying to create .so file of a c++ file(twocams.cpp) which includes main() and another C++ file (say abc.h). abc.c includes opencv. while creating an object using ctypes,
g++ -fPIC -shared ...
1
vote
2
answers
1k
views
Python extension in C - Metaclass
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....
-1
votes
1
answer
89
views
python random function not working , I am very new at python
I have used a random function in python but its not working , I am very new at python. Please review below code
def get_context_data(self, **kwargs):
context = super().get_context_data(**...
2
votes
1
answer
561
views
Compiled boost_python extension fails to import in python 2.7
I am trying to import a python extension coded in c++ with boost. While I experienced some problems compiling the extension using cmake, I managed to do so linked to the boost_python27 library. I then ...