diff options
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libshiboken/basewrapper.cpp | 4 | ||||
-rw-r--r-- | libshiboken/basewrapper.h | 3 | ||||
-rw-r--r-- | libshiboken/sbkmodule.cpp | 84 | ||||
-rw-r--r-- | libshiboken/sbkmodule.h | 63 | ||||
-rw-r--r-- | libshiboken/shiboken.h | 1 |
6 files changed, 157 insertions, 0 deletions
diff --git a/libshiboken/CMakeLists.txt b/libshiboken/CMakeLists.txt index 38aa46bc7..81812e9aa 100644 --- a/libshiboken/CMakeLists.txt +++ b/libshiboken/CMakeLists.txt @@ -26,6 +26,7 @@ basewrapper.cpp gilstate.cpp helper.cpp sbkenum.cpp +sbkmodule.cpp bindingmanager.cpp threadstatesaver.cpp typeresolver.cpp @@ -50,6 +51,7 @@ install(FILES gilstate.h helper.h sbkenum.h + sbkmodule.h python25compat.h sbkdbg.h shiboken.h diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp index c1d30d101..6503616ab 100644 --- a/libshiboken/basewrapper.cpp +++ b/libshiboken/basewrapper.cpp @@ -397,12 +397,16 @@ void DtorCallerVisitor::done() } } +namespace Module { void init(); } + void init() { static bool shibokenAlreadInitialised = false; if (shibokenAlreadInitialised) return; + Module::init(); + initTypeResolver(); PyEval_InitThreads(); diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h index f9f2a36a3..4d08b7cd4 100644 --- a/libshiboken/basewrapper.h +++ b/libshiboken/basewrapper.h @@ -106,6 +106,9 @@ void callCppDestructor(void* cptr) delete reinterpret_cast<T*>(cptr); } +/** + * Shiboken::importModule is DEPRECATED. Use Shiboken::Module::import() instead. + */ LIBSHIBOKEN_API bool importModule(const char* moduleName, PyTypeObject*** cppApiPtr); LIBSHIBOKEN_API void setErrorAboutWrongArguments(PyObject* args, const char* funcName, const char** cppOverloads); diff --git a/libshiboken/sbkmodule.cpp b/libshiboken/sbkmodule.cpp new file mode 100644 index 000000000..998ccfc8f --- /dev/null +++ b/libshiboken/sbkmodule.cpp @@ -0,0 +1,84 @@ +/* + * This file is part of the Shiboken Python Bindings Generator project. + * + * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: PySide team <contact@pyside.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "sbkmodule.h" +#include "basewrapper.h" +#include "bindingmanager.h" + +// TODO: for performance reasons this should be a sparse_hash_map, +// because there'll be very few modules as keys. The sparse_hash_map +// is missing from the code added in ../ext/sparsehash/google directory. +#include "google/dense_hash_map" + +/// This hash maps module objects to arrays of Python types. +typedef google::dense_hash_map<PyObject*, PyTypeObject**> ModuleTypesMap; + +/// All types produced in imported modules are mapped here. +static ModuleTypesMap moduleTypes; + +namespace Shiboken +{ +namespace Module +{ + +void init() +{ + // Initializes type registry for modules. + moduleTypes.set_empty_key((ModuleTypesMap::key_type)0); + moduleTypes.set_deleted_key((ModuleTypesMap::key_type)1); +} + +PyObject* import(const char* moduleName) +{ + PyObject* sysModules = PyImport_GetModuleDict(); + PyObject* module = PyDict_GetItemString(sysModules, moduleName); + if (module) + Py_INCREF(module); + else + module = PyImport_ImportModule(moduleName); + + if (!module) + PyErr_Format(PyExc_ImportError,"could not import module '%s'", moduleName); + + return module; +} + +PyObject* create(const char* moduleName, void* moduleData) +{ + Shiboken::init(); + return Py_InitModule(moduleName, (PyMethodDef*)moduleData); +} + +void registerTypes(PyObject* module, PyTypeObject** types) +{ + ModuleTypesMap::iterator iter = moduleTypes.find(module); + if (iter == moduleTypes.end()) + moduleTypes.insert(std::make_pair(module, types)); +} + +PyTypeObject** getTypes(PyObject* module) +{ + ModuleTypesMap::iterator iter = moduleTypes.find(module); + return (iter == moduleTypes.end()) ? 0 : iter->second; +} + +} } // namespace Shiboken::Module diff --git a/libshiboken/sbkmodule.h b/libshiboken/sbkmodule.h new file mode 100644 index 000000000..028afec72 --- /dev/null +++ b/libshiboken/sbkmodule.h @@ -0,0 +1,63 @@ +/* + * This file is part of the Shiboken Python Bindings Generator project. + * + * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: PySide team <contact@pyside.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef SBK_MODULE_H +#define SBK_MODULE_H + +#include <Python.h> +#include "shibokenmacros.h" + +namespace Shiboken { +namespace Module { + +/** + * Imports and returns the module named \p moduleName, or a NULL pointer in case of failure. + * If the module is already imported, it increments its reference count before returning it. + * \returns the module specified in \p moduleName or NULL if an error occurs. + */ +LIBSHIBOKEN_API PyObject* import(const char* moduleName); + +/** + * Creates a new Python module named \p moduleName using the information passed in \p moduleData. + * In fact, \p moduleData expects a "PyMethodDef*" object, but that's for Python 2. A void* + * was preferred to make this work with future Python 3 support. + * \returns a newly created module. + */ +LIBSHIBOKEN_API PyObject* create(const char* moduleName, void* moduleData); + +/** + * Registers the list of types created by \p module. + * \param module Module where the types were created. + * \param types Array of PyTypeObject* objects representing the types created on \p module. + */ +LIBSHIBOKEN_API void registerTypes(PyObject* module, PyTypeObject** types); + +/** + * Retrieves the array of types. + * \param module Module where the types were created. + * \returns A pointer to the PyTypeObject* array of types. + */ +LIBSHIBOKEN_API PyTypeObject** getTypes(PyObject* module); + +} } // namespace Shiboken::Module + +#endif // SBK_MODULE_H diff --git a/libshiboken/shiboken.h b/libshiboken/shiboken.h index f2087fe0e..724784246 100644 --- a/libshiboken/shiboken.h +++ b/libshiboken/shiboken.h @@ -32,6 +32,7 @@ #include "threadstatesaver.h" #include "helper.h" #include "sbkenum.h" +#include "sbkmodule.h" #include "shibokenmacros.h" #include "typeresolver.h" #include "shibokenbuffer.h" |