Mercurial > cpython-lru_cache-weakref
annotate Objects/cobject.c @ 64166:d0e9febfa00a 3.1
Merged revisions 84172 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines
Restore GIL in nis_cat in case of error.
........
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 19 Aug 2010 09:03:03 +0000 |
| parents | 7cb6df00699f |
| 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 |
|
53848
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
12 |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
13 static int deprecation_exception(void) |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
14 { |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
15 return PyErr_WarnEx(PyExc_PendingDeprecationWarning, |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
16 "The CObject API is deprecated as of Python 3.1. " |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
17 "Please convert to using the Capsule API.", 1); |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
18 } |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
19 |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 PyObject * |
| 13450 | 21 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
|
22 { |
| 13450 | 23 PyCObject *self; |
| 24 | |
|
53848
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
25 if (deprecation_exception()) { |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
26 return NULL; |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
27 } |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
28 |
| 13450 | 29 self = PyObject_NEW(PyCObject, &PyCObject_Type); |
| 30 if (self == NULL) | |
| 31 return NULL; | |
| 32 self->cobject=cobj; | |
| 33 self->destructor=destr; | |
| 34 self->desc=NULL; | |
| 35 | |
| 36 return (PyObject *)self; | |
|
6509
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 |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
39 PyObject * |
| 13450 | 40 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
| 41 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
|
42 { |
| 13450 | 43 PyCObject *self; |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
44 |
|
53848
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
45 if (deprecation_exception()) { |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
46 return NULL; |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
47 } |
|
7cb6df00699f
#5947: add PendingDeprecationWarning to PyCObject functions.
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
48 |
| 13450 | 49 if (!desc) { |
| 50 PyErr_SetString(PyExc_TypeError, | |
| 51 "PyCObject_FromVoidPtrAndDesc called with null" | |
| 52 " description"); | |
| 53 return NULL; | |
| 54 } | |
| 55 self = PyObject_NEW(PyCObject, &PyCObject_Type); | |
| 56 if (self == NULL) | |
| 57 return NULL; | |
| 29814 | 58 self->cobject = cobj; |
| 59 self->destructor = (destructor1)destr; | |
| 60 self->desc = desc; | |
| 13450 | 61 |
| 62 return (PyObject *)self; | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
65 void * |
| 13450 | 66 PyCObject_AsVoidPtr(PyObject *self) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
67 { |
| 13450 | 68 if (self) { |
| 69 if (self->ob_type == &PyCObject_Type) | |
| 70 return ((PyCObject *)self)->cobject; | |
| 71 PyErr_SetString(PyExc_TypeError, | |
| 72 "PyCObject_AsVoidPtr with non-C-object"); | |
| 73 } | |
| 74 if (!PyErr_Occurred()) | |
| 75 PyErr_SetString(PyExc_TypeError, | |
| 76 "PyCObject_AsVoidPtr called with null pointer"); | |
| 77 return NULL; | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
78 } |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
79 |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
80 void * |
| 13450 | 81 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
|
82 { |
| 13450 | 83 if (self) { |
| 84 if (self->ob_type == &PyCObject_Type) | |
| 85 return ((PyCObject *)self)->desc; | |
| 86 PyErr_SetString(PyExc_TypeError, | |
| 87 "PyCObject_GetDesc with non-C-object"); | |
| 88 } | |
| 89 if (!PyErr_Occurred()) | |
| 90 PyErr_SetString(PyExc_TypeError, | |
| 91 "PyCObject_GetDesc called with null pointer"); | |
| 92 return NULL; | |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
93 } |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
94 |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
95 void * |
| 13450 | 96 PyCObject_Import(char *module_name, char *name) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
97 { |
| 13450 | 98 PyObject *m, *c; |
| 99 void *r = NULL; | |
| 100 | |
| 101 if ((m = PyImport_ImportModule(module_name))) { | |
| 102 if ((c = PyObject_GetAttrString(m,name))) { | |
| 103 r = PyCObject_AsVoidPtr(c); | |
| 104 Py_DECREF(c); | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
105 } |
| 13450 | 106 Py_DECREF(m); |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
107 } |
| 13450 | 108 return r; |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
109 } |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
110 |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
111 int |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
112 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
|
113 { |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
114 PyCObject* cself = (PyCObject*)self; |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
115 if (cself == NULL || !PyCObject_Check(cself) || |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
116 cself->destructor != NULL) { |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
117 PyErr_SetString(PyExc_TypeError, |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
118 "Invalid call to PyCObject_SetVoidPtr"); |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
119 return 0; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
120 } |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
121 cself->cobject = cobj; |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
122 return 1; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
123 } |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
124 |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 static void |
| 13450 | 126 PyCObject_dealloc(PyCObject *self) |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 { |
| 13450 | 128 if (self->destructor) { |
| 129 if(self->desc) | |
| 130 ((destructor2)(self->destructor))(self->cobject, self->desc); | |
| 131 else | |
| 132 (self->destructor)(self->cobject); | |
| 133 } | |
| 134 PyObject_DEL(self); | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
137 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
138 PyDoc_STRVAR(PyCObject_Type__doc__, |
|
3910
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
139 "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
|
140 \n\ |
|
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 mechanism to link to one another."); |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 PyTypeObject PyCObject_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
37879
diff
changeset
|
147 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
148 "PyCObject", /*tp_name*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
149 sizeof(PyCObject), /*tp_basicsize*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
150 0, /*tp_itemsize*/ |
| 13450 | 151 /* methods */ |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
152 (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
153 0, /*tp_print*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
154 0, /*tp_getattr*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
155 0, /*tp_setattr*/ |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48888
diff
changeset
|
156 0, /*tp_reserved*/ |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
157 0, /*tp_repr*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
158 0, /*tp_as_number*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
159 0, /*tp_as_sequence*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
160 0, /*tp_as_mapping*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
161 0, /*tp_hash*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
162 0, /*tp_call*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
163 0, /*tp_str*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
164 0, /*tp_getattro*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
165 0, /*tp_setattro*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
166 0, /*tp_as_buffer*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
167 0, /*tp_flags*/ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36394
diff
changeset
|
168 PyCObject_Type__doc__ /*tp_doc*/ |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 }; |
