Advertisement
tyler569

Python C extention

Aug 20th, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <python3.4m/Python.h>
  2.  
  3.  
  4. static PyObject *
  5. metest_thing(PyObject *self, PyObject *args)
  6. {
  7.     int a, b;
  8.     int ret;
  9.  
  10.     if (!PyArg_ParseTuple(args, "ii", &a, &b))
  11.         return NULL;
  12.     Py_XDECREF(args)
  13.     ret = a + b;
  14.     return PyLong_FromLong(ret);
  15. }
  16.  
  17. static PyMethodDef TestMethods[] = {
  18.     {"thing", metest_thing, METH_VARARGS,
  19.      "Add 2 PyNumbers"},
  20.     {NULL, NULL, 0, NULL}
  21. };
  22.  
  23. static struct PyModuleDef metestmodule = {
  24.     PyModuleDef_HEAD_INIT,
  25.     "metest",     /* Name of module */
  26.     NULL,       /* documentation */
  27.     -1,         /* global state */
  28.     TestMethods
  29. };
  30.  
  31. PyMODINIT_FUNC
  32. PyInit_metest(void)
  33. {
  34.     PyObject *mod;
  35.  
  36.     mod = PyModule_Create(&metestmodule);
  37.     if (mod == NULL)
  38.         return NULL;
  39.  
  40.     return mod;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement