blob: 4016b91c4f27395aa7220e6369727e40d0e57c33 [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 Rossum77654a71996-01-12 00:44:03 +000060static void
61PyCObject_dealloc(self)
62 PyCObject *self;
63{
64 if(self->destructor) (self->destructor)(self->cobject);
65 PyMem_DEL(self);
66}
67
68static char PyCObject_Type__doc__[] =
Guido van Rossumcee555b1996-08-01 00:02:33 +000069"C objects to be exported from one extension module to another\n\
70\n\
71C objects are used for communication between extension modules. They\n\
72provide a way for an extension module to export a C interface to other\n\
73extension modules, so that extension modules can use the Python import\n\
74mechanism to link to one another.\n"
Guido van Rossum77654a71996-01-12 00:44:03 +000075;
76
77PyTypeObject PyCObject_Type = {
78 PyObject_HEAD_INIT(&PyType_Type)
79 0, /*ob_size*/
80 "PyCObject", /*tp_name*/
81 sizeof(PyCObject), /*tp_basicsize*/
82 0, /*tp_itemsize*/
83 /* methods */
84 (destructor)PyCObject_dealloc, /*tp_dealloc*/
85 (printfunc)0, /*tp_print*/
86 (getattrfunc)0, /*tp_getattr*/
87 (setattrfunc)0, /*tp_setattr*/
88 (cmpfunc)0, /*tp_compare*/
89 (reprfunc)0, /*tp_repr*/
90 0, /*tp_as_number*/
91 0, /*tp_as_sequence*/
92 0, /*tp_as_mapping*/
93 (hashfunc)0, /*tp_hash*/
94 (ternaryfunc)0, /*tp_call*/
95 (reprfunc)0, /*tp_str*/
96
97 /* Space for future expansion */
98 0L,0L,0L,0L,
99 PyCObject_Type__doc__ /* Documentation string */
100};
Guido van Rossum04f95d51996-12-05 21:53:15 +0000101
102void *
103PyCObject_AsVoidPtr(self)
104 PyObject *self;
105{
106 if(self)
107 {
108 if(self->ob_type == &PyCObject_Type)
109 return ((PyCObject *)self)->cobject;
110 PyErr_SetString(PyExc_TypeError,
111 "PyCObject_AsVoidPtr with non-C-object");
112 }
113 if(! PyErr_Occurred())
114 PyErr_SetString(
115 PyExc_TypeError,
116 "PyCObject_AsVoidPtr called with null pointer");
117 return NULL;
118}