/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;
47
    PyObject *c_obj = NULL;
48
    const char *desc = NULL;
49
50
    d = PyObject_GetAttrString(module, _C_API_NAME);
51
    if (!d) {
52
        // PyObject_GetAttrString sets an appropriate exception
53
        goto bad;
54
    }
55
    c_obj = PyDict_GetItemString(d, funcname);
56
    if (!c_obj) {
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
    }
63
    desc = (char *)PyCObject_GetDesc(c_obj);
64
    if (!desc || strcmp(desc, signature) != 0) {
65
        if (desc == NULL) {
66
            desc = "<null>";
67
        }
68
        PyErr_Format(PyExc_TypeError,
69
            "C function %s.%s has wrong signature (expected %s, got %s)",
70
                PyModule_GetName(module), funcname, signature, desc);
71
        goto bad;
72
    }
73
    *func = PyCObject_AsVoidPtr(c_obj);
74
    Py_DECREF(d);
75
    return 0;
76
bad:
77
    Py_XDECREF(d);
78
    return -1;
79
}
80
81
82
/**
83
 * Get a pointer to an exported PyTypeObject.
84
 *
85
 * @param   module        The Python module we are importing from
86
 * @param   class_name    Attribute of the module that should reference the
87
 *                        Type object. Note that a PyTypeObject is the python
88
 *                        description of the type, not the raw C structure.
89
 * @return  A Pointer to the requested type object. On error NULL will be
90
 *          returned and an exception will be set.
91
 */
92
static PyTypeObject *
93
_import_type(PyObject *module, const char *class_name)
94
{
95
    PyObject *type = NULL;
96
97
    type = PyObject_GetAttrString(module, class_name);
98
    if (!type) {
99
        goto bad;
100
    }
101
    if (!PyType_Check(type)) {
102
        PyErr_Format(PyExc_TypeError,
103
            "%s.%s is not a type object",
104
            PyModule_GetName(module), class_name);
105
        goto bad;
106
    }
107
    return (PyTypeObject *)type;
108
bad:
109
    Py_XDECREF(type);
110
    return NULL;
111
}
112
113
114
struct function_description
115
{
116
    const char *name;
117
    void **pointer;
118
    const char *signature;
119
};
120
121
struct type_description
122
{
123
    const char *name;
124
    PyTypeObject **pointer;
125
};
126
127
/**
128
 * Helper for importing several functions and types in a data-driven manner.
129
 *
130
 * @param   module  The name of the module we will be importing
131
 * @param   functions   A list of function_description objects, describing the
132
 *                      functions being imported.
133
 *                      The list should be terminated with {NULL} to indicate
134
 *                      there are no more functions to import.
135
 * @param   types       A list of type_description objects describing type
136
 *                      objects that we want to import. The list should be
137
 *                      terminated with {NULL} to indicate there are no more
138
 *                      types to import.
139
 * @return  0 on success, -1 on error and an exception should be set.
140
 */
141
142
static int
143
_import_extension_module(const char *module_name,
144
                         struct function_description *functions,
145
                         struct type_description *types)
146
{
147
    PyObject *module = NULL;
148
    struct function_description *cur_func;
149
    struct type_description *cur_type;
150
    int ret_code;
151
    
152
    module = PyImport_ImportModule(module_name);
153
    if (!module)
154
        goto bad;
155
    if (functions != NULL) {
156
        cur_func = functions;
157
        while (cur_func->name != NULL) {
158
            ret_code = _import_function(module, cur_func->name,
159
                                        cur_func->pointer,
160
                                        cur_func->signature);
161
            if (ret_code < 0)
162
                goto bad;
163
            cur_func++;
164
        }
165
    }
166
    if (types != NULL) {
167
        PyTypeObject *type_p = NULL;
168
        cur_type = types;
169
        while (cur_type->name != NULL)  {
170
            type_p = _import_type(module, cur_type->name);
171
            if (type_p == NULL)
172
                goto bad;
173
            *(cur_type->pointer) = type_p;
174
            cur_type++;
175
        }
176
    }
177
    
178
    Py_XDECREF(module);
179
    return 0;
180
bad:
181
    Py_XDECREF(module);
182
    return -1;
183
}
184
185
186
#endif // _IMPORT_C_API_H_