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
0
votes
2 answers

How to improve Python C Extensions file line reading?

Originally asked on Are there alternative and portable algorithm implementation for reading lines from a file on Windows (Visual Studio Compiler) and Linux? but closed as too abroad, then, I am here trying to reduce its scope with a more concise…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
0
votes
2 answers

Python C++ API - overloading functions with variable number of arguments

I am trying to overload the following sayHi function, that is meant to receive a char* as input argument, or, alternatively, a char* and an integer. It is part of class Box (which itself is used to define the python object PyBox): class Box { …
PintoDoido
  • 1,011
  • 16
  • 35
0
votes
1 answer

Python C++ api - return different types in function overloading

I have a C++ class that contains two overloaded methods sayHi(), each with a different return type: class Box { public: Box(); void sayHi(char *name); int sayHi(int number); }; When I tried to wrap this functions, I…
PintoDoido
  • 1,011
  • 16
  • 35
0
votes
1 answer

Python C++ api: How to access public class attribute?

I have a C++ class Box that I want to wrap up using python C++ api. The class is defined as: class Box { public: int id; Box(double l, double b, double h); double getVolume(void); void setLength( double len ); …
PintoDoido
  • 1,011
  • 16
  • 35
0
votes
1 answer

PyRun_SimpleString failing for special characters

I was trying Basics embedding Python in C++ and when i run this code i get error "SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf4 in position 10 : invalid continuation byte". I am not able to figure out what's wrong in this. I…
Devious
  • 9
  • 8
0
votes
1 answer

GMPy - type checking in python C-api

I am trying to understand how to type check using python C-api. To achieve this, I am studing the source code of GMPy, where they check if the argument to a function is an integer using the macro IS_INTEGER(x), which itself is defined based on a…
PintoDoido
  • 1,011
  • 16
  • 35
0
votes
1 answer

Which members of PySequenceMethods are optional?

I'd like to allow my extension objects to be passed into Python's len() function, and I understand the way to do this is to implement the sq_length member of PySequenceMethods, but right now I don't have time to implement all the methods of the…
Rich
  • 7,348
  • 4
  • 34
  • 54
0
votes
1 answer

Python C API: PyDateTime_FromTimestamp causes segmentation fault

I followed this answer to call PyDateTime_FromTimestamp to create a datetime object in C++. But I got a Segmentation fault when PyDateTime_FromTimestamp is called. Here is my C++ code: #include #include #include…
Cosmo
  • 836
  • 1
  • 12
  • 27
0
votes
0 answers

Py_DECREF on Temporary Variables in Functions

According to the accepted answer in Py_INCREF/DECREF: When, Python objects that are created by functions but not explicitly returned should have their reference counts decremented via DECREF. Does this guideline apply to temporary variables? For…
porzchop
  • 1
  • 1
0
votes
2 answers

How to access sys.stderr after calling PyErr_Print with python c-api

I am trying to use the python c-api from a c# project via dll import. I am getting an ModuleNotFoundError when importing some modules, which I thought are builtin. (Note: I compiled python myself) I am a bit stuck right now, but my hope is to get…
TruckerCat
  • 1,437
  • 2
  • 16
  • 39
0
votes
0 answers

How to create an instance of a Python class in C/C++ API?

I want to create an instance of a PyTypeObject in my C++ code Python C-api: I used Create an object using Python's C API as a basis. I have defined a custom type/class: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; static…
PintoDoido
  • 1,011
  • 16
  • 35
0
votes
1 answer

Using the Python C API, how can I write a function that accepts any number of arguments, including none at all?

METH_VARARGS requires at least one argument; METH_NOARGS doesn't seem to let me pass any at all. How can I define a function build() that can be called as either build() or build(True)/build(False)? Calling a METH_VARARGS function with no arguments…
Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61
0
votes
0 answers

dynamic module does not define init function with python 2.2 only

I have a problem with the dynamic error module does not define init function (initsomething). Everything is fine when I use python 2.7, the problem only occurs with compilation using python 2.2 The output format is .pyd. My code in c++ #ifndef…
0
votes
1 answer

Calling a Python class method from C++, if given an initialised class as PyObject

I have a function in c++ that receives a initialised class as a PyObject. The python class is: class Expression: def __init__(self, obj): self.obj = obj def get_source(self): #Check if the object whose source is being…
Methodicle
  • 27
  • 8
0
votes
2 answers

Python C api Exception class inside class

I'm trying to throw exception which is defined inside class in extension module created with python C api. Here is what I want in python C api, but written in python: class Graph: class TooManyVerticesError( Exception ): pass def…