/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/_export_c_api.h

  • Committer: John Arbash Meinel
  • Date: 2009-10-08 04:35:01 UTC
  • mto: (4679.6.1 2.1-export-c-api)
  • mto: This revision was merged to the branch mainline in revision 4735.
  • Revision ID: john@arbash-meinel.com-20091008043501-8fgotm1vbt1zp4g6
Remove everything except for SimpleSet, and clean up its test suite.

This will be my first submission, since it doesn't directly depend on anything else.

Show diffs side-by-side

added added

removed removed

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