xxmodule.c 5.96 KB
Newer Older
1

2
/* Use this file as a template to start implementing a module that
3
   also declares object types. All occurrences of 'Xxo' should be changed
4 5 6 7 8 9 10 11 12 13 14 15
   to something reasonable for your objects. After that, all other
   occurrences of 'xx' should be changed to something reasonable for your
   module. If your module is named foo your sourcefile should be named
   foomodule.c.
   
   You will probably want to delete all references to 'x_attr' and add
   your own types of attributes instead.  Maybe you want to name your
   local variables other than 'self'.  If your object type is needed in
   other files, you'll have to create a file "foobarobject.h"; see
   intobject.h for an example. */

/* Xxo objects */
16

17
#include "Python.h"
18

19
static PyObject *ErrorObject;
20 21

typedef struct {
22 23 24
	PyObject_HEAD
	PyObject	*x_attr;	/* Attributes dictionary */
} XxoObject;
25

26
staticforward PyTypeObject Xxo_Type;
27

28
#define XxoObject_Check(v)	((v)->ob_type == &Xxo_Type)
29

30
static XxoObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
31
newXxoObject(PyObject *arg)
32
{
33
	XxoObject *self;
34
	self = PyObject_New(XxoObject, &Xxo_Type);
35 36 37 38 39 40 41 42 43
	if (self == NULL)
		return NULL;
	self->x_attr = NULL;
	return self;
}

/* Xxo methods */

static void
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
44
Xxo_dealloc(XxoObject *self)
45
{
46
	Py_XDECREF(self->x_attr);
47
	PyObject_Del(self);
48 49
}

50
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
51
Xxo_demo(XxoObject *self, PyObject *args)
52
{
53
	if (!PyArg_ParseTuple(args, ":demo"))
54
		return NULL;
55 56
	Py_INCREF(Py_None);
	return Py_None;
57 58
}

59
static PyMethodDef Xxo_methods[] = {
60
	{"demo",	(PyCFunction)Xxo_demo,	METH_VARARGS},
61 62 63
	{NULL,		NULL}		/* sentinel */
};

64
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
65
Xxo_getattr(XxoObject *self, char *name)
66 67
{
	if (self->x_attr != NULL) {
68
		PyObject *v = PyDict_GetItemString(self->x_attr, name);
69
		if (v != NULL) {
70
			Py_INCREF(v);
71 72 73
			return v;
		}
	}
74
	return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
75 76 77
}

static int
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
78
Xxo_setattr(XxoObject *self, char *name, PyObject *v)
79 80
{
	if (self->x_attr == NULL) {
81
		self->x_attr = PyDict_New();
82 83 84 85
		if (self->x_attr == NULL)
			return -1;
	}
	if (v == NULL) {
86
		int rv = PyDict_DelItemString(self->x_attr, name);
87
		if (rv < 0)
88 89
			PyErr_SetString(PyExc_AttributeError,
			        "delete non-existing Xxo attribute");
90 91 92
		return rv;
	}
	else
93
		return PyDict_SetItemString(self->x_attr, name, v);
94 95
}

