xxmodule.c 8.9 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
   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.
Tim Peters's avatar
Tim Peters committed
8

9 10 11 12 13 14 15
   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
static 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 61
	{"demo",	(PyCFunction)Xxo_demo,	METH_VARARGS,
		PyDoc_STR("demo() -> None")},
62 63 64
	{NULL,		NULL}		/* sentinel */
};

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

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

97
static PyTypeObject Xxo_Type = {
98 99 100
	/* The ob_type field must be initialized in the module init function
	 * to be portable to Windows without using C++. */
	PyObject_HEAD_INIT(NULL)
101
	0,			/*ob_size*/
102
	"xxmodule.Xxo",		/*tp_name*/
103
	sizeof(XxoObject),	/*tp_basicsize*/
104 105
	0,			/*tp_itemsize*/
	/* methods */
106
	(destructor)Xxo_dealloc, /*tp_dealloc*/
107
	0,			/*tp_print*/
108 109
	(getattrfunc)Xxo_getattr, /*tp_getattr*/
	(setattrfunc)Xxo_setattr, /*tp_setattr*/
110 111 112 113 114 115
	0,			/*tp_compare*/
	0,			/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
	0,			/*tp_hash*/
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 141
        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*/
142 143
};
/* --------------------------------------------------------------------- */
144 145 146

/* Function of two integers returning integer */

147 148 149 150 151
PyDoc_STRVAR(xx_foo_doc,
"foo(i,j)\n\
\n\
Return the sum of i and j.");

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


164
/* Function of no arguments returning new Xxo object */
165

166
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
167
xx_new(PyObject *self, PyObject *args)
168
{
169
	XxoObject *rv;
Tim Peters's avatar
Tim Peters committed
170

171
	if (!PyArg_ParseTuple(args, ":new"))
172
		return NULL;
173
	rv = newXxoObject(args);
Tim Peters's avatar
Tim Peters committed
174 175
	if (rv == NULL)
		return NULL;
176
	return (PyObject *)rv;
177 178
}

179 180 181
/* Example with subtle bug from extensions manual ("Thin Ice"). */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
182
xx_bug(PyObject *self, PyObject *args)
183 184
{
	PyObject *list, *item;
Tim Peters's avatar
Tim Peters committed
185

186
	if (!PyArg_ParseTuple(args, "O:bug", &list))
187
		return NULL;
Tim Peters's avatar
Tim Peters committed
188

189 190 191 192 193 194
	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); */
Tim Peters's avatar
Tim Peters committed
195

196 197 198 199
	Py_INCREF(Py_None);
	return Py_None;
}

200 201 202
/* Test bad format character */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
203
xx_roj(PyObject *self, PyObject *args)
204 205 206
{
	PyObject *a;
	long b;
207
	if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
208 209 210 211 212
		return NULL;
	Py_INCREF(Py_None);
	return Py_None;
}

213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/* ---------- */

static PyTypeObject Str_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)
	0,			/*ob_size*/
	"xxmodule.Str",		/*tp_name*/
	0,			/*tp_basicsize*/
	0,			/*tp_itemsize*/
	/* methods */
	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 | Py_TPFLAGS_BASETYPE, /*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*/
	&PyString_Type,		/*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*/
};

Guido van Rossum's avatar
Guido van Rossum committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
/* ---------- */

static PyObject *
null_richcompare(PyObject *self, PyObject *other, int op)
{
	Py_INCREF(Py_NotImplemented);
	return Py_NotImplemented;
}

static PyTypeObject Null_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)
	0,			/*ob_size*/
	"xxmodule.Null",	/*tp_name*/
	0,			/*tp_basicsize*/
	0,			/*tp_itemsize*/
	/* methods */
	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 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
	0,			/*tp_doc*/
	0,			/*tp_traverse*/
	0,			/*tp_clear*/
	null_richcompare,	/*tp_richcompare*/
	0,			/*tp_weaklistoffset*/
	0,			/*tp_iter*/
	0,			/*tp_iternext*/
	0,			/*tp_methods*/
	0,			/*tp_members*/
	0,			/*tp_getset*/
	&PyBaseObject_Type,	/*tp_base*/
	0,			/*tp_dict*/
	0,			/*tp_descr_get*/
	0,			/*tp_descr_set*/
	0,			/*tp_dictoffset*/
	0,			/*tp_init*/
	0,			/*tp_alloc*/
	PyType_GenericNew,	/*tp_new*/
	0,			/*tp_free*/
	0,			/*tp_is_gc*/
};

319 320 321 322

/* ---------- */


323 324
/* List of functions defined in the module */

325
static PyMethodDef xx_methods[] = {
326 327 328 329 330 331 332 333
	{"roj",		xx_roj,		METH_VARARGS,
		PyDoc_STR("roj(a,b) -> None")},
	{"foo",		xx_foo,		METH_VARARGS,
	 	xx_foo_doc},
	{"new",		xx_new,		METH_VARARGS,
		PyDoc_STR("new() -> new Xx object")},
	{"bug",		xx_bug,		METH_VARARGS,
		PyDoc_STR("bug(o) -> None")},
334 335 336
	{NULL,		NULL}		/* sentinel */
};

337 338
PyDoc_STRVAR(module_doc,
"This is a template module just for instruction.");
339 340 341

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

342
PyMODINIT_FUNC
343
initxx(void)
344
{
Thomas Heller's avatar
Thomas Heller committed
345
	PyObject *m;
346

347 348 349 350 351
	/* Finalize the type object including setting type of the new type
	 * object; doing it here is required for portability to Windows 
	 * without requiring C++. */
	if (PyType_Ready(&Xxo_Type) < 0)
		return;
352

353
	/* Create the module and add the functions */
354
	m = Py_InitModule3("xx", xx_methods, module_doc);
355 356

	/* Add some symbolic constants to the module */
357 358 359 360 361 362
	if (ErrorObject == NULL) {
		ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
		if (ErrorObject == NULL)
			return;
	}
	Py_INCREF(ErrorObject);
Thomas Heller's avatar
Thomas Heller committed
363
	PyModule_AddObject(m, "error", ErrorObject);
364 365 366 367 368

	/* Add Str */
	if (PyType_Ready(&Str_Type) < 0)
		return;
	PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
Guido van Rossum's avatar
Guido van Rossum committed
369 370 371 372 373

	/* Add Null */
	if (PyType_Ready(&Null_Type) < 0)
		return;
	PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
374
}