annotate Objects/cobject.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 6f6f965c5e2d
children ed1ba37a46b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Wrap void* pointers to be passed between C modules */
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #include "Python.h"
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 /* Declarations for objects of type PyCObject */
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
9 typedef void (*destructor1)(void *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
10 typedef void (*destructor2)(void *, void*);
6509
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
11
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 typedef struct {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
13 PyObject_HEAD
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
14 void *cobject;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
15 void *desc;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
16 void (*destructor)(void *);
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 } PyCObject;
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 PyObject *
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
20 PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
22 PyCObject *self;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
23
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
24 self = PyObject_NEW(PyCObject, &PyCObject_Type);
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
25 if (self == NULL)
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
26 return NULL;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
27 self->cobject=cobj;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
28 self->destructor=destr;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
29 self->desc=NULL;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
30
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
31 return (PyObject *)self;
6509
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
32 }
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
33
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
34 PyObject *
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
35 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
36 void (*destr)(void *, void *))
6509
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
37 {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
38 PyCObject *self;
6509
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
39
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
40 if (!desc) {
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
41 PyErr_SetString(PyExc_TypeError,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
42 "PyCObject_FromVoidPtrAndDesc called with null"
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
43 " description");
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
44 return NULL;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
45 }
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
46 self = PyObject_NEW(PyCObject, &PyCObject_Type);
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
47 if (self == NULL)
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
48 return NULL;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
49 self->cobject=cobj;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
50 self->destructor=(destructor1)destr;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
51 self->desc=desc;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
52
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
53 return (PyObject *)self;
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 }
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
56 void *
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
57 PyCObject_AsVoidPtr(PyObject *self)
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
58 {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
59 if (self) {
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
60 if (self->ob_type == &PyCObject_Type)
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
61 return ((PyCObject *)self)->cobject;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
62 PyErr_SetString(PyExc_TypeError,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
63 "PyCObject_AsVoidPtr with non-C-object");
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
64 }
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
65 if (!PyErr_Occurred())
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
66 PyErr_SetString(PyExc_TypeError,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
67 "PyCObject_AsVoidPtr called with null pointer");
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
68 return NULL;
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
69 }
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
70
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
71 void *
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
72 PyCObject_GetDesc(PyObject *self)
6509
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
73 {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
74 if (self) {
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
75 if (self->ob_type == &PyCObject_Type)
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
76 return ((PyCObject *)self)->desc;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
77 PyErr_SetString(PyExc_TypeError,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
78 "PyCObject_GetDesc with non-C-object");
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
79 }
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
80 if (!PyErr_Occurred())
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
81 PyErr_SetString(PyExc_TypeError,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
82 "PyCObject_GetDesc called with null pointer");
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
83 return NULL;
6509
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
84 }
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
85
f022bbd12338 New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents: 5115
diff changeset
86 void *
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
87 PyCObject_Import(char *module_name, char *name)
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
88 {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
89 PyObject *m, *c;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
90 void *r = NULL;
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
91
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
92 if ((m = PyImport_ImportModule(module_name))) {
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
93 if ((c = PyObject_GetAttrString(m,name))) {
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
94 r = PyCObject_AsVoidPtr(c);
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
95 Py_DECREF(c);
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
96 }
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
97 Py_DECREF(m);
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
98 }
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
99 return r;
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
100 }
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
101
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 static void
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
103 PyCObject_dealloc(PyCObject *self)
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
105 if (self->destructor) {
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
106 if(self->desc)
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
107 ((destructor2)(self->destructor))(self->cobject, self->desc);
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
108 else
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
109 (self->destructor)(self->cobject);
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
110 }
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
111 PyObject_DEL(self);
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
5033
5b7b703dabfa Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents: 4584
diff changeset
114
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 static char PyCObject_Type__doc__[] =
3910
9cf11af3628c Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents: 3315
diff changeset
116 "C objects to be exported from one extension module to another\n\
9cf11af3628c Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents: 3315
diff changeset
117 \n\
9cf11af3628c Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents: 3315
diff changeset
118 C objects are used for communication between extension modules. They\n\
9cf11af3628c Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents: 3315
diff changeset
119 provide a way for an extension module to export a C interface to other\n\
9cf11af3628c Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents: 3315
diff changeset
120 extension modules, so that extension modules can use the Python import\n\
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
121 mechanism to link to one another.";
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 PyTypeObject PyCObject_Type = {
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
124 PyObject_HEAD_INIT(&PyType_Type)
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
125 0, /*ob_size*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
126 "PyCObject", /*tp_name*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
127 sizeof(PyCObject), /*tp_basicsize*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
128 0, /*tp_itemsize*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
129 /* methods */
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
130 (destructor)PyCObject_dealloc, /*tp_dealloc*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
131 (printfunc)0, /*tp_print*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
132 (getattrfunc)0, /*tp_getattr*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
133 (setattrfunc)0, /*tp_setattr*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
134 (cmpfunc)0, /*tp_compare*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
135 (reprfunc)0, /*tp_repr*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
136 0, /*tp_as_number*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
137 0, /*tp_as_sequence*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
138 0, /*tp_as_mapping*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
139 (hashfunc)0, /*tp_hash*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
140 (ternaryfunc)0, /*tp_call*/
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
141 (reprfunc)0, /*tp_str*/
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
13450
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
143 /* Space for future expansion */
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
144 0L,0L,0L,0L,
4ad26968662b ANSI-fication of the source.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
145 PyCObject_Type__doc__ /* Documentation string */
3315
44f48aaca8bf opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 };