My notes on extending Python with C/C++

  1. Without third party tools : Using C and C++
  2. With third party tools:
    1. Cython
    2. cffi
    3. SWIG
    4. Numba

Extending Python with C or C++:

Prerequisites :

  1. Programming in C
  2. Basic knowledge of Python

Functions performed by extension modules in C and C++:

  1. Implement new built-in object types
  2. Can call C library functions and system calls

All user visible entities are defined by “Python.h” have prefix Py or PY. “Python.h” imports stdio.h, string.h, errno.h and stdlib.h automatically.

static PyObject * spam_system( PyObject * self, PyObject * args )
{
    const char * command;
    int sts;

    if(!PyArg_ParseTuple(args, "s", &command))
       return NULL;

    sts = system(command)
    return PyLong_FromLong(sts);
}

Here PyArg_ParseTuple() <-- checks the argument types and converts them to C values.
It returns True if arguments are of right type (nonzero), otherwise return False(zero)
if arguments list is invalid.

 

Advertisements

Author: scorpiocoder

I am a 3rd year, IIIT-Chittoor student enthusiastic and confused about various software technologies : Python, Machine Learning, Networks, Natural Language Processing and Android Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s