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

Prevent delay while returning from C-Python api

I have C-Python api like below. It works fine, but the problem is while invoking this method from python script say In script.py: offset=0 size=4 write_object(offset,size) This calls write_this_c() C API and returns quickly to the next printf…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
0
votes
3 answers

is it possible to completly flatten a python package, removing directory completly?

I'm wondering because i'm having some massive issues with importing package modules into my embedded python enterpreter, regardless of sys.path . so for example. my package. program.py lib| Packz| - __init__.py …
flow
  • 571
  • 1
  • 4
  • 16
0
votes
1 answer

How does one produce a specific unicode character with Python's C-API?

I'm writing a Python extension that runs through a Py_UNICODE array, finds specific (ASCII, if it matters) characters, i.e. '\' or '\n', and does some additional stuff for each one that it finds. Is there a way to write those characters as literals?…
DNS
  • 37,249
  • 18
  • 95
  • 132
0
votes
2 answers

Embedding Python in C++ crashes during running-time

I've been around this question for way too long. Now I searched this and it was working for a while but it then crashed at a random place. I think that the problem is a deadlock or something. So please, someone tell me what I'm doing wrong: I make…
João Pereira
  • 673
  • 1
  • 9
  • 29
0
votes
1 answer

Struct variables overlap (Python C API)

Here's a strange one for you: I have been following the framework of this tutorial, in an attempt to write a Python module that will return a struct containing the results of a C function. The C struct is defined as such: typedef struct…
Ian
  • 71
  • 1
  • 5
0
votes
1 answer

Slowdown and high memory uasge when adding a new module in embedded Python 3.4 on 64bit Windows

I need to have my Python program access my C functions. So I used the example from the documentation to get started. Unfortunately, its very slow and it takes up all of my memory. I am using Windows 7 64bit, Python 3.4 and gcc (rubenvb-4.8.0)…
MrValdez
  • 8,515
  • 10
  • 56
  • 79
0
votes
1 answer

migrating from python 2 to python 3 - embedding issues

i am migrating an application that embeds python, from version 2.7 to version 3.3. The application makes functions available to script, by calling Py_InitModule() with the appropriate data. Just to annoy poor guys like me, the api was dropped in…
bernd feinman
  • 324
  • 2
  • 11
0
votes
1 answer

How to chose python executable if several exist?

I would like to execute Python scripts using the Python/C API (I am using Mac OS X Mavericks, Xcode). Since multiple Python distributions are installed on my machine (System, Homebrew, several virtualenvs), how can I define which to use?
DaPhil
  • 1,509
  • 4
  • 25
  • 47
0
votes
1 answer

receive wrong answer using python/c extension

I want make easy module with c-extension for python. I want just add two double values. But receive wrong result value. I can't find a mistake. my files: add.c #include "add.h" double add(double a, double b){ return a+b; } add.h double…
Nikolas
  • 2,322
  • 9
  • 33
  • 55
0
votes
1 answer

Name of variable passed as argument

I want to do something along the lines of static PyObject* printArgs(PyObject* self, PyObject* args) { PyObject * oarg1 = NULL; PyArg_ParseTuple(args,"O",&oarg1); return -- magic -- } Such that calling modulename.printArgs(a) returns…
user1294039
0
votes
1 answer

Python C build: "undefined symbol" errror, when code runs using gcc directly

I have a python extension written in c which compiles fine however barfs ImportError: /path/to/cmongo.so: undefined symbol: json_tokener_parse When I try to import the module (import cmongo) in python. What makes this strange is the the c compiles…
SColvin
  • 11,584
  • 6
  • 57
  • 71
0
votes
2 answers

Python C API and C++ functions

I'm trying to extend the Python interpreter in my C++ program, my problem is as follows. When I'm trying to call a function, explained in the code below, I get a NameError, from the Python interpreter. The error is Traceback (most recent call…
P.K.
  • 777
  • 1
  • 7
  • 18
0
votes
2 answers

Python and C extensions

This is an extension to my previous question found in C code within python and copying arrays in C code Serberg suggested I use PyArray_CopyInto functions from NUMPY. I am new to both python and C though I know a little bit of both. I am trying to…
Steve Grafton
  • 1,821
  • 4
  • 17
  • 18
0
votes
1 answer

Numpy C++: How to iterate over PyArrayObject without a segfault

For me, the following all result in a segfault: my_array->descr->subarray->shape; my_array->dimensions; PyArray_SHAPE(my_array); PyArray_DIMS(my_array); PyArray_ITEMSIZE(my_array); PyArray_NBYTES(my_array); My function looks like this: static…
Alex Eftimiades
  • 2,527
  • 3
  • 24
  • 33
0
votes
1 answer

Calling a Python function within a C++ program

I have a real simple Python function: def myfunc(x): return 2.0 * x I want to send this function to a C++ program and call it so I have done this: #include "Python.h" static PyObject *execMyPyFunc(PyObject *self, PyObject *args) { …
Joel Vroom
  • 1,611
  • 1
  • 16
  • 30