1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Wrap void* pointers to be passed between C modules */
16 /* Declarations for objects of type PyCObject */
18 typedef void (*destructor1
)(void *);
19 typedef void (*destructor2
)(void *, void*);
25 void (*destructor
)(void *);
29 PyCObject_FromVoidPtr(void *cobj
, void (*destr
)(void *))
33 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
37 self
->destructor
=destr
;
40 return (PyObject
*)self
;
44 PyCObject_FromVoidPtrAndDesc(void *cobj
, void *desc
,
45 void (*destr
)(void *, void *))
50 PyErr_SetString(PyExc_TypeError
,
51 "PyCObject_FromVoidPtrAndDesc called with null"
55 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
59 self
->destructor
=(destructor1
)destr
;
62 return (PyObject
*)self
;
66 PyCObject_AsVoidPtr(PyObject
*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");
74 if (!PyErr_Occurred())
75 PyErr_SetString(PyExc_TypeError
,
76 "PyCObject_AsVoidPtr called with null pointer");
81 PyCObject_GetDesc(PyObject
*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");
89 if (!PyErr_Occurred())
90 PyErr_SetString(PyExc_TypeError
,
91 "PyCObject_GetDesc called with null pointer");
96 PyCObject_Import(char *module_name
, char *name
)
101 if ((m
= PyImport_ImportModule(module_name
))) {
102 if ((c
= PyObject_GetAttrString(m
,name
))) {
103 r
= PyCObject_AsVoidPtr(c
);
112 PyCObject_dealloc(PyCObject
*self
)
114 if (self
->destructor
) {
116 ((destructor2
)(self
->destructor
))(self
->cobject
, self
->desc
);
118 (self
->destructor
)(self
->cobject
);
124 static char PyCObject_Type__doc__
[] =
125 "C objects to be exported from one extension module to another\n\
127 C objects are used for communication between extension modules. They\n\
128 provide a way for an extension module to export a C interface to other\n\
129 extension modules, so that extension modules can use the Python import\n\
130 mechanism to link to one another.";
132 PyTypeObject PyCObject_Type
= {
133 PyObject_HEAD_INIT(&PyType_Type
)
135 "PyCObject", /*tp_name*/
136 sizeof(PyCObject
), /*tp_basicsize*/
139 (destructor
)PyCObject_dealloc
, /*tp_dealloc*/
140 (printfunc
)0, /*tp_print*/
141 (getattrfunc
)0, /*tp_getattr*/
142 (setattrfunc
)0, /*tp_setattr*/
143 (cmpfunc
)0, /*tp_compare*/
144 (reprfunc
)0, /*tp_repr*/
146 0, /*tp_as_sequence*/
148 (hashfunc
)0, /*tp_hash*/
149 (ternaryfunc
)0, /*tp_call*/
150 (reprfunc
)0, /*tp_str*/
152 /* Space for future expansion */
154 PyCObject_Type__doc__
/* Documentation string */