/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
1
/* Copyright (C) 2009 Canonical Ltd
2
 * 
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 */
17
18
#ifndef _IMPORT_C_API_H_
19
#define _IMPORT_C_API_H_
20
21
/**
22
 * Helper functions to eliminate some of the boilerplate when importing a C API
23
 * from a CPython extension module.
24
 *
25
 * For more information see _export_c_api.h
26
 */
27
28
static const char *_C_API_NAME = "_C_API";
29
30
/**
31
 * Import a function from the _C_API_NAME dict that is part of module.
32
 *
33
 * @param   module  The Python module we are importing from
34
 *                  the attribute _C_API_NAME will be used as a dictionary
35
 *                  containing the function pointer we are looking for.
36
 * @param   funcname    Name of the function we want to import
37
 * @param   func    A pointer to the function handle where we will store the
38
 *                  function.
39
 * @param   signature   The C signature of the function. This is validated
40
 *                      against the signature stored in the C api, to make sure
41
 *                      there is no versioning skew.
42
 */
43
static int _import_function(PyObject *module, const char *funcname,
44
                            void **func, const char *signature)
45
{
46
    PyObject *d = NULL;
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
47
    PyObject *capsule = NULL;
48
    void *pointer;
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
49
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
50
    d = PyObject_GetAttrString(module, _C_API_NAME);
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
51
    if (!d) {
52
        // PyObject_GetAttrString sets an appropriate exception
53
        goto bad;
54
    }
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
55
    capsule = PyDict_GetItemString(d, funcname);
56
    if (!capsule) {
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
57
        // PyDict_GetItemString does not set an exception
58
        PyErr_Format(PyExc_AttributeError,
59
            "Module %s did not export a function named %s\n",
60
            PyModule_GetName(module), funcname);
61
        goto bad;
62
    }
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
63
    pointer = PyCapsule_GetPointer(capsule, signature);
64
    if (!pointer) {
65
	// PyCapsule_GetPointer sets an error with a little context
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
66
        goto bad;
67
    }
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
68
    *func = pointer;
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
69
    Py_DECREF(d);
70
    return 0;
71
bad:
72
    Py_XDECREF(d);
73
    return -1;
74
}
75
76
77
/**
78
 * Get a pointer to an exported PyTypeObject.
79
 *
80
 * @param   module        The Python module we are importing from
81
 * @param   class_name    Attribute of the module that should reference the
82
 *                        Type object. Note that a PyTypeObject is the python
83
 *                        description of the type, not the raw C structure.
84
 * @return  A Pointer to the requested type object. On error NULL will be
85
 *          returned and an exception will be set.
86
 */
87
static PyTypeObject *
88
_import_type(PyObject *module, const char *class_name)
89
{
90
    PyObject *type = NULL;
91
4736.1.1 by John Arbash Meinel
Py_ssize_t and its associated function typedefs are not available w/ python 2.4
92
    type = PyObject_GetAttrString(module, (char *)class_name);
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
93
    if (!type) {
94
        goto bad;
95
    }
96
    if (!PyType_Check(type)) {
97
        PyErr_Format(PyExc_TypeError,
98
            "%s.%s is not a type object",
99
            PyModule_GetName(module), class_name);
100
        goto bad;
101
    }
102
    return (PyTypeObject *)type;
103
bad:
104
    Py_XDECREF(type);
105
    return NULL;
106
}
107
108
109
struct function_description
110
{
111
    const char *name;
112
    void **pointer;
113
    const char *signature;
114
};
115
116
struct type_description
117
{
118
    const char *name;
119
    PyTypeObject **pointer;
120
};
121
122
/**
123
 * Helper for importing several functions and types in a data-driven manner.
124
 *
125
 * @param   module  The name of the module we will be importing
126
 * @param   functions   A list of function_description objects, describing the
127
 *                      functions being imported.
128
 *                      The list should be terminated with {NULL} to indicate
129
 *                      there are no more functions to import.
130
 * @param   types       A list of type_description objects describing type
131
 *                      objects that we want to import. The list should be
132
 *                      terminated with {NULL} to indicate there are no more
133
 *                      types to import.
134
 * @return  0 on success, -1 on error and an exception should be set.
135
 */
136
137
static int
138
_import_extension_module(const char *module_name,
139
                         struct function_description *functions,
140
                         struct type_description *types)
141
{
142
    PyObject *module = NULL;
143
    struct function_description *cur_func;
144
    struct type_description *cur_type;
145
    int ret_code;
146
    
4736.1.1 by John Arbash Meinel
Py_ssize_t and its associated function typedefs are not available w/ python 2.4
147
    module = PyImport_ImportModule((char *)module_name);
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
148
    if (!module)
149
        goto bad;
150
    if (functions != NULL) {
151
        cur_func = functions;
152
        while (cur_func->name != NULL) {
153
            ret_code = _import_function(module, cur_func->name,
154
                                        cur_func->pointer,
155
                                        cur_func->signature);
156
            if (ret_code < 0)
157
                goto bad;
158
            cur_func++;
159
        }
160
    }
161
    if (types != NULL) {
162
        PyTypeObject *type_p = NULL;
163
        cur_type = types;
164
        while (cur_type->name != NULL)  {
165
            type_p = _import_type(module, cur_type->name);
166
            if (type_p == NULL)
167
                goto bad;
168
            *(cur_type->pointer) = type_p;
169
            cur_type++;
170
        }
171
    }
172
    
173
    Py_XDECREF(module);
174
    return 0;
175
bad:
176
    Py_XDECREF(module);
177
    return -1;
178
}
179
180
181
#endif // _IMPORT_C_API_H_