Questions tagged [python-c-api]

API used by C and C++ programmers who want to write extension modules or embed Python.

The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. This is probably the most common use. The second reason is to use Python as a component in a larger application; this technique is generally referred to as embedding Python in an application.

Writing an extension module is a relatively well-understood process, where a “cookbook” approach works well. There are several tools that automate the process to some extent. While people have embedded Python in other applications since its early existence, the process of embedding Python is less straightforward than writing an extension.

Many API functions are useful independent of whether you’re embedding or extending Python; moreover, most applications that embed Python will need to provide a custom extension as well, so it’s probably a good idea to become familiar with writing an extension before attempting to embed Python in a real application.

Reference: http://docs.python.org/c-api/intro.html

1096 questions
-1
votes
1 answer

How to convert PyObjects to C data types?

I am working on a project where I have to convert a user input C++ string variable to python executable code, I'm using the Python C API, so I have to go through the intermediary of converting the C++ string to a C string (or character array). Once…
-1
votes
1 answer

build C extension for python——'undefined reference to _Py_GC_generation0'

The background information is that I want to implement my own HashMap object by pyhton for C extension. But When I finished 'hashmap.c' and used 'distutils' to compile it as a module, I found something wrong with 'undefined reference to…
-1
votes
1 answer

"The system cannot find the file specified" when trying to build python c extension

I'm new to python c extension. I'm copying a basic example that prints "hello world". But the system keeps saying "error: [WinError 2] The system cannot find the file specified". setup.py: from distutils.core import setup, Extension setup(name =…
Jason Guo
  • 9
  • 3
-1
votes
2 answers

Windows: How does Jupyter throw a keyboard interrupt?

I know how to interrupt the kernel (such as by tapping I twice or by interrupting the kernel on the web interface). However, I built a C-extension for Python (I'm using Windows) that handles CTRL-C events in my C++ code (a toy example): static int…
user1691278
  • 1,751
  • 1
  • 15
  • 20
-1
votes
1 answer

Get list from Python in C extension

I'm trying to get a list as an argument in a C extension in Python, the function and the wrapper (Function's table is omitted): #include int MyFunction(int a_triplet[]){ printf("%i\n", a_triplet[0]); return 0; } static…
dfrojas
  • 673
  • 1
  • 13
  • 32
-1
votes
1 answer

PyObject_Call segfaults when invoked with bound method

PyObject_Call segfaults when it is called with an instance of a bound method, but works fine when invoked with regular (unbound) procedures or with an instance of a class that implements __call__, or with subclasses of type. Any reason it should…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
-1
votes
1 answer

Are there any datetime.tzinfo implementations in C?

I've been working on a Python library that uses a C extension module to do ISO 8601 parsing. Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Calls out to Python implementations of tzinfo…
movermeyer
  • 1,502
  • 2
  • 13
  • 19
-1
votes
1 answer

Wrong checksum calculated after migrating code to C extension for python 3

I am new to python C extension code and migrating old python 2.7 based c extension code to python 3.6 based c extension code using Visual Studio 2015. Steps in old code - Use METH_OLDARGS while initialising function Use…
sam
  • 11
  • 1
  • 5
-1
votes
1 answer

Trying to create a new type for python

I am trying to create a new type for python (2.5). I am trying to follow: python's documentation, however i am unable to compile the basic bit: #include typedef struct { PyObject_HEAD /* Type-specific fields go here. */ }…
cdvv7788
  • 2,021
  • 1
  • 18
  • 26
-1
votes
1 answer

Running a file with arguments from python embedded within c

I'm currently working on a project that uses a C source file that has to interact with a python file (run the file and capture output) and im not exactly sure how to do it. currently the python file is run through terminal (linux) using: python file…
EthanW05
  • 1
  • 1
-1
votes
1 answer

extending python using C extensions

I am trying to learn how to extend python using C extensions and so far I have been able to go through the official python docs for the same. Browsing through, I have found this helpful resource which explains how to extend python using C…
JohnJ
  • 6,736
  • 13
  • 49
  • 82
-2
votes
2 answers

What is the most efficient option to implement a C based library into my python code?

I have a moderate amount of experience in python and a little experience in C++ and c#. I am currently doing an optimization challenge where I am gated by efficiency, and am hoping to use a C library in python to increase efficiency. I have no…
-2
votes
1 answer

Can't pass arguments to Python function while embedding Python in C++

I'm embedding a Python module in my C++ code and I am using Python/C API. I need to call a Python module function and get the results. The function gets an unsigned integer and a double as input arguments and outputs a list. I use the following to…
Amir
  • 421
  • 1
  • 4
  • 14
-2
votes
1 answer

how to resize and subtract numpy arrays in c++

I have two numpy 3D-array in python with different height and width. I want to pass them to my C-Extension. How can I resize and subtract them in c++? Please see the comments in the code. static PyObject *my_func(PyObject *self, PyObject …
Iman
  • 424
  • 5
  • 18
-2
votes
1 answer

How to deal with multiple Data Type in cython?

What are the different techniques to deal with multiple data types in cython? How can I make the code generic for multiple data types and on the same hand maintain efficiency as well.
bewithaman
  • 768
  • 8
  • 16
1 2 3
73
74