1
/* Copyright (C) 2009 Canonical Ltd
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.
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.
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
19
/* This file contains helper functions for exporting a C API for a CPython
23
#ifndef _EXPORT_C_API_H_
24
#define _EXPORT_C_API_H_
26
static const char *_C_API_NAME = "_C_API";
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.
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
43
_export_function(PyObject *module, char *funcname, void *func, char *signature)
46
PyObject *c_obj = NULL;
48
d = PyObject_GetAttrString(module, _C_API_NAME);
55
if (PyModule_AddObject(module, _C_API_NAME, d) < 0)
58
c_obj = PyCObject_FromVoidPtrAndDesc(func, signature, 0);
61
if (PyDict_SetItemString(d, funcname, c_obj) < 0)
71
#endif // _EXPORT_C_API_H_