-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
To install | ||
|
||
$ sudo python setup.py install | ||
|
||
|
||
To use | ||
|
||
>>> import spam | ||
>>> spam.system("ls -l") | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from distutils.core import setup, Extension | ||
|
||
spam = Extension('spam', | ||
sources = ['spammodule.c']) | ||
|
||
setup (name = 'spam', | ||
version = '1.0', | ||
description = 'spam', | ||
ext_modules = [spam]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <Python.h> | ||
|
||
// Implements the module "spam", which has one function, "system". | ||
|
||
|
||
// Module method implementations | ||
static PyObject * | ||
spam_system(PyObject *self, PyObject *args) | ||
{ | ||
const char *command; | ||
int sts; | ||
|
||
// Parse Python arguments into C variables | ||
if (!PyArg_ParseTuple(args, "s", &command)) | ||
return NULL; | ||
|
||
// Do some work on the method arguments | ||
sts = system(command); | ||
|
||
// Build a Python object out of a C variable, and return | ||
return Py_BuildValue("i", sts); | ||
} | ||
|
||
|
||
// Method table | ||
static PyMethodDef SpamMethods[] = { | ||
{"system", spam_system, METH_VARARGS, "Execute a shell command."} | ||
}; | ||
|
||
|
||
// Module initialization | ||
PyMODINIT_FUNC | ||
initspam(void) | ||
{ | ||
(void) Py_InitModule("spam", SpamMethods); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
To install | ||
|
||
$ sudo python setup.py install | ||
|
||
|
||
To use | ||
|
||
>>> from noddy import Noddy | ||
>>> n = Noddy() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <Python.h> | ||
|
||
// Type definition | ||
typedef struct { | ||
PyObject_HEAD | ||
/* Type-specific fields go here. */ | ||
} noddy_NoddyObject; | ||
|
||
static PyTypeObject noddy_NoddyType = { | ||
PyObject_HEAD_INIT(NULL) | ||
0, /*ob_size*/ | ||
"noddy.Noddy", /*tp_name*/ | ||
sizeof(noddy_NoddyObject), /*tp_basicsize*/ | ||
0, /*tp_itemsize*/ | ||
0, /*tp_dealloc*/ | ||
0, /*tp_print*/ | ||
0, /*tp_getattr*/ | ||
0, /*tp_setattr*/ | ||
0, /*tp_compare*/ | ||
0, /*tp_repr*/ | ||
0, /*tp_as_number*/ | ||
0, /*tp_as_sequence*/ | ||
0, /*tp_as_mapping*/ | ||
0, /*tp_hash */ | ||
0, /*tp_call*/ | ||
0, /*tp_str*/ | ||
0, /*tp_getattro*/ | ||
0, /*tp_setattro*/ | ||
0, /*tp_as_buffer*/ | ||
Py_TPFLAGS_DEFAULT, /*tp_flags*/ | ||
"Noddy objects", /* tp_doc */ | ||
}; | ||
|
||
|
||
// Method table | ||
static PyMethodDef noddy_methods[] = { | ||
{NULL} /* Sentinel */ | ||
}; | ||
|
||
|
||
// Type initialization | ||
#ifndef PyMODINIT_FUNC/* declarations for DLL import/export */ | ||
#define PyMODINIT_FUNC void | ||
#endif | ||
PyMODINIT_FUNC | ||
initnoddy(void) | ||
{ | ||
PyObject* m; | ||
|
||
noddy_NoddyType.tp_new = PyType_GenericNew; | ||
if (PyType_Ready(&noddy_NoddyType) < 0) | ||
return; | ||
|
||
m = Py_InitModule3("noddy", noddy_methods, | ||
"Example module that creates an extension type."); | ||
|
||
Py_INCREF(&noddy_NoddyType); | ||
PyModule_AddObject(m, "Noddy", (PyObject *)&noddy_NoddyType); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from distutils.core import setup, Extension | ||
|
||
noddy = Extension("noddy", ["noddytype.c"]) | ||
|
||
setup(name="noddy", | ||
version="1.0", | ||
ext_modules=[noddy]) |