blob: 6b757d5acbd06e3902542f8ec2c5fb3805f8dd83 [file] [log] [blame]
Guido van Rossum77654a71996-01-12 00:44:03 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum77654a71996-01-12 00:44:03 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum77654a71996-01-12 00:44:03 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum77654a71996-01-12 00:44:03 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum77654a71996-01-12 00:44:03 +000029
30******************************************************************/
31
32/* Wrap void* pointers to be passed between C modules */
33
34#include "Python.h"
35
36
37/* Declarations for objects of type PyCObject */
38
39typedef struct {
40 PyObject_HEAD
41 void *cobject;
Guido van Rossumcee555b1996-08-01 00:02:33 +000042 void (*destructor) Py_PROTO((void *));
Guido van Rossum77654a71996-01-12 00:44:03 +000043} PyCObject;
44
45PyObject *
46PyCObject_FromVoidPtr(cobj, destr)
47 void *cobj;
Guido van Rossumcee555b1996-08-01 00:02:33 +000048 void (*destr) Py_PROTO((void *));
Guido van Rossum77654a71996-01-12 00:44:03 +000049{
50 PyCObject *self;
51
52 self = PyObject_NEW(PyCObject, &PyCObject_Type);
53 if (self == NULL)
54 return NULL;
55 self->cobject=cobj;
56 self->destructor=destr;
57 return (PyObject *)self;
58}
59
Guido van Rossume0e69621997-01-22 20:48:48 +000060void *
61PyCObject_AsVoidPtr(self)
62 PyObject *self;
63{
64 if(self)
65 {
66 if(self->ob_type == &PyCObject_Type)
67 return ((PyCObject *)self)->cobject;
68 PyErr_SetString(PyExc_TypeError,
69 "PyCObject_AsVoidPtr with non-C-object");
70 }
71 if(! PyErr_Occurred())
72 PyErr_SetString(PyExc_TypeError,
73 "PyCObject_AsVoidPtr called with null pointer");
74 return NULL;
75}
76
77void *
78PyCObject_Import(module_name, name)
79 char *module_name;
80 char *name;
81{
82 PyObject *m, *c;
83 void *r=NULL;
84
Guido van Rossum0f4bbd21997-02-14 21:12:56 +000085 if((m=PyImport_ImportModule(module_name)))
Guido van Rossume0e69621997-01-22 20:48:48 +000086 {
Guido van Rossum0f4bbd21997-02-14 21:12:56 +000087 if((c=PyObject_GetAttrString(m,name)))
Guido van Rossume0e69621997-01-22 20:48:48 +000088 {
89 r=PyCObject_AsVoidPtr(c);
90 Py_DECREF(c);
91 }
92 Py_DECREF(m);
93 }
94
95 return r;
96}
97
Guido van Rossum77654a71996-01-12 00:44:03 +000098static void
99PyCObject_dealloc(self)
100 PyCObject *self;
101{
102 if(self->destructor) (self->destructor)(self->cobject);
103 PyMem_DEL(self);
104}
105
Guido van Rossume0e69621997-01-22 20:48:48 +0000106
Guido van Rossum77654a71996-01-12 00:44:03 +0000107static char PyCObject_Type__doc__[] =
Guido van Rossumcee555b1996-08-01 00:02:33 +0000108"C objects to be exported from one extension module to another\n\
109\n\
110C objects are used for communication between extension modules. They\n\
111provide a way for an extension module to export a C interface to other\n\
112extension modules, so that extension modules can use the Python import\n\
113mechanism to link to one another.\n"
Guido van Rossum77654a71996-01-12 00:44:03 +0000114;
115
116PyTypeObject PyCObject_Type = {
117 PyObject_HEAD_INIT(&PyType_Type)
118 0, /*ob_size*/
119 "PyCObject", /*tp_name*/
120 sizeof(PyCObject), /*tp_basicsize*/
121 0, /*tp_itemsize*/
122 /* methods */
123 (destructor)PyCObject_dealloc, /*tp_dealloc*/
124 (printfunc)0, /*tp_print*/
125 (getattrfunc)0, /*tp_getattr*/
126 (setattrfunc)0, /*tp_setattr*/
127 (cmpfunc)0, /*tp_compare*/
128 (reprfunc)0, /*tp_repr*/
129 0, /*tp_as_number*/
130 0, /*tp_as_sequence*/
131 0, /*tp_as_mapping*/
132 (hashfunc)0, /*tp_hash*/
133 (ternaryfunc)0, /*tp_call*/
134 (reprfunc)0, /*tp_str*/
135
136 /* Space for future expansion */
137 0L,0L,0L,0L,
138 PyCObject_Type__doc__ /* Documentation string */
139};