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
2
votes
1 answer

Hashing numpy object array, how does hashlib see objects' contents and not just pointers?

I am wondering, how come hashing e.g. strings in an np object[] produces expected results: >>> hashlib.sha256(np.array(['asdfda'], dtype=object)).hexdigest() '6cc08fd2542235fe8097c017c20b85350899c81616db8cb59045022663e3cee1' >>>…
Adam
  • 1,724
  • 4
  • 21
  • 31
2
votes
1 answer

How do you make python recognize read a precompiled shared file?

I have a package I've created in C++ and, have already compiled it into a shared library. When I link against it with my own main function, I can initialize the package by calling the initialization function directly, initfoo, and everything works…
Juan
  • 3,667
  • 3
  • 28
  • 32
2
votes
1 answer

Can a native extension support multiple modules?

So in python native extension, is it possible to implement multiple modules in a single shared library? Then what would be the name of the shared library should be? PyMODINIT_FUNC PyInit_foo(void) { PyModule_Create(...); } PyMODINIT_FUNC…
fluter
  • 13,238
  • 8
  • 62
  • 100
2
votes
0 answers

Breaks when return np.array using MinGW32

I'm trying to build a python extension which has a function returns a np.array. But it breaks when compiled with MinGW32 but works with VS2019. I tried to comment the code setting the flag NPY_ARRAY_OWNDATA, then MinGW32 and VS2019 both work, but…
PX.Liu
  • 171
  • 1
  • 7
2
votes
1 answer

Binary Tree implemented in C using Python Api converts PyObject into Bytes

My PyObject becomes a Bytes object while getting it's value So lately, I am doing a project in C, where I implement a few type of trees, to be able to use them in python, since a C btree implementation is way faster than a Python one. (Ikr there are…
nagyl
  • 1,644
  • 1
  • 7
  • 18
2
votes
1 answer

Python/C++: can import Armadillo (arma::) but not subroutine arma::arma_rng::randn

QUESTION When creating a Python extension in C++ that uses Armadillo, I get the errors: A) In Mac OS Mojave 10.14.4, Python 3.7.5: Traceback (most recent call last): File "./py_program.py", line 5, in import cmodule ImportError:…
Godoy
  • 87
  • 2
  • 9
2
votes
1 answer

How to download Python.h header file on Mac?

I am trying to write Python C extensions and I'm on a mac. I know how to install the Python.h header file on Linux, but I don't know how to do it on a Mac. How can I install it?
Serket
  • 3,785
  • 3
  • 14
  • 45
2
votes
2 answers

Python C extension function that accepts optional integer

I want to implement a following Python function in a C extension module: def value(x: Optional[int] = None) -> Optional[int]: if x is None: # act like a getter return APIGetValue() # retrieve the value from an external library …
hoefling
  • 59,418
  • 12
  • 147
  • 194
2
votes
1 answer

Have C extension class inherit from Python class

I have a Python class AbstractFoo which uses @abc.abstractmethod to define 2 abstract methods. For more performance the bulk of the work is implemented in C as a Python extension class CFoo, so what I wanted to do is to inherit that extension class…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
2
votes
2 answers

Python C API: why doesn't PyRun_String evaluate simple conditional expressions?

PyRun_String("if True: 1\nelse: 0", Py_eval_input, globals, globals); returns error with PyErr_Print() printing out: File "", line 1 if True: 1 ^ What am I doing wrong? Thank you.
David
  • 65
  • 4
2
votes
2 answers

Use Python C-API to get the current size of value stack

I'm playing with Python's C-API. Specifically, I want to see if I can inspect how many elements the value stack currently has. Here's my code: #include #include int test() { PyFrameObject* f = PyEval_GetFrame(); …
laike9m
  • 18,344
  • 20
  • 107
  • 140
2
votes
1 answer

Getting undefined reference when trying to build a pybind11 project using gcc

I am trying to build a C++ static library in Linux (Ubuntu 18.04 in my case) using GCC using a Makefile. I noticed the issue is not with the makefile itself but the way I'm trying to compile and build with GCC. Before I explain a bit more on the…
Hossein
  • 24,202
  • 35
  • 119
  • 224
2
votes
3 answers

Variadic macro wrapper that expands to format string with characters corresponding to number of arguments

Question I am looking for a variadic C preprocessor macro that passes its argument and a corresponding format string to a function, repeating a character depending on the number of arguments. For example, I would like a macro FOO which expands as…
Wrzlprmft
  • 4,234
  • 1
  • 28
  • 54
2
votes
1 answer

How to get object ID using C API in Python?

I wonder - is there a canonical way of getting object's ID using CPython's C API?
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
2
votes
1 answer

Numpy multi-slice using C API?

I know how to get a slice from a Numpy array using the C API this way: // 'arr' is a 2D Python array, already created PyObject pyArray = PyArray_FromAny(arr, PyArray_DescrFromType(NPY_FLOAT), 0, 0, NPY_ARRAY_DEFAULT, null); PyObject…
fred87100
  • 33
  • 2