python-c

A small Python module in C
git clone https://git.tronto.net/python-c
Download | Log | Files | Refs | README

sum_module.c (562B)


      1 #define PY_SSIZE_T_CLEAN
      2 #include <Python.h>
      3 
      4 #include "sum.h"
      5 
      6 static PyObject *csum(PyObject *self, PyObject *args) {
      7 	int a, b, result;
      8 
      9 	if (!PyArg_ParseTuple(args, "ii", &a, &b))
     10 		return NULL;
     11 
     12 	result = sum(a, b);
     13 	return PyLong_FromLong(result);
     14 }
     15 
     16 static PyMethodDef SumMethods[] = {
     17 	{ "sum_from_c", csum, METH_VARARGS, "Sum two integers." },
     18 	{ NULL, NULL, 0, NULL }
     19 };
     20 
     21 static struct PyModuleDef summodule = {
     22 	PyModuleDef_HEAD_INIT,
     23 	"sum",
     24 	NULL,
     25 	-1,
     26 	SumMethods
     27 };
     28 
     29 PyMODINIT_FUNC PyInit_sum_module(void) {
     30 	return PyModule_Create(&summodule);
     31 }