Mercurial > cpython-lru_cache-weakref
annotate Objects/cobject.c @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 4ee682fbec30 |
| children |
| 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 |
|
60157
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
12 static int cobject_deprecation_warning(void) |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
13 { |
|
65948
4ee682fbec30
CObject use is marked as a Py3k warning, not a deprecation warning
Jesus Cea <jcea@jcea.es>
parents:
60157
diff
changeset
|
14 return PyErr_WarnPy3k("CObject type is not supported in 3.x. " |
|
60157
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
15 "Please use capsule objects instead.", 1); |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
16 } |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
17 |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
18 |
|
3315
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 | |
|
60157
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
24 if (cobject_deprecation_warning()) { |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
25 return NULL; |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
26 } |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
27 |
| 13450 | 28 self = PyObject_NEW(PyCObject, &PyCObject_Type); |
| 29 if (self == NULL) | |
| 30 return NULL; | |
| 31 self->cobject=cobj; | |
| 32 self->destructor=destr; | |
| 33 self->desc=NULL; | |
| 34 | |
| 35 return (PyObject *)self; | |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
36 } |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
37 |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
38 PyObject * |
| 13450 | 39 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
| 40 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
|
41 { |
| 13450 | 42 PyCObject *self; |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
43 |
|
60157
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
44 if (cobject_deprecation_warning()) { |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
45 return NULL; |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
46 } |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
47 |
| 13450 | 48 if (!desc) { |
| 49 PyErr_SetString(PyExc_TypeError, | |
| 50 "PyCObject_FromVoidPtrAndDesc called with null" | |
| 51 " description"); | |
| 52 return NULL; | |
| 53 } | |
| 54 self = PyObject_NEW(PyCObject, &PyCObject_Type); | |
| 55 if (self == NULL) | |
| 56 return NULL; | |
| 29814 | 57 self->cobject = cobj; |
| 58 self->destructor = (destructor1)destr; | |
| 59 self->desc = desc; | |
| 13450 | 60 |
| 61 return (PyObject *)self; | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
64 void * |
| 13450 | 65 PyCObject_AsVoidPtr(PyObject *self) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
66 { |
| 13450 | 67 if (self) { |
|
60157
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
68 if (PyCapsule_CheckExact(self)) { |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
69 const char *name = PyCapsule_GetName(self); |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
70 return (void *)PyCapsule_GetPointer(self, name); |
|
57f4c99082df
Capsule-related changes:
Larry Hastings <larry@hastings.org>
parents:
48889
diff
changeset
|
71 } |
| 13450 | 72 if (self->ob_type == &PyCObject_Type) |
| 73 return ((PyCObject *)self)->cobject; | |
| 74 PyErr_SetString(PyExc_TypeError, | |
| 75 "PyCObject_AsVoidPtr with non-C-object"); | |
| 76 } | |
| 77 if (!PyErr_Occurred()) | |
| 78 PyErr_SetString(PyExc_TypeError, | |
| 79 "PyCObject_AsVoidPtr called with null pointer"); | |
| 80 return NULL; | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
81 } |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
82 |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
83 void * |
| 13450 | 84 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
|
85 { |
| 13450 | 86 if (self) { |
| 87 if (self->ob_type == &PyCObject_Type) | |
| 88 return ((PyCObject *)self)->desc; | |
| 89 PyErr_SetString(PyExc_TypeError, | |
| 90 "PyCObject_GetDesc with non-C-object"); | |
| 91 } | |
| 92 if (!PyErr_Occurred()) | |
| 93 PyErr_SetString(PyExc_TypeError, | |
| 94 "PyCObject_GetDesc called with null pointer"); | |
| 95 return NULL; | |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
96 } |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
97 |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
98 void * |
| 13450 | 99 PyCObject_Import(char *module_name, char *name) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
100 { |
| 13450 | 101 PyObject *m, *c; |
| 102 void *r = NULL; | |
| 103 | |
| 104 if ((m = PyImport_ImportModule(module_name))) { | |
| 105 if ((c = PyObject_GetAttrString(m,name))) { | |
| 106 r = PyCObject_AsVoidPtr(c); | |
| 107 Py_DECREF(c); | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
108 } |
| 13450 | 109 Py_DECREF(m); |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
110 } |
| 13450 | 111 return r; |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
112 } |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
113 |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
114 int |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
115 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
|
116 { |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
117 PyCObject* cself = (PyCObject*)self; |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
118 if (cself == NULL || !PyCObject_Check(cself) || |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
119 cself->destructor != NULL) { |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
120 PyErr_SetString(PyExc_TypeError, |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
121 "Invalid call to PyCObject_SetVoidPtr"); |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
122 return 0; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
123 } |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
124 cself->cobject = cobj; |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
125 return 1; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
126 } |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
127 |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 static void |
| 13450 | 129 PyCObject_dealloc(PyCObject *self) |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 { |
| 13450 | 131 if (self->destructor) { |
| 132 if(self->desc) | |
| 133 ((destructor2)(self->destructor))(self->cobject, self->desc); | |
| 134 else | |
| 135 (self->destructor)(self->cobject); | |
| 136 } | |
| 137 PyObject_DEL(self); | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
140 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
141 PyDoc_STRVAR(PyCObject_Type__doc__, |
|
3910
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
142 "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
|
143 \n\ |
|
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 mechanism to link to one another."); |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 PyTypeObject PyCObject_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
37304
diff
changeset
|
150 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
151 "PyCObject", /*tp_name*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
152 sizeof(PyCObject), /*tp_basicsize*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
153 0, /*tp_itemsize*/ |
| 13450 | 154 /* methods */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
155 (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
156 0, /*tp_print*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
157 0, /*tp_getattr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
158 0, /*tp_setattr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
159 0, /*tp_compare*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
160 0, /*tp_repr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
161 0, /*tp_as_number*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
162 0, /*tp_as_sequence*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
163 0, /*tp_as_mapping*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
164 0, /*tp_hash*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
165 0, /*tp_call*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
166 0, /*tp_str*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
167 0, /*tp_getattro*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
168 0, /*tp_setattro*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
169 0, /*tp_as_buffer*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
170 0, /*tp_flags*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
171 PyCObject_Type__doc__ /*tp_doc*/ |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 }; |
