Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <Python.h>
- static PyObject *
- test_add(PyObject *self, PyObject *args)
- {
- int a,b;
- if (!PyArg_ParseTuple(args, "ii", &a, &b))
- return NULL;
- return PyLong_FromLong(a+b);
- }
- static PyMethodDef TestMethods[] = {
- {"add", test_add, METH_VARARGS, "Add command."},
- {NULL, NULL, 0, NULL}
- };
- PyMODINIT_FUNC
- init_test(void)
- {
- PyObject *m;
- m = Py_InitModule("test", TestMethods);
- if (m == NULL)
- return;
- }
- static PyMethodDef ContainerMethods[] = {
- {NULL, NULL, 0, NULL}
- };
- PyMODINIT_FUNC
- init_container(void)
- {
- PyObject *m = Py_InitModule("container", ContainerMethods);
- if (m == NULL)
- return;
- PyObject *d, *tmp;
- d = PyModule_GetDict(m);
- tmp = PyImport_AddModule("test");
- PyDict_SetItemString(d, "module1", tmp);
- Py_DECREF(tmp);
- tmp = PyImport_AddModule("test");
- PyDict_SetItemString(d, "module2", tmp);
- Py_DECREF(tmp);
- }
- int main(int argc, char** argv)
- {
- Py_Initialize();
- Py_SetProgramName(argv[0]);
- init_test();
- init_container();
- PyRun_SimpleString(
- "import container\n"
- "print container.module1.add(1,2)\n"
- );
- Py_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment