Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f84449

Browse files
committed
New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and
PyCObject_GetDesc().
1 parent 16cb6f4 commit 1f84449

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

Include/cobject.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,27 @@ extern DL_IMPORT(PyTypeObject) PyCObject_Type;
5454
destroyed.
5555
5656
*/
57-
5857
extern PyObject *
5958
PyCObject_FromVoidPtr Py_PROTO((void *cobj, void (*destruct)(void*)));
6059

60+
61+
/* Create a PyCObject from a pointer to a C object, a description object,
62+
and an optional destrutor function. If the third argument is non-null,
63+
then it will be called with the first and second arguments if and when
64+
the PyCObject is destroyed.
65+
*/
66+
extern PyObject *
67+
PyCObject_FromVoidPtrAndDesc Py_PROTO((void *cobj, void *desc,
68+
void (*destruct)(void*,void*)));
69+
6170
/* Retrieve a pointer to a C object from a PyCObject. */
6271
extern void *
6372
PyCObject_AsVoidPtr Py_PROTO((PyObject *));
6473

74+
/* Retrieve a pointer to a description object from a PyCObject. */
75+
extern void *
76+
PyCObject_GetDesc Py_PROTO((PyObject *));
77+
6578
/* Import a pointer to a C object from a module using a PyCObject. */
6679
extern void *
6780
PyCObject_Import Py_PROTO((char *module_name, char *cobject_name));

Objects/cobject.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ PERFORMANCE OF THIS SOFTWARE.
3636

3737
/* Declarations for objects of type PyCObject */
3838

39+
typedef void (*destructor1) Py_PROTO((void *));
40+
typedef void (*destructor2) Py_PROTO((void *, void*));
41+
3942
typedef struct {
4043
PyObject_HEAD
4144
void *cobject;
45+
void *desc;
4246
void (*destructor) Py_PROTO((void *));
4347
} PyCObject;
4448

@@ -54,6 +58,30 @@ PyCObject_FromVoidPtr(cobj, destr)
5458
return NULL;
5559
self->cobject=cobj;
5660
self->destructor=destr;
61+
self->desc=NULL;
62+
return (PyObject *)self;
63+
}
64+
65+
PyObject *
66+
PyCObject_FromVoidPtrAndDesc(cobj, desc, destr)
67+
void *cobj;
68+
void *desc;
69+
void (*destr) Py_PROTO((void *, void *));
70+
{
71+
PyCObject *self;
72+
73+
if(!desc) {
74+
PyErr_SetString(PyExc_TypeError,
75+
"PyCObject_FromVoidPtrAndDesc called with null description");
76+
return NULL;
77+
}
78+
79+
self = PyObject_NEW(PyCObject, &PyCObject_Type);
80+
if (self == NULL)
81+
return NULL;
82+
self->cobject=cobj;
83+
self->destructor=(destructor1)destr;
84+
self->desc=desc;
5785
return (PyObject *)self;
5886
}
5987

@@ -74,6 +102,23 @@ PyCObject_AsVoidPtr(self)
74102
return NULL;
75103
}
76104

105+
void *
106+
PyCObject_GetDesc(self)
107+
PyObject *self;
108+
{
109+
if(self)
110+
{
111+
if(self->ob_type == &PyCObject_Type)
112+
return ((PyCObject *)self)->desc;
113+
PyErr_SetString(PyExc_TypeError,
114+
"PyCObject_GetDesc with non-C-object");
115+
}
116+
if(! PyErr_Occurred())
117+
PyErr_SetString(PyExc_TypeError,
118+
"PyCObject_GetDesc called with null pointer");
119+
return NULL;
120+
}
121+
77122
void *
78123
PyCObject_Import(module_name, name)
79124
char *module_name;
@@ -99,7 +144,13 @@ static void
99144
PyCObject_dealloc(self)
100145
PyCObject *self;
101146
{
102-
if(self->destructor) (self->destructor)(self->cobject);
147+
if(self->destructor)
148+
{
149+
if(self->desc)
150+
((destructor2)(self->destructor))(self->cobject, self->desc);
151+
else
152+
(self->destructor)(self->cobject);
153+
}
103154
PyMem_DEL(self);
104155
}
105156

0 commit comments

Comments
 (0)