mirror of
https://github.com/python/cpython.git
synced 2025-11-13 15:40:05 +00:00
Added PyCObject_Import.
This commit is contained in:
parent
43d287ad73
commit
e0e696282f
3 changed files with 44 additions and 18 deletions
|
|
@ -62,6 +62,10 @@ PyCObject_FromVoidPtr Py_PROTO((void *cobj, void (*destruct)(void*)));
|
||||||
extern void *
|
extern void *
|
||||||
PyCObject_AsVoidPtr Py_PROTO((PyObject *));
|
PyCObject_AsVoidPtr Py_PROTO((PyObject *));
|
||||||
|
|
||||||
|
/* Import a pointer to a C object from a module using a PyCObject. */
|
||||||
|
extern void *
|
||||||
|
PyCObject_Import Py_PROTO((char *module_name, char *cobject_name));
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,44 @@ PyCObject_FromVoidPtr(cobj, destr)
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
PyCObject_AsVoidPtr(self)
|
||||||
|
PyObject *self;
|
||||||
|
{
|
||||||
|
if(self)
|
||||||
|
{
|
||||||
|
if(self->ob_type == &PyCObject_Type)
|
||||||
|
return ((PyCObject *)self)->cobject;
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"PyCObject_AsVoidPtr with non-C-object");
|
||||||
|
}
|
||||||
|
if(! PyErr_Occurred())
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"PyCObject_AsVoidPtr called with null pointer");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
PyCObject_Import(module_name, name)
|
||||||
|
char *module_name;
|
||||||
|
char *name;
|
||||||
|
{
|
||||||
|
PyObject *m, *c;
|
||||||
|
void *r=NULL;
|
||||||
|
|
||||||
|
if(m=PyImport_ImportModule(module_name))
|
||||||
|
{
|
||||||
|
if(c=PyObject_GetAttrString(m,name))
|
||||||
|
{
|
||||||
|
r=PyCObject_AsVoidPtr(c);
|
||||||
|
Py_DECREF(c);
|
||||||
|
}
|
||||||
|
Py_DECREF(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
PyCObject_dealloc(self)
|
PyCObject_dealloc(self)
|
||||||
PyCObject *self;
|
PyCObject *self;
|
||||||
|
|
@ -65,6 +103,7 @@ PyCObject_dealloc(self)
|
||||||
PyMem_DEL(self);
|
PyMem_DEL(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static char PyCObject_Type__doc__[] =
|
static char PyCObject_Type__doc__[] =
|
||||||
"C objects to be exported from one extension module to another\n\
|
"C objects to be exported from one extension module to another\n\
|
||||||
\n\
|
\n\
|
||||||
|
|
@ -98,21 +137,3 @@ PyTypeObject PyCObject_Type = {
|
||||||
0L,0L,0L,0L,
|
0L,0L,0L,0L,
|
||||||
PyCObject_Type__doc__ /* Documentation string */
|
PyCObject_Type__doc__ /* Documentation string */
|
||||||
};
|
};
|
||||||
|
|
||||||
void *
|
|
||||||
PyCObject_AsVoidPtr(self)
|
|
||||||
PyObject *self;
|
|
||||||
{
|
|
||||||
if(self)
|
|
||||||
{
|
|
||||||
if(self->ob_type == &PyCObject_Type)
|
|
||||||
return ((PyCObject *)self)->cobject;
|
|
||||||
PyErr_SetString(PyExc_TypeError,
|
|
||||||
"PyCObject_AsVoidPtr with non-C-object");
|
|
||||||
}
|
|
||||||
if(! PyErr_Occurred())
|
|
||||||
PyErr_SetString(
|
|
||||||
PyExc_TypeError,
|
|
||||||
"PyCObject_AsVoidPtr called with null pointer");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -351,4 +351,5 @@ EXPORTS
|
||||||
_Py_c_diff
|
_Py_c_diff
|
||||||
PyCObject_FromVoidPtr
|
PyCObject_FromVoidPtr
|
||||||
PyCObject_AsVoidPtr
|
PyCObject_AsVoidPtr
|
||||||
|
PyCObject_Import
|
||||||
Py_GetBuildInfo
|
Py_GetBuildInfo
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue