annotate Modules/xxmodule.c @ 15842:f6cd36498a50 legacy-trunk

Document extensions to .pth files.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 13 Jan 2001 09:54:41 +0000
parents 6f6f965c5e2d
children b32d66a4b5e6
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*/
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
101 "Xxo", /*tp_name*/
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*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
115 };
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
116 /* --------------------------------------------------------------------- */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 /* Function of two integers returning integer */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
120 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
121 xx_foo(PyObject *self, PyObject *args)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 {
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 long i, j;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 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
125 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return NULL;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 res = i+j; /* XXX Do something here */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
128 return PyInt_FromLong(res);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
132 /* Function of no arguments returning new Xxo object */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
134 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
135 xx_new(PyObject *self, PyObject *args)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
137 XxoObject *rv;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
138
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
139 if (!PyArg_ParseTuple(args, ":new"))
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
141 rv = newXxoObject(args);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
142 if ( rv == NULL )
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
143 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
144 return (PyObject *)rv;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
4714
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
147 /* 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
148
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
149 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
150 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
151 {
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
152 PyObject *list, *item;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
153
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10202
diff changeset
154 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
155 return NULL;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
156
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
157 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
158 /* Py_INCREF(item); */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
159 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
160 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
161 printf("\n");
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
162 /* Py_DECREF(item); */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
163
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
164 Py_INCREF(Py_None);
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
165 return Py_None;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
166 }
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
167
6806
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
168 /* Test bad format character */
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
169
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
170 static PyObject *
13499
046799a08057 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
171 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
172 {
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
173 PyObject *a;
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
174 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
175 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
176 return NULL;
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
177 Py_INCREF(Py_None);
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
178 return Py_None;
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
179 }
ac1b261f1690 Add a test for a bad format character.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
180
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 /* List of functions defined in the module */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
184 static PyMethodDef xx_methods[] = {
14073
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
185 {"roj", xx_roj, METH_VARARGS},
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
186 {"foo", xx_foo, METH_VARARGS},
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
187 {"new", xx_new, METH_VARARGS},
314ec59e4e14 Use METH_VARARGS constant in example module.
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
188 {"bug", xx_bug, METH_VARARGS},
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 {NULL, NULL} /* sentinel */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 /* Initialization function for the module (*must* be called initxx) */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 6806
diff changeset
195 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13499
diff changeset
196 initxx(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
198 PyObject *m, *d;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
10202
e1da290acc5f Use the portable form of initializing the ob_type field for new types.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
200 /* 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
201 * 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
202 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
203
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 /* Create the module and add the functions */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
205 m = Py_InitModule("xx", xx_methods);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 /* Add some symbolic constants to the module */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
208 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 4714
diff changeset
209 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
210 PyDict_SetItemString(d, "error", ErrorObject);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 }