/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
/* This file contains helper functions for exporting a C API for a CPython
19
 * extension module.
20
 */
21
22
#ifndef _EXPORT_C_API_H_
23
#define _EXPORT_C_API_H_
24
25
static const char *_C_API_NAME = "_C_API";
26
27
/**
28
 * Add a C function to the modules _C_API
29
 * This wraps the function in a PyCObject, and inserts that into a dict.
30
 * The key of the dict is the function name, and the description is the
31
 * signature of the function.
32
 * This is generally called during a modules init_MODULE function.
33
 *
34
 * @param   module  A Python module (the one being initialized)
35
 * @param   funcname The name of the function being exported
36
 * @param   func    A pointer to the function
37
 * @param   signature The C signature of the function
38
 * @return  0 if everything is successful, -1 if there is a problem. An
39
 *          exception should also be set
40
 */
41
static int
42
_export_function(PyObject *module, char *funcname, void *func, char *signature)
43
{
44
    PyObject *d = NULL;
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
45
    PyObject *capsule = NULL;
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
46
6715.1.3 by Martin
Note changes needed for c api helpers
47
    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.
48
    if (!d) {
49
        PyErr_Clear();
50
        d = PyDict_New();
51
        if (!d)
52
            goto bad;
53
        Py_INCREF(d);
6715.1.3 by Martin
Note changes needed for c api helpers
54
        if (PyModule_AddObject(module, _C_API_NAME, d) < 0)
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
55
            goto bad;
56
    }
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
57
    capsule = PyCapsule_New(func, signature, 0);
58
    if (!capsule)
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
59
        goto bad;
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
60
    if (PyDict_SetItemString(d, funcname, capsule) < 0)
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
61
        goto bad;
62
    Py_DECREF(d);
63
    return 0;
64
bad:
6715.1.5 by Martin
Switch c_api helpers for _static_tuple_c to capsules
65
    Py_XDECREF(capsule);
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
66
    Py_XDECREF(d);
67
    return -1;
68
}
69
4679.3.55 by John Arbash Meinel
A quick note about how I'd *like* to do things.
70
/* Note:
71
 *  It feels like more could be done here. Specifically, if you look at
72
 *  _static_tuple_c.h you can see some boilerplate where we have:
73
 * #ifdef STATIC_TUPLE_MODULE  // are we exporting or importing
74
 * static RETVAL FUNCNAME PROTO;
75
 * #else
76
 * static RETVAL (*FUNCNAME) PROTO;
77
 * #endif
78
 * 
79
 * And then in _static_tuple_c.c we have
80
 * int setup_c_api()
81
 * {
82
 *   _export_function(module, #FUNCNAME, FUNCNAME, #PROTO);
83
 * }
84
 *
85
 * And then in _static_tuple_c.h import_##MODULE
86
 * struct function_definition functions[] = {
87
 *   {#FUNCNAME, (void **)&FUNCNAME, #RETVAL #PROTO},
88
 *   ...
89
 *   {NULL}};
90
 *
91
 * And some similar stuff for types. However, this would mean that we would
92
 * need a way for the C preprocessor to build up a list of definitions to be
93
 * generated, and then expand that list at the appropriate time.
94
 * I would guess there would be a way to do this, but probably not without a
95
 * lot of magic, and the end result probably wouldn't be very pretty to
96
 * maintain. Perhaps python's dynamic nature has left me jaded about writing
97
 * boilerplate....
98
 */
99
4679.3.54 by John Arbash Meinel
Factor out some of the C API code into some helper headers.
100
#endif // _EXPORT_C_API_H_