Created
December 4, 2013 14:18
-
-
Save bulv1ne/7788117 to your computer and use it in GitHub Desktop.
Extening Python with C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Python.h> | |
static PyObject* say_hello(PyObject* self, PyObject* args) | |
{ | |
const char* name; | |
if (!PyArg_ParseTuple(args, "s", &name)) | |
return NULL; | |
printf("Hello %s!\n", name); | |
Py_RETURN_NONE; | |
} | |
static PyObject* say_ihello(PyObject* self, PyObject* args) | |
{ | |
int name; | |
if (!PyArg_ParseTuple(args, "i", &name)) | |
return NULL; | |
name++; | |
printf("Hello %i!\n", name); | |
Py_RETURN_NONE; | |
} | |
static PyMethodDef HelloMethods[] = | |
{ | |
{"say_hello", say_hello, METH_VARARGS, "Greet somebody."}, | |
{"say_ihello", say_ihello, METH_VARARGS, "Greet somebody."}, | |
{NULL, NULL, 0, NULL} | |
}; | |
static struct PyModuleDef hellomodule = { | |
PyModuleDef_HEAD_INIT, | |
"hello", /* name of module */ | |
NULL, /* module documentation, may be NULL */ | |
-1, /* size of per-interpreter state of the module, | |
or -1 if the module keeps state in global variables. */ | |
HelloMethods | |
}; | |
PyMODINIT_FUNC | |
PyInit_hello(void) | |
{ | |
PyObject *m; | |
m = PyModule_Create(&hellomodule); | |
if (m == NULL) | |
return NULL; | |
return m; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from setuptools import setup, Extension | |
module1 = Extension('hello', sources = ['hellomodule.c']) | |
setup (name = 'PackageName', | |
version = '1.0', | |
description = 'This is a demo package', | |
ext_modules = [module1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment