diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-07-16 17:31:08 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:17:04 -0300 |
commit | 5d61dc4c2d080c1e843d8958169c924c8b31f4de (patch) | |
tree | 93d12d7e4ea333f9c55935d5b070b4e8796436d0 | |
parent | a456b8d969eb81331cf35cfbfab8690072bae692 (diff) |
Introduces new API to create and import Python modules.
The Shiboken::Module namespace also provides management of module types,
including the communication of types among dependent modules.
Module::create() will call Shiboken::init(), so this can be removed
from the generated module's source code.
This deprecates the old Shiboken::importModule() function.
The generation of module initialization code was updated to use the new
Shiboken::Module functions.
-rw-r--r-- | generator/cppgenerator.cpp | 35 | ||||
-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 |
7 files changed, 180 insertions, 12 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 663163888..b8204ac5d 100644 --- a/generator/cppgenerator.cpp +++ b/generator/cppgenerator.cpp @@ -3909,9 +3909,12 @@ void CppGenerator::finishGeneration() } } - s << "PyTypeObject** " << cppApiVariableName() << ";" << endl << endl;; + s << "// Current module's type array." << endl; + s << "PyTypeObject** " << cppApiVariableName() << ';' << endl; + s << "// Required modules' type arrays." << endl; foreach (const QString& requiredModule, typeDb->requiredTargetImports()) - s << "PyTypeObject** " << cppApiVariableName(requiredModule) << ";" << endl << endl;; + s << "PyTypeObject** " << cppApiVariableName(requiredModule) << ';' << endl; + s << endl; s << "// Module initialization "; s << "------------------------------------------------------------" << endl; @@ -3944,21 +3947,27 @@ void CppGenerator::finishGeneration() } foreach (const QString& requiredModule, typeDb->requiredTargetImports()) { - s << INDENT << "if (!Shiboken::importModule(\"" << requiredModule << "\", &" << cppApiVariableName(requiredModule) << ")) {" << endl; - s << INDENT << INDENT << "PyErr_SetString(PyExc_ImportError," << "\"could not import "; - s << requiredModule << "\");" << endl << INDENT << INDENT << "return;" << endl; + s << INDENT << "{" << endl; + { + Indentation indentation(INDENT); + s << INDENT << "Shiboken::AutoDecRef requiredModule(Shiboken::Module::import(\"" << requiredModule << "\"));" << endl; + s << INDENT << "if (requiredModule.isNull())" << endl; + { + Indentation indentation(INDENT); + s << INDENT << "return;" << endl; + } + s << INDENT << cppApiVariableName(requiredModule) << " = Shiboken::Module::getTypes(requiredModule);" << endl; + } s << INDENT << "}" << endl << endl; } - s << INDENT << "Shiboken::init();" << endl; - s << INDENT << "PyObject* module = Py_InitModule(\"" << moduleName() << "\", "; + s << INDENT << "// Create an array of wrapper types for the current module." << endl; + s << INDENT << "static PyTypeObject* cppApi[" << "SBK_" << moduleName() << "_IDX_COUNT" << "];" << endl; + s << INDENT << cppApiVariableName() << " = cppApi;" << endl << endl; + + s << INDENT << "PyObject* module = Shiboken::Module::create(\"" << moduleName() << "\", "; s << moduleName() << "_methods);" << endl << endl; - s << INDENT << "// Create a CObject containing the API pointer array's address" << endl; - s << INDENT << "static PyTypeObject* cppApi[" << "SBK_" << moduleName() << "_IDX_COUNT" << "];" << endl; - s << INDENT << cppApiVariableName() << " = cppApi;" << endl; - s << INDENT << "PyObject* cppApiObject = PyCObject_FromVoidPtr(reinterpret_cast<void*>(cppApi), 0);" << endl; - s << INDENT << "PyModule_AddObject(module, \"_Cpp_Api\", cppApiObject);" << endl << endl; s << INDENT << "// Initialize classes in the type system" << endl; s << classPythonDefines; @@ -4010,6 +4019,8 @@ void CppGenerator::finishGeneration() foreach (QByteArray type, typeResolvers) s << INDENT << "Shiboken::TypeResolver::createValueTypeResolver< ::" << type << " >(\"" << type << "\");" << endl; + s << endl << INDENT << "Shiboken::Module::registerTypes(module, " << cppApiVariableName() << ");" << endl; + s << endl << INDENT << "if (PyErr_Occurred()) {" << endl; { Indentation indentation(INDENT); 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" |