Helloworld with Python C extension

Ganesh Pal ganesh1pal at gmail.com
Mon Aug 29 13:30:35 EDT 2016


 Hello Team ,

I need you input on the below  hello world program.  I a m trying to add a
python binding which will return the character for the given index .  I am
on Python 2.7   and linux

Example :
>> string ='helloworld'
>>dda_hello(5)
>> 'w'


 /*
+ * Hello world example for python bindings
+ */
+
+static char* string = "helloworld";
+char dda_hello(int i)
+     {
+      return string[i];
+     }
+
+static PyObject *
+py_dda_hello(PyObject *self, PyObject *args )
+{
+       int index;
+       char char1;
+       if (!PyArg_ParseTuple(args, "i", &index))
+               return NULL;
+       char1 = dda_hello(index);
+       return Py_BuildValue("s",char1);
+}
+
+/*

@@ -1674,6 +1705,10 @@ PyMethodDef xyz_methods[] = {
+        {"dda_hello", py_dda_hello, METH_VARARGS,
+                "Returns the character entered for a given index"},


>>>  import as.ds.dss as daa
>>> print dda.dda_hello(1)
zsh: segmentation fault (core dumped)  python

Apologies for posting the diff  , I didn't find a better way

Regards,
Ganesh



More information about the Python-list mailing list