Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Python.h>
- static PyObject* test_square(PyObject *self, PyObject *args) {
- const int* i;
- if (!PyArg_ParseTuple(args, "l", &i))
- return NULL;
- return PyLong_FromLong(*i**i);
- }
- /*
- For void functions
- Py_INCREF(Py_None);
- return Py_None;
- */
- static PyMethodDef TestMethods[] = {
- {"test_square", test_square, METH_VARARGS, "Square a number."},
- {NULL, NULL, 0, NULL} /* Sentinel */
- };
- static struct PyModuleDef testmodule = {
- PyModuleDef_HEAD_INIT,
- "test", /* name of module */
- NULL, /* originally test_doc, module documentation, may be NULL */
- -1, /* size of per-interpreter state of the module,
- or -1 if the module keeps state in global variables. */
- TestMethods
- };
- PyMODINIT_FUNC PyInit_test(void)
- {
- PyObject* module;
- module = PyModule_Create(&testmodule);
- if (module == NULL)
- return NULL;
- TestError = PyErr_NewException("test error thrown", NULL, NULL);
- Py_INCREF(TestError);
- PyModule_AddObject(module, "test error added", TestError);
- return module;
- return PyModule_Create(&testmodule);
- }
- from distutils.core import setup, Extension
- # the c++ extension module
- test_module_py = Extension("assignment1", sources=["assignment1.c"])
- setup(name='PackageName',
- version='1.0',
- description='This is a demo package',
- ext_modules=[test_module_py])
- #include <Python.h>
- #include "sum.h"
- static PyObject* mod_sum(PyObject *self, PyObject *args)
- {
- int a;
- int b;
- int s;
- if (!PyArg_ParseTuple(args, "ii", &a, &b))
- return NULL;
- s = sum(a, b);
- return Py_BuildValue("i", s);
- }
- static PyMethodDef ModMethods[] = {
- { "sum", mod_sum, METH_VARARGS, "Description.." },
- { NULL,NULL,0,NULL }
- };
- PyMODINIT_FUNC initmod(void)
- {
- PyObject *m;
- m = Py_InitModule("module", ModMethods);
- if (m == NULL)
- return;
- }
- int sum(int a, int b) {
- return a + b;
- }
- import assignment1
- s = assignment1.sum(1, 5)
- print(s)
Add Comment
Please, Sign In to add comment