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

Cleanup, Python 3 support

parent 304bb4c3
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Wed 11 Dec 08:42:53 2013
*
* @brief Some C++ tricks to make our life dealing with Python references a bit
* easier
*/
#include <Python.h>
#include <memory>
/**
* Calls Py_DECREF(x) on the input object x. Usage pattern:
*
* PyObject* x = ... // builds x with a new python reference
* auto protected_x = make_safe(x);
*
* After this point, no need to worry about DECREF'ing x anymore.
* You can still use `x' inside your code, or protected_x.get().
*/
template <typename T> std::shared_ptr<T> make_safe(T* o) {
return std::shared_ptr<T>(o, [&](T* p){Py_DECREF(p);});
}
/**
* Calls Py_XDECREF(x) on the input object x. Usage pattern:
*
* PyObject* x = ... // builds x with a new python reference, x may be NULL
* auto protected_x = make_xsafe(x);
*
* After this point, no need to worry about XDECREF'ing x anymore.
* You can still use `x' inside your code, or protected_x.get(). Note
* `x' may be NULL with this method.
*/
template <typename T> std::shared_ptr<T> make_xsafe(T* o) {
return std::shared_ptr<T>(o, [&](T* p){Py_XDECREF(p);});
}
......@@ -5,11 +5,11 @@
* @brief Bindings to bob::io
*/
#include "cleanup.h"
#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
#include <xbob.blitz/cppapi.h>
#include <xbob.blitz/cleanup.h>
#include <bob/measure/error.h>
static int double1d_converter(PyObject* o, PyBlitzArrayObject** a) {
......@@ -1085,27 +1085,30 @@ static PyModuleDef module_definition = {
};
#endif
PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) {
static PyObject* create_module (void) {
# if PY_VERSION_HEX >= 0x03000000
PyObject* m = PyModule_Create(&module_definition);
if (!m) return 0;
# else
PyObject* m = Py_InitModule3(XBOB_EXT_MODULE_NAME,
module_methods, module_docstr);
if (!m) return;
PyObject* m = Py_InitModule3(XBOB_EXT_MODULE_NAME, module_methods, module_docstr);
# endif
if (!m) return 0;
auto m_ = make_safe(m); ///< protects against early returns
PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION);
if (PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION) < 0)
return 0;
/* imports the NumPy C-API */
import_array();
/* imports xbob.blitz C-API + dependencies */
if (import_xbob_blitz() < 0) return 0;
/* imports xbob.blitz C-API */
import_xbob_blitz();
Py_INCREF(m);
return m;
}
PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) {
# if PY_VERSION_HEX >= 0x03000000
return m;
return
# endif
create_module();
}
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