Python API: Add support for shared memory prefix
[vpp.git] / vpp-api / python / vpp_papi / pneum_wrap.c
index 09d972d..18c4f23 100644 (file)
@@ -1,5 +1,20 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <Python.h>
-#include "pneum.h"
+#include "../pneum/pneum.h"
 
 static PyObject *pneum_callback = NULL;
 
@@ -12,7 +27,11 @@ wrap_pneum_callback (char *data, int len)
   gstate = PyGILState_Ensure();
 
   /* Time to call the callback */
+#if PY_VERSION_HEX >= 0x03000000
   result = PyObject_CallFunction(pneum_callback, "y#", data, len);
+#else
+  result = PyObject_CallFunction(pneum_callback, "s#", data, len);
+#endif
   if (result)
     Py_DECREF(result);
   else
@@ -25,13 +44,13 @@ wrap_pneum_callback (char *data, int len)
 static PyObject *
 wrap_connect (PyObject *self, PyObject *args)
 {
-  char *name;
+  char * name, * chroot_prefix = NULL;
   int rv;
-  PyObject *temp;
+  PyObject * temp;
 
-  if (!PyArg_ParseTuple(args, "sO:set_callback", &name, &temp))
+  if (!PyArg_ParseTuple(args, "sO|s:wrap_connect", &name, &temp, &chroot_prefix))
     return (NULL);
-  
+
   if (!PyCallable_Check(temp)) {
     PyErr_SetString(PyExc_TypeError, "parameter must be callable");
     return NULL;
@@ -42,7 +61,7 @@ wrap_connect (PyObject *self, PyObject *args)
   pneum_callback = temp;       /* Remember new callback */
 
   Py_BEGIN_ALLOW_THREADS
-  rv = pneum_connect(name);
+  rv = pneum_connect(name, chroot_prefix);
   Py_END_ALLOW_THREADS
   return PyLong_FromLong(rv);
 }
@@ -62,8 +81,8 @@ wrap_write (PyObject *self, PyObject *args)
   char *data;
   int len, rv;
 
-  if (!PyArg_ParseTuple(args, "s#", &data, &len)) 
-    return NULL;     
+  if (!PyArg_ParseTuple(args, "s#", &data, &len))
+    return NULL;
   Py_BEGIN_ALLOW_THREADS
   rv = pneum_write(data, len);
   Py_END_ALLOW_THREADS
@@ -84,8 +103,11 @@ wrap_read (PyObject *self, PyObject *args)
   Py_END_ALLOW_THREADS
 
   if (rv != 0) { Py_RETURN_NONE; }
-
+#if PY_VERSION_HEX >= 0x03000000
   PyObject *ret = Py_BuildValue("y#", data, len);
+#else
+  PyObject *ret = Py_BuildValue("s#", data, len);
+#endif
   if (!ret) { Py_RETURN_NONE; }
 
   vl_msg_api_free(data);
@@ -100,21 +122,46 @@ static PyMethodDef vpp_api_Methods[] = {
   {NULL, NULL, 0, NULL}        /* Sentinel */
 };
 
-static struct PyModuleDef vpp_api_module = {
-  PyModuleDef_HEAD_INIT,
-  "vpp_api",   /* name of module */
-   NULL, /* module documentation, may be NULL */
-  -1,       /* size of per-interpreter state of the module,
-              or -1 if the module keeps state in global variables. */
-  vpp_api_Methods
-};
-
+#if PY_VERSION_HEX >= 0x03000000
 PyMODINIT_FUNC
 PyInit_vpp_api (void)
+#else
+void
+initvpp_api (void)
+#endif
 {
+#if PY_VERSION_HEX >= 0x03000000
+  static struct PyModuleDef vpp_api_module = {
+#if PY_VERSION_HEX >= 0x03020000
+    PyModuleDef_HEAD_INIT,
+#else
+    {
+      PyObject_HEAD_INIT(NULL)
+      NULL, /* m_init */
+      0,    /* m_index */
+      NULL, /* m_copy */
+    },
+#endif
+    (char *) "vpp_api",
+    NULL,
+    -1,
+    vpp_api_Methods,
+    NULL,
+    NULL,
+    NULL,
+    NULL
+  };
+#endif
+
   /* Ensure threading is initialised */
   if (!PyEval_ThreadsInitialized()) {
     PyEval_InitThreads();
   }
+
+#if PY_VERSION_HEX >= 0x03000000
   return PyModule_Create(&vpp_api_module);
+#else
+  Py_InitModule((char *) "vpp_api", vpp_api_Methods);
+  return;
+#endif
 }