annotate Modules/xxmodule.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 91dd223829c6
children aa3bf525f482
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
2 /* Use this file as a template to start implementing a module that
14073
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
3 also declares object types. All occurrences of 'Xxo' should be changed
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
4 to something reasonable for your objects. After that, all other
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
5 occurrences of 'xx' should be changed to something reasonable for your
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
6 module. If your module is named foo your sourcefile should be named
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
7 foomodule.c.
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
8
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
9 You will probably want to delete all references to 'x_attr' and add
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
10 your own types of attributes instead. Maybe you want to name your
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
11 local variables other than 'self'. If your object type is needed in
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
12 other files, you'll have to create a file "foobarobject.h"; see
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
13 intobject.h for an example. */
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
14
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
15 /* Xxo objects */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
17 #include "Python.h"
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
18
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
19 static PyObject *ErrorObject;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
20
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
21 typedef struct {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
22 PyObject_HEAD
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
23 PyObject *x_attr; /* Attributes dictionary */
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
24 } XxoObject;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
25
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
26 staticforward PyTypeObject Xxo_Type;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
27
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
28 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
29
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
30 static XxoObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
31 newXxoObject(PyObject *arg)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
32 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
33 XxoObject *self;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
34 self = PyObject_New(XxoObject, &Xxo_Type);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
35 if (self == NULL)
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
36 return NULL;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
37 self->x_attr = NULL;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
38 return self;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
39 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
40
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
41 /* Xxo methods */
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
42
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
43 static void
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
44 Xxo_dealloc(XxoObject *self)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
45 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
46 Py_XDECREF(self->x_attr);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
47 PyObject_Del(self);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
48 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
49
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
50 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
51 Xxo_demo(XxoObject *self, PyObject *args)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
52 {
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
53 if (!PyArg_ParseTuple(args, ":demo"))
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
54 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
55 Py_INCREF(Py_None);
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
56 return Py_None;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
57 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
58
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
59 static PyMethodDef Xxo_methods[] = {
14073
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
60 {"demo", (PyCFunction)Xxo_demo, METH_VARARGS},
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
61 {NULL, NULL} /* sentinel */
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
62 };
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
64 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
65 Xxo_getattr(XxoObject *self, char *name)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
66 {
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
67 if (self->x_attr != NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
68 PyObject *v = PyDict_GetItemString(self->x_attr, name);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
69 if (v != NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
70 Py_INCREF(v);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
71 return v;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
72 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
73 }
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
74 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
75 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
76
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
77 static int
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
78 Xxo_setattr(XxoObject *self, char *name, PyObject *v)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
79 {
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
80 if (self->x_attr == NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
81 self->x_attr = PyDict_New();
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
82 if (self->x_attr == NULL)
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
83 return -1;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
84 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
85 if (v == NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
86 int rv = PyDict_DelItemString(self->x_attr, name);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
87 if (rv < 0)
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
88 PyErr_SetString(PyExc_AttributeError,
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
89 "delete non-existing Xxo attribute");
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
90 return rv;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
91 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
92 else
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
93 return PyDict_SetItemString(self->x_attr, name, v);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
94 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
95
10202
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
96 statichere PyTypeObject Xxo_Type = {
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
97 /* The ob_type field must be initialized in the module init function
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
98 * to be portable to Windows without using C++. */
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
99 PyObject_HEAD_INIT(NULL)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
100 0, /*ob_size*/
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 20441
diff changeset
101 "xxmodule.Xxo", /*tp_name*/
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
102 sizeof(XxoObject), /*tp_basicsize*/
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
103 0, /*tp_itemsize*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
104 /* methods */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
105 (destructor)Xxo_dealloc, /*tp_dealloc*/
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
106 0, /*tp_print*/
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
107 (getattrfunc)Xxo_getattr, /*tp_getattr*/
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
108 (setattrfunc)Xxo_setattr, /*tp_setattr*/
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
109 0, /*tp_compare*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
110 0, /*tp_repr*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
111 0, /*tp_as_number*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
112 0, /*tp_as_sequence*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
113 0, /*tp_as_mapping*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
114 0, /*tp_hash*/
20326
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
115 0, /*tp_call*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
116 0, /*tp_str*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
117 0, /*tp_getattro*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
118 0, /*tp_setattro*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
119 0, /*tp_as_buffer*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
120 Py_TPFLAGS_DEFAULT, /*tp_flags*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
121 0, /*tp_doc*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
122 0, /*tp_traverse*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
123 0, /*tp_clear*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
124 0, /*tp_richcompare*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
125 0, /*tp_weaklistoffset*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
126 0, /*tp_iter*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
127 0, /*tp_iternext*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
128 0, /*tp_methods*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
129 0, /*tp_members*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
130 0, /*tp_getset*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
131 0, /*tp_base*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
132 0, /*tp_dict*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
133 0, /*tp_descr_get*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
134 0, /*tp_descr_set*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
135 0, /*tp_dictoffset*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
136 0, /*tp_init*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
137 0, /*tp_alloc*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
138 0, /*tp_new*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
139 0, /*tp_free*/
b32d66a4b5e6 Add additional fields to Xxo_Type declaration. Fixes bug #469250.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
140 0, /*tp_is_gc*/
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
141 };
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
142 /* --------------------------------------------------------------------- */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 /* Function of two integers returning integer */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
146 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
147 xx_foo(PyObject *self, PyObject *args)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 {
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 long i, j;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 long res;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
151 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 return NULL;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 res = i+j; /* XXX Do something here */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
154 return PyInt_FromLong(res);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
158 /* Function of no arguments returning new Xxo object */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
160 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
161 xx_new(PyObject *self, PyObject *args)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
163 XxoObject *rv;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
164
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
165 if (!PyArg_ParseTuple(args, ":new"))
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
167 rv = newXxoObject(args);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
168 if ( rv == NULL )
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
169 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
170 return (PyObject *)rv;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
4714
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
173 /* Example with subtle bug from extensions manual ("Thin Ice"). */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
174
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
175 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
176 xx_bug(PyObject *self, PyObject *args)
4714
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
177 {
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
178 PyObject *list, *item;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
179
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
180 if (!PyArg_ParseTuple(args, "O:bug", &list))
4714
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
181 return NULL;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
182
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
183 item = PyList_GetItem(list, 0);
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
184 /* Py_INCREF(item); */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
185 PyList_SetItem(list, 1, PyInt_FromLong(0L));
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
186 PyObject_Print(item, stdout, 0);
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
187 printf("\n");
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
188 /* Py_DECREF(item); */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
189
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
190 Py_INCREF(Py_None);
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
191 return Py_None;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
192 }
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
193
6806
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
194 /* Test bad format character */
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
195
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
196 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
197 xx_roj(PyObject *self, PyObject *args)
6806
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
198 {
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
199 PyObject *a;
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
200 long b;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
201 if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
6806
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
202 return NULL;
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
203 Py_INCREF(Py_None);
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
204 return Py_None;
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
205 }
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
206
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 /* List of functions defined in the module */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
210 static PyMethodDef xx_methods[] = {
14073
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
211 {"roj", xx_roj, METH_VARARGS},
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
212 {"foo", xx_foo, METH_VARARGS},
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
213 {"new", xx_new, METH_VARARGS},
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
214 {"bug", xx_bug, METH_VARARGS},
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 {NULL, NULL} /* sentinel */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 /* Initialization function for the module (*must* be called initxx) */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 6806
diff changeset
221 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13499
diff changeset
222 initxx(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
224 PyObject *m, *d;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
10202
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
226 /* Initialize the type of the new type object here; doing it here
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
227 * is required for portability to Windows without requiring C++. */
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
228 Xxo_Type.ob_type = &PyType_Type;
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
229
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 /* Create the module and add the functions */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
231 m = Py_InitModule("xx", xx_methods);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 /* Add some symbolic constants to the module */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
234 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 4714
diff changeset
235 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
236 PyDict_SetItemString(d, "error", ErrorObject);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 }