annotate Modules/xxmodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 0c6c69b0d3e9
children 2c29b8982643
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 /***********************************************************
2061
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1915
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1915
diff changeset
3 The Netherlands.
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
8 documentation for any purpose and without fee is hereby granted,
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
10 both that copyright notice and this permission notice appear in
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
15 permission.
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3845
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
32 /* Use this file as a template to start implementing a module that
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
33 also declares objects 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
34 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
35 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
36 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
37 foomodule.c.
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
38
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
39 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
40 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
41 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
42 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
43 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
44
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
45 /* Xxo objects */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
47 #include "Python.h"
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
48
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
49 static PyObject *ErrorObject;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
50
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
51 typedef struct {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
52 PyObject_HEAD
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
53 PyObject *x_attr; /* Attributes dictionary */
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
54 } XxoObject;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
55
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
56 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
57
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
58 #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
59
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
60 static XxoObject *
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
61 newXxoObject(arg)
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
62 PyObject *arg;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
63 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
64 XxoObject *self;
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
65 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
66 if (self == NULL)
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
67 return NULL;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
68 self->x_attr = NULL;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
69 return self;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
70 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
71
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
72 /* Xxo methods */
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
73
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
74 static void
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
75 Xxo_dealloc(self)
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
76 XxoObject *self;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
77 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
78 Py_XDECREF(self->x_attr);
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
79 PyMem_DEL(self);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
80 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
81
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
82 static PyObject *
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
83 Xxo_demo(self, args)
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
84 XxoObject *self;
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
85 PyObject *args;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
86 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
87 if (!PyArg_ParseTuple(args, ""))
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
88 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
89 Py_INCREF(Py_None);
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
90 return Py_None;
1915
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
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
93 static PyMethodDef Xxo_methods[] = {
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
94 {"demo", (PyCFunction)Xxo_demo, 1},
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
95 {NULL, NULL} /* sentinel */
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
96 };
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
98 static PyObject *
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
99 Xxo_getattr(self, name)
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
100 XxoObject *self;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
101 char *name;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
102 {
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
103 if (self->x_attr != NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
104 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
105 if (v != NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
106 Py_INCREF(v);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
107 return v;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
108 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
109 }
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
110 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
111 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
112
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
113 static int
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
114 Xxo_setattr(self, name, v)
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
115 XxoObject *self;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
116 char *name;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
117 PyObject *v;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
118 {
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
119 if (self->x_attr == NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
120 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
121 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
122 return -1;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
123 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
124 if (v == NULL) {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
125 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
126 if (rv < 0)
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
127 PyErr_SetString(PyExc_AttributeError,
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
128 "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
129 return rv;
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
130 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
131 else
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
132 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
133 }
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
134
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
135 staticforward PyTypeObject Xxo_Type = {
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
136 PyObject_HEAD_INIT(&PyType_Type)
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
137 0, /*ob_size*/
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
138 "Xxo", /*tp_name*/
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
139 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
140 0, /*tp_itemsize*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
141 /* methods */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
142 (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
143 0, /*tp_print*/
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
144 (getattrfunc)Xxo_getattr, /*tp_getattr*/
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
145 (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
146 0, /*tp_compare*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
147 0, /*tp_repr*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
148 0, /*tp_as_number*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
149 0, /*tp_as_sequence*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
150 0, /*tp_as_mapping*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
151 0, /*tp_hash*/
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
152 };
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
153 /* --------------------------------------------------------------------- */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 /* Function of two integers returning integer */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
157 static PyObject *
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 xx_foo(self, args)
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
159 PyObject *self; /* Not used */
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
160 PyObject *args;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 {
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 long i, j;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 long res;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
164 if (!PyArg_ParseTuple(args, "ll", &i, &j))
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 return NULL;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 res = i+j; /* XXX Do something here */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
167 return PyInt_FromLong(res);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
171 /* Function of no arguments returning new Xxo object */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
173 static PyObject *
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
174 xx_new(self, args)
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
175 PyObject *self; /* Not used */
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
176 PyObject *args;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
178 XxoObject *rv;
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
179
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
180 if (!PyArg_ParseTuple(args, ""))
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
182 rv = newXxoObject(args);
1915
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
183 if ( rv == NULL )
80818c106e3e * Modules/xxmodule.c: integrated with xxobject.c by Jack
Guido van Rossum <guido@python.org>
parents: 1473
diff changeset
184 return NULL;
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
185 return (PyObject *)rv;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
4714
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
188 /* 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
189
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
190 static PyObject *
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
191 xx_bug(self, args)
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
192 PyObject *self;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
193 PyObject *args;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
194 {
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
195 PyObject *list, *item;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
196
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
197 if (!PyArg_ParseTuple(args, "O", &list))
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
198 return NULL;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
199
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
200 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
201 /* Py_INCREF(item); */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
202 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
203 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
204 printf("\n");
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
205 /* Py_DECREF(item); */
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
206
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
207 Py_INCREF(Py_None);
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
208 return Py_None;
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
209 }
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
210
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 /* List of functions defined in the module */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
214 static PyMethodDef xx_methods[] = {
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
215 {"foo", xx_foo, 1},
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
216 {"new", xx_new, 1},
4714
0c6c69b0d3e9 Added the example "thin ice" from the extensions manual.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
217 {"bug", xx_bug, 1},
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 {NULL, NULL} /* sentinel */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 /* Initialization function for the module (*must* be called initxx) */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 void
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 initxx()
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 {
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
227 PyObject *m, *d;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 /* Create the module and add the functions */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
230 m = Py_InitModule("xx", xx_methods);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 /* Add some symbolic constants to the module */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
233 d = PyModule_GetDict(m);
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
234 ErrorObject = PyString_FromString("xx.error");
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
235 PyDict_SetItemString(d, "error", ErrorObject);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 /* Check for errors */
3845
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
238 if (PyErr_Occurred())
d19338fd38eb Converted to new style
Guido van Rossum <guido@python.org>
parents: 2377
diff changeset
239 Py_FatalError("can't initialize module xx");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 }