2009-05-06 06:31:58 +08:00
|
|
|
/* Wrap void * pointers to be passed between C modules */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
/* Internal structure of PyCapsule */
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
void *pointer;
|
|
|
|
const char *name;
|
|
|
|
void *context;
|
|
|
|
PyCapsule_Destructor destructor;
|
2023-08-24 06:19:11 +08:00
|
|
|
traverseproc traverse_func;
|
|
|
|
inquiry clear_func;
|
2009-05-06 06:31:58 +08:00
|
|
|
} PyCapsule;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2023-08-24 06:19:11 +08:00
|
|
|
_is_legal_capsule(PyObject *op, const char *invalid_capsule)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!op || !PyCapsule_CheckExact(op)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
|
|
|
|
|
|
|
if (capsule->pointer == NULL) {
|
|
|
|
goto error;
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
return 1;
|
2023-08-24 06:19:11 +08:00
|
|
|
|
|
|
|
error:
|
|
|
|
PyErr_SetString(PyExc_ValueError, invalid_capsule);
|
|
|
|
return 0;
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define is_legal_capsule(capsule, name) \
|
|
|
|
(_is_legal_capsule(capsule, \
|
|
|
|
name " called with invalid PyCapsule object"))
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
name_matches(const char *name1, const char *name2) {
|
|
|
|
/* if either is NULL, */
|
|
|
|
if (!name1 || !name2) {
|
|
|
|
/* they're only the same if they're both NULL. */
|
2009-06-29 00:23:55 +08:00
|
|
|
return name1 == name2;
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
return !strcmp(name1, name2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
|
|
|
|
{
|
|
|
|
PyCapsule *capsule;
|
|
|
|
|
|
|
|
if (!pointer) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "PyCapsule_New called with null pointer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-08-24 06:19:11 +08:00
|
|
|
capsule = PyObject_GC_New(PyCapsule, &PyCapsule_Type);
|
2009-05-06 06:31:58 +08:00
|
|
|
if (capsule == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
capsule->pointer = pointer;
|
|
|
|
capsule->name = name;
|
|
|
|
capsule->context = NULL;
|
|
|
|
capsule->destructor = destructor;
|
2023-08-24 06:19:11 +08:00
|
|
|
capsule->traverse_func = NULL;
|
|
|
|
capsule->clear_func = NULL;
|
|
|
|
// Only track the capsule if _PyCapsule_SetTraverse() is called
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
return (PyObject *)capsule;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_IsValid(PyObject *op, const char *name)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
return (capsule != NULL &&
|
|
|
|
PyCapsule_CheckExact(capsule) &&
|
|
|
|
capsule->pointer != NULL &&
|
|
|
|
name_matches(capsule->name, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_GetPointer(PyObject *op, const char *name)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_GetPointer")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
if (!name_matches(name, capsule->name)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "PyCapsule_GetPointer called with incorrect name");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return capsule->pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_GetName(PyObject *op)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_GetName")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
return capsule->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyCapsule_Destructor
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_GetDestructor(PyObject *op)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_GetDestructor")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
return capsule->destructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_GetContext(PyObject *op)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_GetContext")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
return capsule->context;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_SetPointer(PyObject *op, void *pointer)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_SetPointer")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!pointer) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "PyCapsule_SetPointer called with null pointer");
|
2009-05-06 06:31:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
capsule->pointer = pointer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_SetName(PyObject *op, const char *name)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_SetName")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
capsule->name = name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_SetDestructor(PyObject *op, PyCapsule_Destructor destructor)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_SetDestructor")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
capsule->destructor = destructor;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule_SetContext(PyObject *op, void *context)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
if (!is_legal_capsule(op, "PyCapsule_SetContext")) {
|
2009-05-06 06:31:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
capsule->context = context;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 06:19:11 +08:00
|
|
|
int
|
|
|
|
_PyCapsule_SetTraverse(PyObject *op, traverseproc traverse_func, inquiry clear_func)
|
|
|
|
{
|
|
|
|
if (!is_legal_capsule(op, "_PyCapsule_SetTraverse")) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
|
|
|
|
|
|
|
if (!PyObject_GC_IsTracked(op)) {
|
|
|
|
PyObject_GC_Track(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
capsule->traverse_func = traverse_func;
|
|
|
|
capsule->clear_func = clear_func;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-06 06:31:58 +08:00
|
|
|
void *
|
|
|
|
PyCapsule_Import(const char *name, int no_block)
|
|
|
|
{
|
|
|
|
PyObject *object = NULL;
|
|
|
|
void *return_value = NULL;
|
|
|
|
char *trace;
|
2009-10-04 04:13:37 +08:00
|
|
|
size_t name_length = (strlen(name) + 1) * sizeof(char);
|
2020-12-01 16:56:42 +08:00
|
|
|
char *name_dup = (char *)PyMem_Malloc(name_length);
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
if (!name_dup) {
|
2018-12-07 18:11:30 +08:00
|
|
|
return PyErr_NoMemory();
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(name_dup, name, name_length);
|
|
|
|
|
|
|
|
trace = name_dup;
|
|
|
|
while (trace) {
|
|
|
|
char *dot = strchr(trace, '.');
|
|
|
|
if (dot) {
|
|
|
|
*dot++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object == NULL) {
|
2021-12-12 17:49:50 +08:00
|
|
|
object = PyImport_ImportModule(trace);
|
|
|
|
if (!object) {
|
|
|
|
PyErr_Format(PyExc_ImportError, "PyCapsule_Import could not import module \"%s\"", trace);
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PyObject *object2 = PyObject_GetAttrString(object, trace);
|
2022-11-22 20:39:11 +08:00
|
|
|
Py_SETREF(object, object2);
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
if (!object) {
|
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace = dot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compare attribute name to module.name by hand */
|
|
|
|
if (PyCapsule_IsValid(object, name)) {
|
|
|
|
PyCapsule *capsule = (PyCapsule *)object;
|
|
|
|
return_value = capsule->pointer;
|
|
|
|
} else {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"PyCapsule_Import \"%s\" is not valid",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXIT:
|
|
|
|
Py_XDECREF(object);
|
|
|
|
if (name_dup) {
|
2020-12-01 16:56:42 +08:00
|
|
|
PyMem_Free(name_dup);
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2023-08-24 06:19:11 +08:00
|
|
|
capsule_dealloc(PyObject *op)
|
2009-05-06 06:31:58 +08:00
|
|
|
{
|
2023-08-24 06:19:11 +08:00
|
|
|
PyCapsule *capsule = (PyCapsule *)op;
|
|
|
|
PyObject_GC_UnTrack(op);
|
2009-05-06 06:31:58 +08:00
|
|
|
if (capsule->destructor) {
|
2023-08-24 06:19:11 +08:00
|
|
|
capsule->destructor(op);
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
2023-08-24 06:19:11 +08:00
|
|
|
PyObject_GC_Del(op);
|
2009-05-06 06:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
capsule_repr(PyObject *o)
|
|
|
|
{
|
|
|
|
PyCapsule *capsule = (PyCapsule *)o;
|
|
|
|
const char *name;
|
|
|
|
const char *quote;
|
|
|
|
|
|
|
|
if (capsule->name) {
|
|
|
|
quote = "\"";
|
|
|
|
name = capsule->name;
|
|
|
|
} else {
|
|
|
|
quote = "";
|
|
|
|
name = "NULL";
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyUnicode_FromFormat("<capsule object %s%s%s at %p>",
|
|
|
|
quote, name, quote, capsule);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 06:19:11 +08:00
|
|
|
static int
|
|
|
|
capsule_traverse(PyCapsule *capsule, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
if (capsule->traverse_func) {
|
|
|
|
return capsule->traverse_func((PyObject*)capsule, visit, arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
capsule_clear(PyCapsule *capsule)
|
|
|
|
{
|
|
|
|
if (capsule->clear_func) {
|
|
|
|
return capsule->clear_func((PyObject*)capsule);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 06:31:58 +08:00
|
|
|
|
|
|
|
PyDoc_STRVAR(PyCapsule_Type__doc__,
|
|
|
|
"Capsule objects let you wrap a C \"void *\" pointer in a Python\n\
|
|
|
|
object. They're a way of passing data through the Python interpreter\n\
|
|
|
|
without creating your own custom type.\n\
|
|
|
|
\n\
|
|
|
|
Capsules are used for communication between extension modules.\n\
|
|
|
|
They provide a way for an extension module to export a C interface\n\
|
|
|
|
to other extension modules, so that extension modules can use the\n\
|
|
|
|
Python import mechanism to link to one another.\n\
|
|
|
|
");
|
|
|
|
|
|
|
|
PyTypeObject PyCapsule_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2023-08-24 06:19:11 +08:00
|
|
|
.tp_name = "PyCapsule",
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
|
|
|
.tp_basicsize = sizeof(PyCapsule),
|
|
|
|
.tp_dealloc = capsule_dealloc,
|
|
|
|
.tp_repr = capsule_repr,
|
|
|
|
.tp_doc = PyCapsule_Type__doc__,
|
|
|
|
.tp_traverse = (traverseproc)capsule_traverse,
|
|
|
|
.tp_clear = (inquiry)capsule_clear,
|
2009-05-06 06:31:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|