Created
September 13, 2012 13:16
-
-
Save wofeiwo/3714215 to your computer and use it in GitHub Desktop.
Python System Execute Module
This file contains 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 distutils.core import setup, Extension | |
ext = Extension('system', sources=['system.c']) | |
setup(name='system', version='1.0', description='Test description', ext_modules=[ext]) |
This file contains 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> | |
#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