Calling C function from Python

user3554898

I made use of a server file which was written in Python to build connection between my Raspberry Pi and my iPhone. And i wrote a simple C program that help translate morse-code. I want to call the translate() function in the C program from the Python server program.

I found a tutorial online and followed its instructions to write my C program and edit the netio_server.py file

In my C program morseCodeTrans.c it's like

#include <Python.h>
#include <stdio.h>

static PyObject* py_translate(PyObject* self, PyObject* args)
{
char *letter;
PyArg_ParseTuple(args, "s", &letter);

if(strcmp(letter, ".-") == 0)
  return Py_BuildValue("c", 'A');
else if(strcmp(letter, "-...") == 0)
  return Py_BuildValue("c", 'B');
...

}

static PyMethodDef morseCodeTrans_methods[] = {
  {"translate", py_translate, METH_VARARGS},
  {NULL, NULL} 
};

void initmorseCodeTrans()
{
  (void)Py_InitModule("morseCodeTrans", morseCodeTrans_methods);
}    

And in the server file netio_server.py it's like:

# other imports
import morseCodeTrans

...

tempLetter = ''

if line == 'short':
  tempLetter += '.'
elif line == 'long':
  tempLetter += '-' 
elif line == 'shortPause': 
  l = morseCodeTrans.translate(tempLetter)
  print "The letter is", l

Above is the only place that I would call the C translate() function

Then I tried to compile morseCodeTrans.c file like this:

gcc -shared -I/usr/include/python2.7/ -lpython2.7 -o myModule.so myModule.c

The compile was successful. But when I ran the Python server program, whenever it reached the line

l = morseCodeTrans.translate(tempLetter)

The server program just terminated without any error message.

I'm very new to Python programming, so I couldn't figure out where the problem is. Any help?

Stefan

You just got some minor mixups in the interface. I modified the code as follows to make it work:

#include <Python.h>
#include <stdio.h>

static PyObject* py_translate(PyObject* self, PyObject* args)
{
  char *letter;
  PyArg_ParseTuple(args, "s", &letter);

  if(strcmp(letter, ".-") == 0)
    return Py_BuildValue("c", 'A');
  else if(strcmp(letter, "-...") == 0)
    return Py_BuildValue("c", 'B');
  /* ... */
  else
    Py_RETURN_NONE;
}

static PyMethodDef morseCodeTrans_methods[] = {
  {"translate", py_translate, METH_VARARGS, ""},
  {0} 
};

PyMODINIT_FUNC initmorseCodeTrans(void)
{
  Py_InitModule("morseCodeTrans", morseCodeTrans_methods);
}

It's much safer to build with distutils, since it prevents you from accidentally linking against wrong versions of Python. Use the following setup.py:

from distutils.core import setup, Extension
module = Extension('morseCodeTrans', sources=['morseCodeTrans.c'])
setup(ext_modules=[module])

Simply use as python setup.py install.

Edit

Despite the mixups in the interface, it appears your code still works. Then your problem is most likely during linking. As mentioned above, using distutils should remedy this. If you absolutely want to build manually, use python-config --cflags, python-config --ldflags etc. to make sure you link against the correct version of Python.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Python function from C++

From Dev

Speed costs of calling Python function from C extension

From Dev

Speed costs of calling Python function from C extension

From Dev

Python calling function in an function from another function

From Dev

Calling an assembly function from C

From Dev

calling c function from MATLAB?

From Dev

calling c function from MATLAB?

From Dev

Calling a function from within a function in Python

From Dev

Calling main function from another function in C

From Dev

Calling main function from another function in C

From Dev

Python: calling function from imported file

From Dev

Calling python function from shell script

From Dev

Calling python dictionary of function from class

From Dev

python - calling a function from within itself

From Dev

Calling a Python function from another file

From Dev

Calling A function from multiple scripts in python

From Dev

python calling custom function from bash shell

From Dev

Python - Calling a function from another class

From Dev

Python calling a function from another file

From Dev

Python: calling function from imported file

From Dev

Calling variable from another function in python

From Dev

Python - Calling a function from another class

From Dev

Python: Problems in calling the function from the while loop

From Dev

python - calling variable from another function

From Dev

Python: Calling function from an active class

From Dev

Calling a D function directly from C++

From Dev

Calling a void* function from main in C

From Dev

Calling a function from a struct (C/Assembly)

From Dev

Calling an F# Function from C#