Skip to content
Snippets Groups Projects
Commit cd82ddea authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

Python 3 cleanup

parent 3261a3d3
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
*/ */
#define XBOB_LEARN_ACTIVATION_MODULE #define XBOB_LEARN_ACTIVATION_MODULE
#include "cleanup.h"
#include <xbob.learn.activation/api.h> #include <xbob.learn.activation/api.h>
#include <xbob.io/api.h> #include <xbob.io/api.h>
#include <xbob.blitz/cppapi.h> #include <xbob.blitz/cppapi.h>
#include <xbob.blitz/cleanup.h>
#include <bob/machine/Activation.h> #include <bob/machine/Activation.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
......
/**
* @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);});
}
...@@ -234,6 +234,8 @@ typedef struct { ...@@ -234,6 +234,8 @@ typedef struct {
# if !defined(NO_IMPORT_ARRAY) # if !defined(NO_IMPORT_ARRAY)
#include <xbob.io/api.h>
/** /**
* Returns -1 on error, 0 on success. PyCapsule_Import will set an exception * Returns -1 on error, 0 on success. PyCapsule_Import will set an exception
* if there's an error. * if there's an error.
...@@ -288,6 +290,13 @@ typedef struct { ...@@ -288,6 +290,13 @@ typedef struct {
return -1; return -1;
} }
/* Imports the xbob.blitz C-API */
if (import_xbob_io() < 0) {
PyErr_Print();
PyErr_SetString(PyExc_ImportError, "xbob.io failed to import");
return -1;
}
/* If you get to this point, all is good */ /* If you get to this point, all is good */
return 0; return 0;
......
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
#ifdef NO_IMPORT_ARRAY #ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY #undef NO_IMPORT_ARRAY
#endif #endif
#include <xbob.blitz/capi.h>
#include <xbob.io/api.h> #include <xbob.io/api.h>
#include <xbob.blitz/cleanup.h>
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
{0} /* Sentinel */ {0} /* Sentinel */
...@@ -33,86 +33,59 @@ static PyModuleDef module_definition = { ...@@ -33,86 +33,59 @@ static PyModuleDef module_definition = {
int PyXbobLearnActivation_APIVersion = XBOB_LEARN_ACTIVATION_API_VERSION; int PyXbobLearnActivation_APIVersion = XBOB_LEARN_ACTIVATION_API_VERSION;
PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) { static PyObject* create_module (void) {
PyBobLearnActivation_Type.tp_new = PyType_GenericNew; PyBobLearnActivation_Type.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyBobLearnActivation_Type) < 0) return if (PyType_Ready(&PyBobLearnActivation_Type) < 0) return 0;
# if PY_VERSION_HEX >= 0x03000000
0
# endif
;
PyBobLearnIdentityActivation_Type.tp_base = &PyBobLearnActivation_Type; PyBobLearnIdentityActivation_Type.tp_base = &PyBobLearnActivation_Type;
if (PyType_Ready(&PyBobLearnIdentityActivation_Type) < 0) return if (PyType_Ready(&PyBobLearnIdentityActivation_Type) < 0) return 0;
# if PY_VERSION_HEX >= 0x03000000
0
# endif
;
PyBobLearnLinearActivation_Type.tp_base = &PyBobLearnActivation_Type; PyBobLearnLinearActivation_Type.tp_base = &PyBobLearnActivation_Type;
if (PyType_Ready(&PyBobLearnLinearActivation_Type) < 0) return if (PyType_Ready(&PyBobLearnLinearActivation_Type) < 0) return 0;
# if PY_VERSION_HEX >= 0x03000000
0
# endif
;
PyBobLearnLogisticActivation_Type.tp_base = &PyBobLearnActivation_Type; PyBobLearnLogisticActivation_Type.tp_base = &PyBobLearnActivation_Type;
if (PyType_Ready(&PyBobLearnLogisticActivation_Type) < 0) return if (PyType_Ready(&PyBobLearnLogisticActivation_Type) < 0) return 0;
# if PY_VERSION_HEX >= 0x03000000
0
# endif
;
PyBobLearnHyperbolicTangentActivation_Type.tp_base = PyBobLearnHyperbolicTangentActivation_Type.tp_base =
&PyBobLearnActivation_Type; &PyBobLearnActivation_Type;
if (PyType_Ready(&PyBobLearnHyperbolicTangentActivation_Type) < 0) return if (PyType_Ready(&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0;
# if PY_VERSION_HEX >= 0x03000000
0
# endif
;
PyBobLearnMultipliedHyperbolicTangentActivation_Type.tp_base = PyBobLearnMultipliedHyperbolicTangentActivation_Type.tp_base =
&PyBobLearnActivation_Type; &PyBobLearnActivation_Type;
if (PyType_Ready(&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) if (PyType_Ready(&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0)
return return 0;
# if PY_VERSION_HEX >= 0x03000000
0
# endif
;
# if PY_VERSION_HEX >= 0x03000000 # if PY_VERSION_HEX >= 0x03000000
PyObject* m = PyModule_Create(&module_definition); PyObject* m = PyModule_Create(&module_definition);
if (!m) return 0;
# else # else
PyObject* m = Py_InitModule3(XBOB_EXT_MODULE_NAME, PyObject* m = Py_InitModule3(XBOB_EXT_MODULE_NAME, module_methods, module_docstr);
module_methods, module_docstr);
if (!m) return;
# endif # endif
if (!m) return 0;
auto m_ = make_safe(m);
/* register some constants */ /* register some constants */
PyModule_AddIntConstant(m, "__api_version__", if (PyModule_AddIntConstant(m, "__api_version__", XBOB_IO_API_VERSION) < 0) return 0;
XBOB_LEARN_ACTIVATION_API_VERSION); if (PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION) < 0) return 0;
PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION);
/* register the types to python */ /* register the types to python */
Py_INCREF(&PyBobLearnActivation_Type); Py_INCREF(&PyBobLearnActivation_Type);
PyModule_AddObject(m, "Activation", (PyObject *)&PyBobLearnActivation_Type); if (PyModule_AddObject(m, "Activation", (PyObject *)&PyBobLearnActivation_Type) < 0) return 0;
Py_INCREF(&PyBobLearnIdentityActivation_Type); Py_INCREF(&PyBobLearnIdentityActivation_Type);
PyModule_AddObject(m, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type); if (PyModule_AddObject(m, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type) < 0) return 0;
Py_INCREF(&PyBobLearnLinearActivation_Type); Py_INCREF(&PyBobLearnLinearActivation_Type);
PyModule_AddObject(m, "Linear", (PyObject *)&PyBobLearnLinearActivation_Type); if (PyModule_AddObject(m, "Linear", (PyObject *)&PyBobLearnLinearActivation_Type) < 0) return 0;
Py_INCREF(&PyBobLearnLogisticActivation_Type); Py_INCREF(&PyBobLearnLogisticActivation_Type);
PyModule_AddObject(m, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type); if (PyModule_AddObject(m, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type) < 0) return 0;
Py_INCREF(&PyBobLearnHyperbolicTangentActivation_Type); Py_INCREF(&PyBobLearnHyperbolicTangentActivation_Type);
PyModule_AddObject(m, "HyperbolicTangent", (PyObject *)&PyBobLearnHyperbolicTangentActivation_Type); if (PyModule_AddObject(m, "HyperbolicTangent", (PyObject *)&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0;
Py_INCREF(&PyBobLearnMultipliedHyperbolicTangentActivation_Type); Py_INCREF(&PyBobLearnMultipliedHyperbolicTangentActivation_Type);
PyModule_AddObject(m, "MultipliedHyperbolicTangent", (PyObject *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type); if (PyModule_AddObject(m, "MultipliedHyperbolicTangent", (PyObject *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) return 0;
static void* PyXbobLearnActivation_API[PyXbobLearnActivation_API_pointers]; static void* PyXbobLearnActivation_API[PyXbobLearnActivation_API_pointers];
...@@ -179,17 +152,17 @@ PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) { ...@@ -179,17 +152,17 @@ PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) {
if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object); if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object);
/* imports the NumPy C-API */ /* imports xbob.io C-API + dependencies */
import_array(); if (import_xbob_io() < 0) return 0;
/* imports xbob.blitz C-API */ Py_INCREF(m);
import_xbob_blitz(); return m;
/* imports xbob.io C-API */ }
import_xbob_io();
PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) {
# if PY_VERSION_HEX >= 0x03000000 # if PY_VERSION_HEX >= 0x03000000
return m; return
# endif # endif
create_module();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment