aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-07-16 17:31:08 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:04 -0300
commit5d61dc4c2d080c1e843d8958169c924c8b31f4de (patch)
tree93d12d7e4ea333f9c55935d5b070b4e8796436d0 /libshiboken
parenta456b8d969eb81331cf35cfbfab8690072bae692 (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.
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/CMakeLists.txt2
-rw-r--r--libshiboken/basewrapper.cpp4
-rw-r--r--libshiboken/basewrapper.h3
-rw-r--r--libshiboken/sbkmodule.cpp84
-rw-r--r--libshiboken/sbkmodule.h63
-rw-r--r--libshiboken/shiboken.h1
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"