Mercurial > cpython-lru_cache-weakref
annotate Objects/cobject.c @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | bbffe0914c56 |
| children | 87fbbf22adf3 57f4c99082df |
| 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 PyObject * |
| 13450 | 13 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
|
14 { |
| 13450 | 15 PyCObject *self; |
| 16 | |
| 17 self = PyObject_NEW(PyCObject, &PyCObject_Type); | |
| 18 if (self == NULL) | |
| 19 return NULL; | |
| 20 self->cobject=cobj; | |
| 21 self->destructor=destr; | |
| 22 self->desc=NULL; | |
| 23 | |
| 24 return (PyObject *)self; | |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
25 } |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
26 |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
27 PyObject * |
| 13450 | 28 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
| 29 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
|
30 { |
| 13450 | 31 PyCObject *self; |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
32 |
| 13450 | 33 if (!desc) { |
| 34 PyErr_SetString(PyExc_TypeError, | |
| 35 "PyCObject_FromVoidPtrAndDesc called with null" | |
| 36 " description"); | |
| 37 return NULL; | |
| 38 } | |
| 39 self = PyObject_NEW(PyCObject, &PyCObject_Type); | |
| 40 if (self == NULL) | |
| 41 return NULL; | |
| 29814 | 42 self->cobject = cobj; |
| 43 self->destructor = (destructor1)destr; | |
| 44 self->desc = desc; | |
| 13450 | 45 |
| 46 return (PyObject *)self; | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
49 void * |
| 13450 | 50 PyCObject_AsVoidPtr(PyObject *self) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
51 { |
| 13450 | 52 if (self) { |
| 53 if (self->ob_type == &PyCObject_Type) | |
| 54 return ((PyCObject *)self)->cobject; | |
| 55 PyErr_SetString(PyExc_TypeError, | |
| 56 "PyCObject_AsVoidPtr with non-C-object"); | |
| 57 } | |
| 58 if (!PyErr_Occurred()) | |
| 59 PyErr_SetString(PyExc_TypeError, | |
| 60 "PyCObject_AsVoidPtr called with null pointer"); | |
| 61 return NULL; | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
62 } |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
63 |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
64 void * |
| 13450 | 65 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
|
66 { |
| 13450 | 67 if (self) { |
| 68 if (self->ob_type == &PyCObject_Type) | |
| 69 return ((PyCObject *)self)->desc; | |
| 70 PyErr_SetString(PyExc_TypeError, | |
| 71 "PyCObject_GetDesc with non-C-object"); | |
| 72 } | |
| 73 if (!PyErr_Occurred()) | |
| 74 PyErr_SetString(PyExc_TypeError, | |
| 75 "PyCObject_GetDesc called with null pointer"); | |
| 76 return NULL; | |
|
6509
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
77 } |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
78 |
|
f022bbd12338
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
79 void * |
| 13450 | 80 PyCObject_Import(char *module_name, char *name) |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
81 { |
| 13450 | 82 PyObject *m, *c; |
| 83 void *r = NULL; | |
| 84 | |
| 85 if ((m = PyImport_ImportModule(module_name))) { | |
| 86 if ((c = PyObject_GetAttrString(m,name))) { | |
| 87 r = PyCObject_AsVoidPtr(c); | |
| 88 Py_DECREF(c); | |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
89 } |
| 13450 | 90 Py_DECREF(m); |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
91 } |
| 13450 | 92 return r; |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
93 } |
|
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
94 |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
95 int |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
96 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
|
97 { |
|
36394
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
98 PyCObject* cself = (PyCObject*)self; |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
99 if (cself == NULL || !PyCObject_Check(cself) || |
|
74a598964c96
Fix SF #1412837, compile failed with Watcom compiler
Neal Norwitz <nnorwitz@gmail.com>
parents:
30716
diff
changeset
|
100 cself->destructor != NULL) { |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
101 PyErr_SetString(PyExc_TypeError, |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
102 "Invalid call to PyCObject_SetVoidPtr"); |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
103 return 0; |
|
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 cself->cobject = cobj; |
|
30716
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
106 return 1; |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
107 } |
|
07a0a0726649
Make CObjects mutable. Fixes #477441.
Martin v. Löwis <martin@v.loewis.de>
parents:
29814
diff
changeset
|
108 |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 static void |
| 13450 | 110 PyCObject_dealloc(PyCObject *self) |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 { |
| 13450 | 112 if (self->destructor) { |
| 113 if(self->desc) | |
| 114 ((destructor2)(self->destructor))(self->cobject, self->desc); | |
| 115 else | |
| 116 (self->destructor)(self->cobject); | |
| 117 } | |
| 118 PyObject_DEL(self); | |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 } |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
5033
5b7b703dabfa
Added PyCObject_Import.
Guido van Rossum <guido@python.org>
parents:
4584
diff
changeset
|
121 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
122 PyDoc_STRVAR(PyCObject_Type__doc__, |
|
3910
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
123 "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
|
124 \n\ |
|
9cf11af3628c
Allow compilation by K&R C compiler.
Guido van Rossum <guido@python.org>
parents:
3315
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 mechanism to link to one another."); |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 PyTypeObject PyCObject_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
37304
diff
changeset
|
131 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
132 "PyCObject", /*tp_name*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
133 sizeof(PyCObject), /*tp_basicsize*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
134 0, /*tp_itemsize*/ |
| 13450 | 135 /* methods */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
136 (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
137 0, /*tp_print*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
138 0, /*tp_getattr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
139 0, /*tp_setattr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
140 0, /*tp_compare*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
141 0, /*tp_repr*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
142 0, /*tp_as_number*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
143 0, /*tp_as_sequence*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
144 0, /*tp_as_mapping*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
145 0, /*tp_hash*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
146 0, /*tp_call*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
147 0, /*tp_str*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
148 0, /*tp_getattro*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
149 0, /*tp_setattro*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
150 0, /*tp_as_buffer*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
151 0, /*tp_flags*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36394
diff
changeset
|
152 PyCObject_Type__doc__ /*tp_doc*/ |
|
3315
44f48aaca8bf
opaque C object a la Jim Fulton
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 }; |