96 97 98 99
statichere PyTypeObject Xxo_Type = {
	/* The ob_type field must be initialized in the module init function
	 * to be portable to Windows without using C++. */
	PyObject_HEAD_INIT(NULL)
100
	0,			/*ob_size*/
101
	"xxmodule.Xxo",		/*tp_name*/
102
	sizeof(XxoObject),	/*tp_basicsize*/
103 104
	0,			/*tp_itemsize*/
	/* methods */
105
	(destructor)Xxo_dealloc, /*tp_dealloc*/
106
	0,			/*tp_print*/
107 108
	(getattrfunc)Xxo_getattr, /*tp_getattr*/
	(setattrfunc)Xxo_setattr, /*tp_setattr*/
109 110 111 112 113 114
	0,			/*tp_compare*/
	0,			/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
	0,			/*tp_hash*/
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        0,                      /*tp_call*/
        0,                      /*tp_str*/
        0,                      /*tp_getattro*/
        0,                      /*tp_setattro*/
        0,                      /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,     /*tp_flags*/
        0,                      /*tp_doc*/
        0,                      /*tp_traverse*/
        0,                      /*tp_clear*/
        0,                      /*tp_richcompare*/
        0,                      /*tp_weaklistoffset*/
        0,                      /*tp_iter*/
        0,                      /*tp_iternext*/
        0,                      /*tp_methods*/
        0,                      /*tp_members*/
        0,                      /*tp_getset*/
        0,                      /*tp_base*/
        0,                      /*tp_dict*/
        0,                      /*tp_descr_get*/
        0,                      /*tp_descr_set*/
        0,                      /*tp_dictoffset*/
        0,                      /*tp_init*/
        0,                      /*tp_alloc*/
        0,                      /*tp_new*/
        0,                      /*tp_free*/
        0,                      /*tp_is_gc*/
141 142
};
/* --------------------------------------------------------------------- */
143 144 145

/* Function of two integers returning integer */

146
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
147
xx_foo(PyObject *self, PyObject *args)
148 149 150
{
	long i, j;
	long res;
151
	if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
152 153
		return NULL;
	res = i+j; /* XXX Do something here */
154
	return PyInt_FromLong(res);
155 156 157
}


158
/* Function of no arguments returning new Xxo object */
159

160
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
161
xx_new(PyObject *self, PyObject *args)
162
{
163
	XxoObject *rv;
164
	
165
	if (!PyArg_ParseTuple(args, ":new"))
166
		return NULL;
167
	rv = newXxoObject(args);
168 169
	if ( rv == NULL )
	    return NULL;
170
	return (PyObject *)rv;
171 172
}

173 174 175
/* Example with subtle bug from extensions manual ("Thin Ice"). */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
176
xx_bug(PyObject *self, PyObject *args)
177 178 179
{
	PyObject *list, *item;
	
180
	if (!PyArg_ParseTuple(args, "O:bug", &list))
181 182 183 184 185 186 187 188 189 190 191 192 193
		return NULL;
	
	item = PyList_GetItem(list, 0);
	/* Py_INCREF(item); */
	PyList_SetItem(list, 1, PyInt_FromLong(0L));
	PyObject_Print(item, stdout, 0);
	printf("\n");
	/* Py_DECREF(item); */
	
	Py_INCREF(Py_None);
	return Py_None;
}

194 195 196
/* Test bad format character */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
197
xx_roj(PyObject *self, PyObject *args)
198 199 200
{
	PyObject *a;
	long b;
201
	if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
202 203 204 205 206
		return NULL;
	Py_INCREF(Py_None);
	return Py_None;
}

207 208 209

/* List of functions defined in the module */

210
static PyMethodDef xx_methods[] = {
211 212 213 214
	{"roj",		xx_roj,		METH_VARARGS},
	{"foo",		xx_foo,		METH_VARARGS},
	{"new",		xx_new,		METH_VARARGS},
	{"bug",		xx_bug,		METH_VARARGS},
215 216 217 218 219 220
	{NULL,		NULL}		/* sentinel */
};


/* Initialization function for the module (*must* be called initxx) */

221
DL_EXPORT(void)
222
initxx(void)
223
{
224
	PyObject *m, *d;
225

226 227 228 229
	/* Initialize the type of the new type object here; doing it here
	 * is required for portability to Windows without requiring C++. */
	Xxo_Type.ob_type = &PyType_Type;

230
	/* Create the module and add the functions */
231
	m = Py_InitModule("xx", xx_methods);
232 233

	/* Add some symbolic constants to the module */
234
	d = PyModule_GetDict(m);
235
	ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
236
	PyDict_SetItemString(d, "error", ErrorObject);
237
}