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

Why does Python keep a reference count on False and True?

I was looking at the source code to the hasattr built-in function and noticed a couple of lines that piqued my interest: Py_INCREF(Py_False); return Py_False; ... Py_INCREF(Py_True); return Py_True; Aren't Py_False and Py_True global values? …
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
23
votes
2 answers

How does PyArg_ParseTupleAndKeywords work?

I've been trying to learn how to write C-extensions for Python and want to be sure I understand how PyArg_ParseTupleAndKeywords works. I believe that the first argument is a PyObject pointer that points to an array of the arguments being passed…
user1245262
  • 6,968
  • 8
  • 50
  • 77
21
votes
7 answers

Python for .NET "unable to find assembly" error

I'm using CPython and I have a C# dll. I'm trying to use Python for .NET to make them talk. I can't use IronPython because I need to integrate this into an existing CPython system. I'm completely new to Python for .NET, and I actually have very…
Annie
  • 335
  • 1
  • 2
  • 6
20
votes
2 answers

Extending python with C: Pass a list to PyArg_ParseTuple

I have been trying to get to grips with extending python with C, and so far, based on the documentation, I have had reasonable success in writing small C functions and extending it with Python. However, I am now struck on a rather simple problem -…
JohnJ
  • 6,736
  • 13
  • 49
  • 82
20
votes
2 answers

How do I properly use Python's C API and exceptions?

if I do something like >>> x = int(1,2,3,4,5) I immediately get a fatal error (one that would end program execution if it was in a pre-written script) Traceback (most recent call last): File "", line 1, in TypeError: int()…
dejay
  • 758
  • 2
  • 6
  • 18
18
votes
3 answers

Calling python method from C++ (or C) callback

I am trying to call methods in a python class from C++. The C++ method from which this is called is a C++ callback. Within this method when I am trying to call python method, it was giving segmentation fault. I have saved an instance of python…
Chaitanya
  • 3,399
  • 6
  • 29
  • 47
17
votes
1 answer

How to get reference count of a PyObject?

How to get reference count of a PyObject from C++? There are functions Py_INCREF and Py_DECREF which increase/decrease it, but I haven't found any function which return object's reference count. I need it for debugging purposes.
DLunin
  • 1,050
  • 10
  • 20
16
votes
1 answer

call C++ using Eigen Library function in python

I'm doing some calculations in C++ with help of Eigen Library, the function is like this: MatrixXd Cov(MatrixXd Data) { VectorXd meanVector; ... return Covariance; } ..in the wrap python function: static PyObject *Wrap_Cov(PyObject…
Ysée
  • 173
  • 1
  • 4
16
votes
2 answers

Python C extension: method signatures for documentation?

I am writing C extensions, and I'd like to make the signature of my methods visible for introspection. static PyObject* foo(PyObject *self, PyObject *args) { /* blabla [...] */ } PyDoc_STRVAR( foo_doc, "Great example function\n" …
Nicolas Dumazet
  • 7,147
  • 27
  • 36
16
votes
2 answers

Python C-API functions that borrow and steal references

The standard convention in the Python C-API is that functions do not steal references from input arguments (that are objects) return values and output arguments (that are objects) own a reference Most functions in the Python C-API follow this…
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
15
votes
2 answers

How to create a custom Python exception type in C extension?

I'm writing a Python module in C. I need to report errors that can't be described by built-in Python exceptions. I therefore wish to throw exceptions of my own type. The problem is, that Python policy is to derive all exceptions from BaseException…
Basilevs
  • 22,440
  • 15
  • 57
  • 102
15
votes
3 answers

What is the purpose of Py_DECREF and PY_INCREF?

I was going through the tutorial for defining 'new types' in python, https://docs.python.org/2/extending/newtypes.html, and I did not understand the purpose of using Py_DECREF in this piece of code. static PyObject * Noddy_new(PyTypeObject *type,…
Nihal Harish
  • 980
  • 5
  • 13
  • 31
15
votes
1 answer

C Python: Running Python code within a context

The Python C API function PyEval_EvalCode let's you execute compiled Python code. I want to execute a block of Python code as if it were executing within the scope of a function, so that it has its own dictionary of local variables which don't…
Channel72
  • 24,139
  • 32
  • 108
  • 180
14
votes
1 answer

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 the regular mechanism for inheritance using…
14
votes
1 answer

Is there any benefit to using Py_DECREF instead of Py_XDECREF for Python C Extensions?

I'm working through the Python C Extension documentation for defining new types and just finished the Providing finer control over data attributes section. In this section, they change the example code to ensure that the first and last attributes of…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
1
2
3
73 74