Skip to content

Instantly share code, notes, and snippets.

@wofeiwo
Created September 13, 2012 13:16
Show Gist options
  • Save wofeiwo/3714215 to your computer and use it in GitHub Desktop.
Save wofeiwo/3714215 to your computer and use it in GitHub Desktop.
Python System Execute Module
from distutils.core import setup, Extension
ext = Extension('system', sources=['system.c'])
setup(name='system', version='1.0', description='Test description', ext_modules=[ext])
#include <Python.h>
#include <string.h>
static PyObject * system_system(PyObject *self, PyObject *args) {
const char *command;
int sts;
if (! PyArg_ParseTuple(args, "s", &command)) {
return NULL;
}
sts = system(command);
return Py_BuildValue("i", sts);
}
// Module's method table and initialization function
// See: http://docs.python.org/extending/extending.html#the-module-s-method-table-and-initialization-function
static PyMethodDef SystemMethods[] = {
{"system", system_system, METH_VARARGS, "Execute a shell command."},
{NULL, NULL, 0, NULL}
};
void initsystem(void) {
PyImport_AddModule("system");
Py_InitModule("system", SystemMethods);
}
int main(int argc, char *argv[]) {
Py_SetProgramName(argv[0]);
Py_Initialize();
initsystem();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment