Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
capsule.c File Reference
#include "Python.h"

Go to the source code of this file.

Classes

struct  PyCapsule

Defines

#define is_legal_capsule(capsule, name)

Functions

static int _is_legal_capsule (PyCapsule *capsule, const char *invalid_capsule)
static int name_matches (const char *name1, const char *name2)
PyObjectPyCapsule_New (void *pointer, const char *name, PyCapsule_Destructor destructor)
int PyCapsule_IsValid (PyObject *o, const char *name)
voidPyCapsule_GetPointer (PyObject *o, const char *name)
const char * PyCapsule_GetName (PyObject *o)
PyCapsule_Destructor PyCapsule_GetDestructor (PyObject *o)
voidPyCapsule_GetContext (PyObject *o)
int PyCapsule_SetPointer (PyObject *o, void *pointer)
int PyCapsule_SetName (PyObject *o, const char *name)
int PyCapsule_SetDestructor (PyObject *o, PyCapsule_Destructor destructor)
int PyCapsule_SetContext (PyObject *o, void *context)
voidPyCapsule_Import (const char *name, int no_block)
static void capsule_dealloc (PyObject *o)
static PyObjectcapsule_repr (PyObject *o)
 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\ ")

Variables

PyTypeObject PyCapsule_Type

Class Documentation

struct PyCapsule

Definition at line 6 of file capsule.c.

Collaboration diagram for PyCapsule:
Class Members
void * context
PyCapsule_Destructor destructor
const char * name
PyObject_HEAD void * pointer

Define Documentation

#define is_legal_capsule (   capsule,
  name 
)
Value:
(_is_legal_capsule(capsule, \
     name " called with invalid PyCapsule object"))

Definition at line 26 of file capsule.c.


Function Documentation

static int _is_legal_capsule ( PyCapsule capsule,
const char *  invalid_capsule 
) [static]

Definition at line 17 of file capsule.c.

{
    if (!capsule || !PyCapsule_CheckExact(capsule) || capsule->pointer == NULL) {
        PyErr_SetString(PyExc_ValueError, invalid_capsule);
        return 0;
    }
    return 1;
}

Here is the call graph for this function:

static void capsule_dealloc ( PyObject o) [static]

Definition at line 257 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;
    if (capsule->destructor) {
        capsule->destructor(o);
    }
    PyObject_DEL(o);
}
static PyObject* capsule_repr ( PyObject o) [static]

Definition at line 268 of file capsule.c.

{
    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);
}

Here is the call graph for this function:

static int name_matches ( const char *  name1,
const char *  name2 
) [static]

Definition at line 32 of file capsule.c.

                                                   {
    /* if either is NULL, */
    if (!name1 || !name2) {
        /* they're only the same if they're both NULL. */
        return name1 == name2;
    }
    return !strcmp(name1, name2);
}

Here is the caller graph for this function:

Definition at line 122 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_GetContext")) {
        return NULL;
    }
    return capsule->context;
}

Here is the caller graph for this function:

Definition at line 110 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_GetDestructor")) {
        return NULL;
    }
    return capsule->destructor;
}

Here is the caller graph for this function:

Definition at line 98 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_GetName")) {
        return NULL;
    }
    return capsule->name;
}

Here is the caller graph for this function:

void* PyCapsule_GetPointer ( PyObject o,
const char *  name 
)

Definition at line 80 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_GetPointer")) {
        return NULL;
    }

    if (!name_matches(name, capsule->name)) {
        PyErr_SetString(PyExc_ValueError, "PyCapsule_GetPointer called with incorrect name");
        return NULL;
    }

    return capsule->pointer;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* PyCapsule_Import ( const char *  name,
int  no_block 
)

Definition at line 195 of file capsule.c.

{
    PyObject *object = NULL;
    void *return_value = NULL;
    char *trace;
    size_t name_length = (strlen(name) + 1) * sizeof(char);
    char *name_dup = (char *)PyMem_MALLOC(name_length);

    if (!name_dup) {
        return NULL;
    }

    memcpy(name_dup, name, name_length);

    trace = name_dup;
    while (trace) {
        char *dot = strchr(trace, '.');
        if (dot) {
            *dot++ = '\0';
        }

        if (object == NULL) {
            if (no_block) {
                object = PyImport_ImportModuleNoBlock(trace);
            } else {
                object = PyImport_ImportModule(trace);
                if (!object) {
                    PyErr_Format(PyExc_ImportError, "PyCapsule_Import could not import module \"%s\"", trace);
                }
            }
        } else {
            PyObject *object2 = PyObject_GetAttrString(object, trace);
            Py_DECREF(object);
            object = object2;
        }
        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) {
        PyMem_FREE(name_dup);
    }
    return return_value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PyCapsule_IsValid ( PyObject o,
const char *  name 
)

Definition at line 68 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    return (capsule != NULL &&
            PyCapsule_CheckExact(capsule) &&
            capsule->pointer != NULL &&
            name_matches(capsule->name, name));
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyObject* PyCapsule_New ( void pointer,
const char *  name,
PyCapsule_Destructor  destructor 
)

Definition at line 44 of file capsule.c.

{
    PyCapsule *capsule;

    if (!pointer) {
        PyErr_SetString(PyExc_ValueError, "PyCapsule_New called with null pointer");
        return NULL;
    }

    capsule = PyObject_NEW(PyCapsule, &PyCapsule_Type);
    if (capsule == NULL) {
        return NULL;
    }

    capsule->pointer = pointer;
    capsule->name = name;
    capsule->context = NULL;
    capsule->destructor = destructor;

    return (PyObject *)capsule;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PyCapsule_SetContext ( PyObject o,
void context 
)

Definition at line 181 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_SetContext")) {
        return -1;
    }

    capsule->context = context;
    return 0;
}

Here is the caller graph for this function:

Definition at line 167 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_SetDestructor")) {
        return -1;
    }

    capsule->destructor = destructor;
    return 0;
}

Here is the caller graph for this function:

int PyCapsule_SetName ( PyObject o,
const char *  name 
)

Definition at line 153 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!is_legal_capsule(capsule, "PyCapsule_SetName")) {
        return -1;
    }

    capsule->name = name;
    return 0;
}

Here is the caller graph for this function:

int PyCapsule_SetPointer ( PyObject o,
void pointer 
)

Definition at line 134 of file capsule.c.

{
    PyCapsule *capsule = (PyCapsule *)o;

    if (!pointer) {
        PyErr_SetString(PyExc_ValueError, "PyCapsule_SetPointer called with null pointer");
        return -1;
    }

    if (!is_legal_capsule(capsule, "PyCapsule_SetPointer")) {
        return -1;
    }

    capsule->pointer = pointer;
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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\"   
)

Variable Documentation

Initial value:
 {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "PyCapsule",                
    sizeof(PyCapsule),          
    0,                          
    
    capsule_dealloc, 
    0,                          
    0,                          
    0,                          
    0,                          
    capsule_repr, 
    0,                          
    0,                          
    0,                          
    0,                          
    0,                          
    0,                          
    0,                          
    0,                          
    0,                          
    0,                          
    PyCapsule_Type__doc__       
}

Definition at line 299 of file capsule.c.