C API from Extension Module in Python | Set 2

Prerequisite: C API from Extension Module in Python | Set 1

Let’s see an example of a new extension module that loads and uses these API functions that we build up in the previous article.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include "pythonsample.h"
/* An extension function that uses the exported API */
static PyObject *print_point(PyObject *self, PyObject *args)
{
    PyObject *obj;
    Point *p;
    if (!PyArg_ParseTuple(args, "O", &obj))
    {
        return NULL;
    }
    /* Note: This is defined in a different module */
    p = PyPoint_AsPoint(obj);
    if (!p)
    {
        return NULL;
    }
    printf("%f %f\n", p->x, p->y);
    return Py_BuildValue("");
}
  
static PyMethodDef PtExampleMethods[] =
{
    {"print_point", print_point, METH_VARARGS, "output a point"},
    { NULL, NULL, 0, NULL}
};
  
static struct PyModuleDef ptexamplemodule =
{
    PyModuleDef_HEAD_INIT,
    /* name of module */
    "ptexample"
    /* Doc string (may be NULL) */
    "A module that imports an API"
    /* Size of per-interpreter state or -1 */
    -1, 
    /* Method table */
    PtExampleMethods 
      
};

chevron_right


 
Code #2 : Module initialization function

filter_none

edit
close

play_arrow

link
brightness_4
code

PyMODINIT_FUNC
PyInit_ptexample(void)
{
    PyObject *m;
      
    m = PyModule_Create(&ptexamplemodule);
      
    if (m == NULL)
        return NULL;
      
    /* Import sample, loading its API functions */
    if (!import_sample())
    {
        return NULL;
    }
      
    return m;
}

chevron_right


Now to compile this new module, one need not bother about how to link against any of the libraries or code from the other module. One can just simply use work.py file as shown below.



Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# setup.py
from distutils.core import setup, Extension
  
# May need pythonsample.h directory
setup(name ='ptexample'
      ext_modules = [ Extension('ptexample'
                     ['ptexample.c'], include_dirs = [], )])

chevron_right


After performing all this task, this new extension function works perfectly with the C API functions defined in the other module.

Code #4 : Using CPI API functions defined in the other module

filter_none

edit
close

play_arrow

link
brightness_4
code

import ptexample
import work
  
point1 = work.Point(2, 3)
print ("Point_1 : ", point1)
  
print ("\n", ptexample.print_point(p1))

chevron_right


Output :

Point_1 : 

2.000000 3.000000


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.