Skip to content

Commit

Permalink
Add example of an extension type
Browse files Browse the repository at this point in the history
  • Loading branch information
hammer committed Jun 1, 2012
1 parent 8fa5c22 commit 437ad77
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
10 changes: 10 additions & 0 deletions c-api/extension-module/README.md
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")

9 changes: 9 additions & 0 deletions c-api/extension-module/setup.py
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])
36 changes: 36 additions & 0 deletions c-api/extension-module/spammodule.c
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);
}
9 changes: 9 additions & 0 deletions c-api/extension-type/README.md
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()
59 changes: 59 additions & 0 deletions c-api/extension-type/noddytype.c
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);
}
7 changes: 7 additions & 0 deletions c-api/extension-type/setup.py
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])

0 comments on commit 437ad77

Please sign in to comment.