Last set of CW Pro 5 projects (probably)
[python/dscho.git] / Objects / cobject.c
blob48abb4a645d7ff1a500fe98c13cf7d1a76b1796f
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.
5 All rights reserved.
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 */
13 #include "Python.h"
16 /* Declarations for objects of type PyCObject */
18 typedef void (*destructor1)(void *);
19 typedef void (*destructor2)(void *, void*);
21 typedef struct {
22 PyObject_HEAD
23 void *cobject;
24 void *desc;
25 void (*destructor)(void *);
26 } PyCObject;
28 PyObject *
29 PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
31 PyCObject *self;
33 self = PyObject_NEW(PyCObject, &PyCObject_Type);
34 if (self == NULL)
35 return NULL;
36 self->cobject=cobj;
37 self->destructor=destr;
38 self->desc=NULL;
40 return (PyObject *)self;
43 PyObject *
44 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
45 void (*destr)(void *, void *))
47 PyCObject *self;
49 if (!desc) {
50 PyErr_SetString(PyExc_TypeError,
51 "PyCObject_FromVoidPtrAndDesc called with null"
52 " description");
53 return NULL;
55 self = PyObject_NEW(PyCObject, &PyCObject_Type);
56 if (self == NULL)
57 return NULL;
58 self->cobject=cobj;
59 self->destructor=(destructor1)destr;
60 self->desc=desc;
62 return (PyObject *)self;
65 void *
66 PyCObject_AsVoidPtr(PyObject *self)
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");
74 if (!PyErr_Occurred())
75 PyErr_SetString(PyExc_TypeError,
76 "PyCObject_AsVoidPtr called with null pointer");
77 return NULL;
80 void *
81 PyCObject_GetDesc(PyObject *self)
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");
89 if (!PyErr_Occurred())
90 PyErr_SetString(PyExc_TypeError,
91 "PyCObject_GetDesc called with null pointer");
92 return NULL;
95 void *
96 PyCObject_Import(char *module_name, char *name)
98 PyObject *m, *c;
99 void *r = NULL;
101 if ((m = PyImport_ImportModule(module_name))) {
102 if ((c = PyObject_GetAttrString(m,name))) {
103 r = PyCObject_AsVoidPtr(c);
104 Py_DECREF(c);
106 Py_DECREF(m);
108 return r;
111 static void
112 PyCObject_dealloc(PyCObject *self)
114 if (self->destructor) {
115 if(self->desc)
116 ((destructor2)(self->destructor))(self->cobject, self->desc);
117 else
118 (self->destructor)(self->cobject);
120 PyObject_DEL(self);
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)
134 0, /*ob_size*/
135 "PyCObject", /*tp_name*/
136 sizeof(PyCObject), /*tp_basicsize*/
137 0, /*tp_itemsize*/
138 /* methods */
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*/
145 0, /*tp_as_number*/
146 0, /*tp_as_sequence*/
147 0, /*tp_as_mapping*/
148 (hashfunc)0, /*tp_hash*/
149 (ternaryfunc)0, /*tp_call*/
150 (reprfunc)0, /*tp_str*/
152 /* Space for future expansion */
153 0L,0L,0L,0L,
154 PyCObject_Type__doc__ /* Documentation string */