Commit f83b54ac authored by André Anjos's avatar André Anjos 💬
Browse files

LPInteriorPoint base class is almost ready for prime-time

parent a4efed56
......@@ -47,6 +47,7 @@ setup(
"xbob/math/pavx.cpp",
"xbob/math/norminv.cpp",
"xbob/math/scatter.cpp",
"xbob/math/lp_interior_point.cpp",
"xbob/math/main.cpp",
],
packages = packages,
......
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Tue 3 Dec 14:27:26 2013 CET
*
* @brief C/C++ API for bob::math
*/
#ifndef XBOB_MATH_H
#define XBOB_MATH_H
/* Define Module Name and Prefix for other Modules
Note: We cannot use XBOB_EXT_* macros here, unfortunately */
#define XBOB_MATH_PREFIX "xbob.math"
#define XBOB_MATH_FULL_NAME "xbob.math._library"
#include <xbob.math/config.h>
#include <Python.h>
/*******************
* C API functions *
*******************/
/**************
* Versioning *
**************/
#define PyXbobMath_APIVersion_NUM 0
#define PyXbobMath_APIVersion_TYPE int
/* Total number of C API pointers */
#define PyXbobMath_API_pointers 1
#ifdef XBOB_MATH_MODULE
/* This section is used when compiling `xbob.math' itself */
/**************
* Versioning *
**************/
extern int PyXbobMath_APIVersion;
#else
/* This section is used in modules that use `xbob.math's' C-API */
/************************************************************************
* Macros to avoid symbol collision and allow for separate compilation. *
* We pig-back on symbols already defined for NumPy and apply the same *
* set of rules here, creating our own API symbol names. *
************************************************************************/
# if defined(PY_ARRAY_UNIQUE_SYMBOL)
# define XBOB_MATH_MAKE_API_NAME_INNER(a) XBOB_MATH_ ## a
# define XBOB_MATH_MAKE_API_NAME(a) XBOB_MATH_MAKE_API_NAME_INNER(a)
# define PyXbobIo_API XBOB_MATH_MAKE_API_NAME(PY_ARRAY_UNIQUE_SYMBOL)
# endif
# if defined(NO_IMPORT_ARRAY)
extern void **PyXbobMath_API;
# else
# if defined(PY_ARRAY_UNIQUE_SYMBOL)
void **PyXbobMath_API;
# else
static void **PyXbobMath_API=NULL;
# endif
# endif
static void **PyXbobMath_API;
/**************
* Versioning *
**************/
# define PyXbobMath_APIVersion (*(PyXbobMath_APIVersion_TYPE *)PyXbobMath_API[PyXbobMath_APIVersion_NUM])
# if !defined(NO_IMPORT_ARRAY)
/**
* Returns -1 on error, 0 on success.
*/
static int import_xbob_math(void) {
PyObject *c_api_object;
PyObject *module;
module = PyImport_ImportModule(XBOB_MATH_FULL_NAME);
if (module == NULL) return -1;
c_api_object = PyObject_GetAttrString(module, "_C_API");
if (c_api_object == NULL) {
Py_DECREF(module);
return -1;
}
# if PY_VERSION_HEX >= 0x02070000
if (PyCapsule_CheckExact(c_api_object)) {
PyXbobMath_API = (void **)PyCapsule_GetPointer(c_api_object,
PyCapsule_GetName(c_api_object));
}
# else
if (PyCObject_Check(c_api_object)) {
PyXbobMath_API = (void **)PyCObject_AsVoidPtr(c_api_object);
}
# endif
Py_DECREF(c_api_object);
Py_DECREF(module);
if (!PyXbobMath_API) {
PyErr_SetString(PyExc_ImportError, "cannot find C/C++ API "
# if PY_VERSION_HEX >= 0x02070000
"capsule"
# else
"cobject"
# endif
" at `" XBOB_MATH_FULL_NAME "._C_API'");
return -1;
}
/* Checks that the imported version matches the compiled version */
int imported_version = *(int*)PyXbobMath_API[PyXbobMath_APIVersion_NUM];
if (XBOB_MATH_API_VERSION != imported_version) {
PyErr_Format(PyExc_ImportError, XBOB_MATH_FULL_NAME " import error: you compiled against API version 0x%04x, but are now importing an API with version 0x%04x which is not compatible - check your Python runtime environment for errors", XBOB_MATH_API_VERSION, imported_version);
return -1;
}
/* If you get to this point, all is good */
return 0;
}
# endif //!defined(NO_IMPORT_ARRAY)
#endif /* XBOB_MATH_MODULE */
#endif /* XBOB_MATH_H */
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Tue 3 Dec 14:26:04 2013 CET
*
* @brief General directives for all modules in xbob.math
*/
#ifndef XBOB_MATH_CONFIG_H
#define XBOB_MATH_CONFIG_H
/* Macros that define versions and important names */
#define XBOB_MATH_API_VERSION 0x0200
#endif /* XBOB_MATH_CONFIG_H */
This diff is collapsed.
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Mon 9 Dec 13:58:20 2013
*
* @brief Declaration of LPInteriorPoint & derived classes bindings
*/
#include <Python.h>
extern PyTypeObject PyBobMathLpInteriorPoint_Type;
......@@ -5,9 +5,6 @@
* @brief Bindings to bob::math
*/
#define XBOB_MATH_MODULE
#include <xbob.math/api.h>
#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
......@@ -18,6 +15,7 @@
#include "pavx.h"
#include "norminv.h"
#include "scatter.h"
#include "lp_interior_point.h"
PyDoc_STRVAR(s_histogram_intersection_str, "histogram_intersection");
PyDoc_STRVAR(s_histogram_intersection_doc,
......@@ -503,40 +501,20 @@ static PyMethodDef module_methods[] = {
PyDoc_STRVAR(module_docstr, "bob::math classes and methods");
int PyXbobMath_APIVersion = XBOB_MATH_API_VERSION;
PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) {
PyBobMathLpInteriorPoint_Type.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyBobMathLpInteriorPoint_Type) < 0) return;
PyObject* m = Py_InitModule3(XBOB_EXT_MODULE_NAME,
module_methods, module_docstr);
/* register some constants */
PyModule_AddIntConstant(m, "__api_version__", XBOB_MATH_API_VERSION);
PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION);
/* exhaustive list of C APIs */
static void* PyXbobMath_API[PyXbobMath_API_pointers];
/**************
* Versioning *
**************/
PyXbobMath_API[PyXbobMath_APIVersion_NUM] = (void *)&PyXbobMath_APIVersion;
#if PY_VERSION_HEX >= 0x02070000
/* defines the PyCapsule */
PyObject* c_api_object = PyCapsule_New((void *)PyXbobMath_API,
XBOB_EXT_MODULE_PREFIX "." XBOB_EXT_MODULE_NAME "._C_API", 0);
#else
PyObject* c_api_object = PyCObject_FromVoidPtr((void *)PyXbobMath_API, 0);
#endif
if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object);
/* register the types to python */
Py_INCREF(&PyBobMathLpInteriorPoint_Type);
PyModule_AddObject(m, "LPInteriorPoint", (PyObject *)&PyBobMathLpInteriorPoint_Type);
/* imports the NumPy C-API */
import_array();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment