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

Skip to content

Commit e0e6962

Browse files
committed
Added PyCObject_Import.
1 parent 43d287a commit e0e6962

3 files changed

Lines changed: 44 additions & 18 deletions

File tree

Include/cobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ PyCObject_FromVoidPtr Py_PROTO((void *cobj, void (*destruct)(void*)));
6262
extern void *
6363
PyCObject_AsVoidPtr Py_PROTO((PyObject *));
6464

65+
/* Import a pointer to a C object from a module using a PyCObject. */
66+
extern void *
67+
PyCObject_Import Py_PROTO((char *module_name, char *cobject_name));
68+
6569
#ifdef __cplusplus
6670
}
6771
#endif

Objects/cobject.c

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,44 @@ PyCObject_FromVoidPtr(cobj, destr)
5757
return (PyObject *)self;
5858
}
5959

60+
void *
61+
PyCObject_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+
77+
void *
78+
PyCObject_Import(module_name, name)
79+
char *module_name;
80+
char *name;
81+
{
82+
PyObject *m, *c;
83+
void *r=NULL;
84+
85+
if(m=PyImport_ImportModule(module_name))
86+
{
87+
if(c=PyObject_GetAttrString(m,name))
88+
{
89+
r=PyCObject_AsVoidPtr(c);
90+
Py_DECREF(c);
91+
}
92+
Py_DECREF(m);
93+
}
94+
95+
return r;
96+
}
97+
6098
static void
6199
PyCObject_dealloc(self)
62100
PyCObject *self;
@@ -65,6 +103,7 @@ PyCObject_dealloc(self)
65103
PyMem_DEL(self);
66104
}
67105

106+
68107
static char PyCObject_Type__doc__[] =
69108
"C objects to be exported from one extension module to another\n\
70109
\n\
@@ -98,21 +137,3 @@ PyTypeObject PyCObject_Type = {
98137
0L,0L,0L,0L,
99138
PyCObject_Type__doc__ /* Documentation string */
100139
};
101-
102-
void *
103-
PyCObject_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-
}

PC/python_nt.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,5 @@ EXPORTS
351351
_Py_c_diff
352352
PyCObject_FromVoidPtr
353353
PyCObject_AsVoidPtr
354+
PyCObject_Import
354355
Py_GetBuildInfo

0 commit comments

Comments
 (0)