Guest User

Untitled

a guest
Jun 17th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Python.h>
  4.  
  5. static PyObject *
  6. test_add(PyObject *self, PyObject *args)
  7. {
  8.     int a,b;
  9.     if (!PyArg_ParseTuple(args, "ii", &a, &b))
  10.         return NULL;
  11.     return PyLong_FromLong(a+b);
  12. }
  13.  
  14. static PyMethodDef TestMethods[] = {
  15.     {"add",  test_add, METH_VARARGS, "Add command."},
  16.     {NULL, NULL, 0, NULL}
  17. };
  18.  
  19. PyMODINIT_FUNC
  20. init_test(void)
  21. {
  22.     PyObject *m;
  23.  
  24.     m = Py_InitModule("test", TestMethods);
  25.     if (m == NULL)
  26.         return;
  27. }
  28.  
  29. static PyMethodDef ContainerMethods[] = {
  30.     {NULL, NULL, 0, NULL}
  31. };
  32.  
  33. PyMODINIT_FUNC
  34. init_container(void)
  35. {
  36.     PyObject *m = Py_InitModule("container", ContainerMethods);
  37.     if (m == NULL)
  38.         return;
  39.  
  40.     PyObject *d, *tmp;
  41.     d = PyModule_GetDict(m);
  42.     tmp = PyImport_AddModule("test");
  43.     PyDict_SetItemString(d, "module1", tmp);
  44.     Py_DECREF(tmp);
  45.  
  46.     tmp = PyImport_AddModule("test");
  47.     PyDict_SetItemString(d, "module2", tmp);
  48.     Py_DECREF(tmp);
  49. }
  50.  
  51. int main(int argc, char** argv)
  52. {
  53.     Py_Initialize();
  54.     Py_SetProgramName(argv[0]);
  55.     init_test();
  56.     init_container();
  57.     PyRun_SimpleString(
  58.       "import container\n"
  59.       "print container.module1.add(1,2)\n"
  60.     );
  61.     Py_Finalize();
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment