Mercurial > cpython-lru_cache-weakref
annotate Objects/cobject.c @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 79a0f080f898 |
| children | e70cdce0e8b2 |
| 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 | 13 PyObject_HEAD |
| 14 void *cobject; | |
| 15 void *desc; | |
| 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 | 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 | 22 PyCObject *self; |
| 23 | |
| 24 self = PyObject_NEW(PyCObject, &PyCObject_Type); | |
| 25 if (self == NULL) | |
| 26 return NULL; | |
| 27 self->cobject=cobj; | |
| 28 self->destructor=destr; | |
| 29 self->desc=NULL; | |
| 30 | |
| 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 | 35 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
| 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 | 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 | 40 if (!desc) { |
| 41 PyErr_SetString(PyExc_TypeError, | |
| 42 "PyCObject_FromVoidPtrAndDesc called with null" | |
| 43 " description"); | |
| 44 return NULL; | |
| 45 } | |
| 46 self = PyObject_NEW(PyCObject, &PyCObject_Type); | |
| 47 if (self == NULL) | |
| 48 return NULL; | |
| 29814 | 49 self->cobject = cobj; |
| 50 self->destructor = (destructor1)destr; | |
| 51 self->desc = desc; | |
| 13450 | 52 |
| 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 | 57 PyCObject_AsVoidPtr(PyObject *self) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
58 { |
| 13450 | 59 if (self) { |
| 60 if (self->ob_type == &PyCObject_Type) | |
| 61 return ((PyCObject *)self)->cobject; | |
| 62 PyErr_SetString(PyExc_TypeError, | |
| 63 "PyCObject_AsVoidPtr with non-C-object"); | |
| 64 } | |
| 65 if (!PyErr_Occurred()) | |
| 66 PyErr_SetString(PyExc_TypeError, | |
| 67 "PyCObject_AsVoidPtr called with null pointer"); | |
| 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 | 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 | 74 if (self) { |
| 75 if (self->ob_type == &PyCObject_Type) | |
| 76 return ((PyCObject *)self)->desc; | |
| 77 PyErr_SetString(PyExc_TypeError, | |
| 78 "PyCObject_GetDesc with non-C-object"); | |
| 79 } | |
| 80 if (!PyErr_Occurred()) | |
| 81 PyErr_SetString(PyExc_TypeError, | |
| 82 "PyCObject_GetDesc called with null pointer"); | |
| 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 | 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 | 89 PyObject *m, *c; |
| 90 void *r = NULL; | |
| 91 | |
| 92 if ((m = PyImport_ImportModule(module_name))) { | |
| 93 if ((c = PyObject_GetAttrString(m,name))) { | |
| 94 r = PyCObject_AsVoidPtr(c); | |
| 95 Py_DECREF(c); | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
96 } |
| 13450 | 97 Py_DECREF(m); |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
98 } |
| 13450 | 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 |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
102 int |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
103 PyCObject_SetVoidPtr(PyObject *self, void *cobj) |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
104 { |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
105 PyCObject* cself = (PyCObject*)self; |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
106 if (cself == NULL || !PyCObject_Check(cself) || |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
107 cself->destructor != NULL) { |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
108 PyErr_SetString(PyExc_TypeError, |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
109 "Invalid call to PyCObject_SetVoidPtr"); |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
110 return 0; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
111 } |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
112 cself->cobject = cobj; |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
113 return 1; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
114 } |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
115 |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 static void |
| 13450 | 117 PyCObject_dealloc(PyCObject *self) |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 { |
| 13450 | 119 if (self->destructor) { |
| 120 if(self->desc) | |
| 121 ((destructor2)(self->destructor))(self->cobject, self->desc); | |
| 122 else | |
| 123 (self->destructor)(self->cobject); | |
| 124 } | |
| 125 PyObject_DEL(self); | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
128 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
129 PyDoc_STRVAR(PyCObject_Type__doc__, |
|
3910
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
130 "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
|
131 \n\ |
|
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
132 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
|
133 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
|
134 extension modules, so that extension modules can use the Python import\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
135 mechanism to link to one another."); |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 PyTypeObject PyCObject_Type = { |
| 13450 | 138 PyObject_HEAD_INIT(&PyType_Type) |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
139 0, /*ob_size*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
140 "PyCObject", /*tp_name*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
141 sizeof(PyCObject), /*tp_basicsize*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
142 0, /*tp_itemsize*/ |
| 13450 | 143 /* methods */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
144 (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
145 0, /*tp_print*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
146 0, /*tp_getattr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
147 0, /*tp_setattr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
148 0, /*tp_compare*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
149 0, /*tp_repr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
150 0, /*tp_as_number*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
151 0, /*tp_as_sequence*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
152 0, /*tp_as_mapping*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
153 0, /*tp_hash*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
154 0, /*tp_call*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
155 0, /*tp_str*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
156 0, /*tp_getattro*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
157 0, /*tp_setattro*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
158 0, /*tp_as_buffer*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
159 0, /*tp_flags*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
160 PyCObject_Type__doc__ /*tp_doc*/ |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 }; |
