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

Taking arguments

I am trying to create a Python extension that takes arguements. This is my script: #include static PyObject * _message(PyObject *self, PyObject *args) { char *title, *message; if (!PyArg_ParseTuple(args, &title, &message)) …
Xantium
  • 11,201
  • 10
  • 62
  • 89
0
votes
0 answers

Is there a Skein implementation that works on on AWS Lambda?

Is it possible to implement a Skein tree hash across AWS lambda functions? In other words using Lambda functions for parallelism.
0
votes
1 answer

Using PyModule_AddIntConstant() in an extension

I have seen Adding symbolic constants with hex values to Python extension module and I am trying to reproduce this effect: #include #include static PyObject * sys_shutdown(PyObject *self, PyObject *args) { int val; …
Xantium
  • 11,201
  • 10
  • 62
  • 89
0
votes
1 answer

Python 2.7 API PyImport_ImportModule() returns NULL if script includes dlopen()

I am using Python C API to load some scripts, however, on macOS X I noticed that if the script is including dylib such as import datetime then it would fail: #include #ifdef __APPLE__ #include…
PerfectlyRock
  • 405
  • 2
  • 7
0
votes
1 answer

Zero reference count and still no segmentation fault

I have developing skills in Python/C API and memory allocation, and was expecting the following Python-embedded C++ code to be problematic and lead to something like a segmentation fault: #include #include int main(){ …
jduriez
  • 15
  • 2
0
votes
1 answer

why can't using loading python27.dll without above Python27/lib py files

I have a vc6 application and using python27.dll. if the runtime environment have python27/lib/* files, it's be ok. but no python27/lib/* files,the application will termination. I have using Process Monitor and watch process will load many py. (like…
ypochien
  • 23
  • 6
0
votes
1 answer

Should I use Py_INCREF for any of my PyObjects in this block? Also am I Py_DECREFing my objects correctly?

Whenever I call this function, memory usage is increases a lot per call, so I think there is some memory leak here. PyObject *pScript, *pModule, *pFunc, *pValue; PyObject *pArgs = NULL; long ret = 1; // Initialize python, set system path and load…
Venkat Krishnan
  • 107
  • 1
  • 2
  • 8
0
votes
1 answer

How to get installed python package folder path?

How to get installed python package folder path in Python C API? Suppose I want to open a file data.txt from the Python C-API module.pyd that resides as follows: package |--module.pyd |--data | |-data.txt How do I get the path name of…
user62039
  • 371
  • 2
  • 15
0
votes
1 answer

Python C API. Pass pointer to C++ function to existing Python file

I Have a C++ code that calls a function named "function" of a python script named "filename". PyObject module= PyImport_ImportModule("filename"); PyObject *python_fn = PyObject_GetAttrString(module, (char *)"function"); PyObject *args =…
Halbarad
  • 73
  • 1
  • 5
0
votes
1 answer

Call a python method from C/C++, and fail in access violation exception

We wanna call a c/c++ extension in python, and in the extension we need to callback one function in the python script. Here is the python code # -*- coding: UTF-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import ctypes class…
Azure
  • 154
  • 1
  • 10
0
votes
1 answer

passing 1 or 2 d numpy array to c throw cython

I am writing an extension to my python code in c and cython, by following this guide. my c function signature is void c_disloc(double *pEOutput, double *pNOutput, double *pZOutput, double *pModel, double *pECoords, double *pNCoords, double nu, int…
Yohai Magan
  • 279
  • 1
  • 12
0
votes
1 answer

Python3 c api - python stops working

I was trying to get a list as an input. I've googled too. I built and installed my C-extension module in the command prompt .I am able to import the module but it did show few warnings.What it had shown me was..... At the CMD D:\Python\c\summa>py…
saiyan
  • 3
  • 5
0
votes
1 answer

Incorrect message length when using C python api

I'm trying to extend libhydrogen (open source found here: https://github.com/jedisct1/libhydrogen/) to support calls to the underlying library in Python. However, I ran into a strange issue. Specifically I have the following code: static PyObject*…
0
votes
0 answers

Passing a structure of data from a C library to Python

I am trying to write a C library for python that will return some data about a value that is passed to it in string from. The returned data is in a c structure format and I need to get this information back to python to use it. So far my research…
rutri
  • 13
  • 4
0
votes
1 answer

Embedding python in c++: Segmentation fault

I am remote Debugging a c++ application with Visual studio on a linux device (raspberry pi/raspbian). In this c++ application I embedded a simple Python script by loading the function using the Python/c api. This is my c++ Code: #include…