From cd82ddead1e1aefd392654a449ef869163015fba Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.dos.anjos@gmail.com> Date: Wed, 29 Jan 2014 17:25:16 +0100 Subject: [PATCH] Python 3 cleanup --- xbob/learn/activation/activation.cpp | 2 +- xbob/learn/activation/cleanup.h | 37 --------- .../include/xbob.learn.activation/api.h | 9 +++ xbob/learn/activation/main.cpp | 81 +++++++------------ 4 files changed, 37 insertions(+), 92 deletions(-) delete mode 100644 xbob/learn/activation/cleanup.h diff --git a/xbob/learn/activation/activation.cpp b/xbob/learn/activation/activation.cpp index 51ca123..995761f 100644 --- a/xbob/learn/activation/activation.cpp +++ b/xbob/learn/activation/activation.cpp @@ -8,10 +8,10 @@ */ #define XBOB_LEARN_ACTIVATION_MODULE -#include "cleanup.h" #include <xbob.learn.activation/api.h> #include <xbob.io/api.h> #include <xbob.blitz/cppapi.h> +#include <xbob.blitz/cleanup.h> #include <bob/machine/Activation.h> #include <boost/bind.hpp> #include <boost/function.hpp> diff --git a/xbob/learn/activation/cleanup.h b/xbob/learn/activation/cleanup.h deleted file mode 100644 index d6f0011..0000000 --- a/xbob/learn/activation/cleanup.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @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);}); -} diff --git a/xbob/learn/activation/include/xbob.learn.activation/api.h b/xbob/learn/activation/include/xbob.learn.activation/api.h index 3438307..21a1b18 100644 --- a/xbob/learn/activation/include/xbob.learn.activation/api.h +++ b/xbob/learn/activation/include/xbob.learn.activation/api.h @@ -234,6 +234,8 @@ typedef struct { # if !defined(NO_IMPORT_ARRAY) +#include <xbob.io/api.h> + /** * Returns -1 on error, 0 on success. PyCapsule_Import will set an exception * if there's an error. @@ -288,6 +290,13 @@ typedef struct { 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 */ return 0; diff --git a/xbob/learn/activation/main.cpp b/xbob/learn/activation/main.cpp index a2d929c..7dc7afb 100644 --- a/xbob/learn/activation/main.cpp +++ b/xbob/learn/activation/main.cpp @@ -11,8 +11,8 @@ #ifdef NO_IMPORT_ARRAY #undef NO_IMPORT_ARRAY #endif -#include <xbob.blitz/capi.h> #include <xbob.io/api.h> +#include <xbob.blitz/cleanup.h> static PyMethodDef module_methods[] = { {0} /* Sentinel */ @@ -33,86 +33,59 @@ static PyModuleDef module_definition = { 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; - if (PyType_Ready(&PyBobLearnActivation_Type) < 0) return -# if PY_VERSION_HEX >= 0x03000000 - 0 -# endif - ; + if (PyType_Ready(&PyBobLearnActivation_Type) < 0) return 0; PyBobLearnIdentityActivation_Type.tp_base = &PyBobLearnActivation_Type; - if (PyType_Ready(&PyBobLearnIdentityActivation_Type) < 0) return -# if PY_VERSION_HEX >= 0x03000000 - 0 -# endif - ; + if (PyType_Ready(&PyBobLearnIdentityActivation_Type) < 0) return 0; PyBobLearnLinearActivation_Type.tp_base = &PyBobLearnActivation_Type; - if (PyType_Ready(&PyBobLearnLinearActivation_Type) < 0) return -# if PY_VERSION_HEX >= 0x03000000 - 0 -# endif - ; + if (PyType_Ready(&PyBobLearnLinearActivation_Type) < 0) return 0; PyBobLearnLogisticActivation_Type.tp_base = &PyBobLearnActivation_Type; - if (PyType_Ready(&PyBobLearnLogisticActivation_Type) < 0) return -# if PY_VERSION_HEX >= 0x03000000 - 0 -# endif - ; + if (PyType_Ready(&PyBobLearnLogisticActivation_Type) < 0) return 0; PyBobLearnHyperbolicTangentActivation_Type.tp_base = &PyBobLearnActivation_Type; - if (PyType_Ready(&PyBobLearnHyperbolicTangentActivation_Type) < 0) return -# if PY_VERSION_HEX >= 0x03000000 - 0 -# endif - ; + if (PyType_Ready(&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0; PyBobLearnMultipliedHyperbolicTangentActivation_Type.tp_base = &PyBobLearnActivation_Type; if (PyType_Ready(&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) - return -# if PY_VERSION_HEX >= 0x03000000 - 0 -# endif - ; - + return 0; # 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); /* register some constants */ - PyModule_AddIntConstant(m, "__api_version__", - XBOB_LEARN_ACTIVATION_API_VERSION); - PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION); + if (PyModule_AddIntConstant(m, "__api_version__", XBOB_IO_API_VERSION) < 0) return 0; + if (PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION) < 0) return 0; /* register the types to python */ 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); - PyModule_AddObject(m, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type); + if (PyModule_AddObject(m, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type) < 0) return 0; 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); - PyModule_AddObject(m, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type); + if (PyModule_AddObject(m, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type) < 0) return 0; 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); - 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]; @@ -179,17 +152,17 @@ PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) { if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object); - /* imports the NumPy C-API */ - import_array(); + /* imports xbob.io C-API + dependencies */ + if (import_xbob_io() < 0) return 0; - /* imports xbob.blitz C-API */ - import_xbob_blitz(); + Py_INCREF(m); + return m; - /* imports xbob.io C-API */ - import_xbob_io(); +} +PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) { # if PY_VERSION_HEX >= 0x03000000 - return m; + return # endif - + create_module(); } -- GitLab