blob: 06c29f00ce4db1e89d98aa2262ac335e782f6e44 [file] [log] [blame]
Guido van Rossum77654a71996-01-12 00:44:03 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum77654a71996-01-12 00:44:03 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum77654a71996-01-12 00:44:03 +00009******************************************************************/
10
11/* Wrap void* pointers to be passed between C modules */
12
13#include "Python.h"
14
15
16/* Declarations for objects of type PyCObject */
17
Tim Petersdbd9ba62000-07-09 03:09:57 +000018typedef void (*destructor1)(void *);
19typedef void (*destructor2)(void *, void*);
Guido van Rossum1f844491997-10-21 19:48:35 +000020
Guido van Rossum77654a71996-01-12 00:44:03 +000021typedef struct {
22 PyObject_HEAD
23 void *cobject;
Guido van Rossum1f844491997-10-21 19:48:35 +000024 void *desc;
Tim Petersdbd9ba62000-07-09 03:09:57 +000025 void (*destructor)(void *);
Guido van Rossum77654a71996-01-12 00:44:03 +000026} PyCObject;
27
28PyObject *
29PyCObject_FromVoidPtr(cobj, destr)
30 void *cobj;
Tim Petersdbd9ba62000-07-09 03:09:57 +000031 void (*destr)(void *);
Guido van Rossum77654a71996-01-12 00:44:03 +000032{
33 PyCObject *self;
34
35 self = PyObject_NEW(PyCObject, &PyCObject_Type);
36 if (self == NULL)
37 return NULL;
38 self->cobject=cobj;
39 self->destructor=destr;
Guido van Rossum1f844491997-10-21 19:48:35 +000040 self->desc=NULL;
41 return (PyObject *)self;
42}
43
44PyObject *
45PyCObject_FromVoidPtrAndDesc(cobj, desc, destr)
46 void *cobj;
47 void *desc;
Tim Petersdbd9ba62000-07-09 03:09:57 +000048 void (*destr)(void *, void *);
Guido van Rossum1f844491997-10-21 19:48:35 +000049{
50 PyCObject *self;
51
52 if(!desc) {
53 PyErr_SetString(PyExc_TypeError,
54 "PyCObject_FromVoidPtrAndDesc called with null description");
55 return NULL;
56 }
57
58 self = PyObject_NEW(PyCObject, &PyCObject_Type);
59 if (self == NULL)
60 return NULL;
61 self->cobject=cobj;
62 self->destructor=(destructor1)destr;
63 self->desc=desc;
Guido van Rossum77654a71996-01-12 00:44:03 +000064 return (PyObject *)self;
65}
66
Guido van Rossume0e69621997-01-22 20:48:48 +000067void *
68PyCObject_AsVoidPtr(self)
69 PyObject *self;
70{
71 if(self)
72 {
73 if(self->ob_type == &PyCObject_Type)
74 return ((PyCObject *)self)->cobject;
75 PyErr_SetString(PyExc_TypeError,
76 "PyCObject_AsVoidPtr with non-C-object");
77 }
78 if(! PyErr_Occurred())
79 PyErr_SetString(PyExc_TypeError,
80 "PyCObject_AsVoidPtr called with null pointer");
81 return NULL;
82}
83
84void *
Guido van Rossum1f844491997-10-21 19:48:35 +000085PyCObject_GetDesc(self)
86 PyObject *self;
87{
88 if(self)
89 {
90 if(self->ob_type == &PyCObject_Type)
91 return ((PyCObject *)self)->desc;
92 PyErr_SetString(PyExc_TypeError,
93 "PyCObject_GetDesc with non-C-object");
94 }
95 if(! PyErr_Occurred())
96 PyErr_SetString(PyExc_TypeError,
97 "PyCObject_GetDesc called with null pointer");
98 return NULL;
99}
100
101void *
Guido van Rossume0e69621997-01-22 20:48:48 +0000102PyCObject_Import(module_name, name)
103 char *module_name;
104 char *name;
105{
106 PyObject *m, *c;
107 void *r=NULL;
108
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000109 if((m=PyImport_ImportModule(module_name)))
Guido van Rossume0e69621997-01-22 20:48:48 +0000110 {
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000111 if((c=PyObject_GetAttrString(m,name)))
Guido van Rossume0e69621997-01-22 20:48:48 +0000112 {
113 r=PyCObject_AsVoidPtr(c);
114 Py_DECREF(c);
115 }
116 Py_DECREF(m);
117 }
118
119 return r;
120}
121
Guido van Rossum77654a71996-01-12 00:44:03 +0000122static void
123PyCObject_dealloc(self)
124 PyCObject *self;
125{
Guido van Rossum1f844491997-10-21 19:48:35 +0000126 if(self->destructor)
127 {
128 if(self->desc)
129 ((destructor2)(self->destructor))(self->cobject, self->desc);
130 else
131 (self->destructor)(self->cobject);
132 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000133 PyObject_DEL(self);
Guido van Rossum77654a71996-01-12 00:44:03 +0000134}
135
Guido van Rossume0e69621997-01-22 20:48:48 +0000136
Guido van Rossum77654a71996-01-12 00:44:03 +0000137static char PyCObject_Type__doc__[] =
Guido van Rossumcee555b1996-08-01 00:02:33 +0000138"C objects to be exported from one extension module to another\n\
139\n\
140C objects are used for communication between extension modules. They\n\
141provide a way for an extension module to export a C interface to other\n\
142extension modules, so that extension modules can use the Python import\n\
143mechanism to link to one another.\n"
Guido van Rossum77654a71996-01-12 00:44:03 +0000144;
145
146PyTypeObject PyCObject_Type = {
147 PyObject_HEAD_INIT(&PyType_Type)
148 0, /*ob_size*/
149 "PyCObject", /*tp_name*/
150 sizeof(PyCObject), /*tp_basicsize*/
151 0, /*tp_itemsize*/
152 /* methods */
153 (destructor)PyCObject_dealloc, /*tp_dealloc*/
154 (printfunc)0, /*tp_print*/
155 (getattrfunc)0, /*tp_getattr*/
156 (setattrfunc)0, /*tp_setattr*/
157 (cmpfunc)0, /*tp_compare*/
158 (reprfunc)0, /*tp_repr*/
159 0, /*tp_as_number*/
160 0, /*tp_as_sequence*/
161 0, /*tp_as_mapping*/
162 (hashfunc)0, /*tp_hash*/
163 (ternaryfunc)0, /*tp_call*/
164 (reprfunc)0, /*tp_str*/
165
166 /* Space for future expansion */
167 0L,0L,0L,0L,
168 PyCObject_Type__doc__ /* Documentation string */
169};