Skip to content

Commit 77654a7

Browse files
committed
opaque C object a la Jim Fulton
1 parent 3a50f8a commit 77654a7

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

Include/cobject.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef Py_COBJECT_H
2+
#define Py_COBJECT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
/***********************************************************
8+
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
9+
The Netherlands.
10+
11+
All Rights Reserved
12+
13+
Permission to use, copy, modify, and distribute this software and its
14+
documentation for any purpose and without fee is hereby granted,
15+
provided that the above copyright notice appear in all copies and that
16+
both that copyright notice and this permission notice appear in
17+
supporting documentation, and that the names of Stichting Mathematisch
18+
Centrum or CWI not be used in advertising or publicity pertaining to
19+
distribution of the software without specific, written prior permission.
20+
21+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
22+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
23+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
24+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28+
29+
******************************************************************/
30+
31+
/* C objects to be exported from one extension module to another.
32+
33+
C objects are used for communication between extension modules.
34+
They provide a way for an extension module to export a C interface
35+
to other extension modules, so that extension modules can use the
36+
Python import mechanism to link to one another.
37+
38+
*/
39+
40+
extern DL_IMPORT(PyTypeObject) PyCObject_Type;
41+
42+
#define PyCObject_Check(op) ((op)->ob_type == &PyCObject_Type)
43+
44+
/* Create a PyCObject from a pointer to a C object and an optional
45+
destrutor function. If the second argument is non-null, then it
46+
will be called with the first argument if and when the PyCObject is
47+
destroyed.
48+
49+
*/
50+
51+
extern PyObject *
52+
PyCObject_FromVoidPtr Py_PROTO((void *cobj, void (*destruct)(void*)));
53+
54+
/* Retrieve a pointer to a C object from a PyCObject. */
55+
extern void *
56+
PyCObject_AsVoidPtr Py_PROTO((PyObject *));
57+
58+
#ifdef __cplusplus
59+
}
60+
#endif
61+
#endif /* !Py_COBJECT_H */

Objects/cobject.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/***********************************************************
2+
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3+
The Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
11+
supporting documentation, and that the names of Stichting Mathematisch
12+
Centrum or CWI not be used in advertising or publicity pertaining to
13+
distribution of the software without specific, written prior permission.
14+
15+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22+
23+
******************************************************************/
24+
25+
/* Wrap void* pointers to be passed between C modules */
26+
27+
#include "Python.h"
28+
29+
30+
/* Declarations for objects of type PyCObject */
31+
32+
typedef struct {
33+
PyObject_HEAD
34+
void *cobject;
35+
void (*destructor)(void *);
36+
} PyCObject;
37+
38+
PyObject *
39+
PyCObject_FromVoidPtr(cobj, destr)
40+
void *cobj;
41+
void (*destr)(void *);
42+
{
43+
PyCObject *self;
44+
45+
self = PyObject_NEW(PyCObject, &PyCObject_Type);
46+
if (self == NULL)
47+
return NULL;
48+
self->cobject=cobj;
49+
self->destructor=destr;
50+
return (PyObject *)self;
51+
}
52+
53+
void *
54+
PyCObject_AsVoidPtr(self)
55+
PyObject *self;
56+
{
57+
return ((PyCObject *)self)->cobject;
58+
}
59+
60+
static void
61+
PyCObject_dealloc(self)
62+
PyCObject *self;
63+
{
64+
if(self->destructor) (self->destructor)(self->cobject);
65+
PyMem_DEL(self);
66+
}
67+
68+
static char PyCObject_Type__doc__[] =
69+
"C objects to be exported from one extension module to another\n"
70+
"\n"
71+
"C objects are used for communication between extension modules. They\n"
72+
"provide a way for an extension module to export a C interface to other\n"
73+
"extension modules, so that extension modules can use the Python import\n"
74+
"mechanism to link to one another.\n"
75+
;
76+
77+
PyTypeObject 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+
};

0 commit comments

Comments
 (0)