summaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-12-09 19:00:11 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2011-12-09 19:10:31 -0200
commitca34e36bb1c9a017a9bf11ae3769afb059194788 (patch)
tree7aff346b32c7711e7cb80180879293e8f49c3877 /libshiboken
parent00c3a199c032d538ea4e0fb27258332225d2d5e7 (diff)
downloadshiboken-ca34e36bb1c9a017a9bf11ae3769afb059194788.tar.gz
shiboken-ca34e36bb1c9a017a9bf11ae3769afb059194788.tar.xz
shiboken-ca34e36bb1c9a017a9bf11ae3769afb059194788.zip
Implemented PSEP 0106.
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Paulo Alcantra <pcacjr@gmail.com>
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/CMakeLists.txt4
-rw-r--r--libshiboken/basewrapper.cpp101
-rw-r--r--libshiboken/basewrapper.h29
-rw-r--r--libshiboken/basewrapper_p.h13
-rw-r--r--libshiboken/sbkversion.h.in33
5 files changed, 175 insertions, 5 deletions
diff --git a/libshiboken/CMakeLists.txt b/libshiboken/CMakeLists.txt
index 7d8a7ded..b57a814c 100644
--- a/libshiboken/CMakeLists.txt
+++ b/libshiboken/CMakeLists.txt
@@ -7,6 +7,8 @@ else()
set(shiboken_SUFFIX "")
endif()
+configure_file(sbkversion.h.in sbkversion.h @ONLY)
+
#Find installed sparsehash
find_path(SPARSEHASH_INCLUDE_PATH sparseconfig.h PATH_SUFFIXES "/google/sparsehash")
if(SPARSEHASH_INCLUDE_PATH)
@@ -35,6 +37,7 @@ shibokenbuffer.cpp
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}
${SBK_PYTHON_INCLUDE_DIR}
${SPARSEHASH_INCLUDE_PATH})
add_library(libshiboken SHARED ${libshiboken_SRC})
@@ -62,6 +65,7 @@ install(FILES
typeresolver.h
shibokenbuffer.h
sbkpython.h
+ "${CMAKE_CURRENT_BINARY_DIR}/sbkversion.h"
DESTINATION include/shiboken${shiboken_SUFFIX})
install(TARGETS libshiboken EXPORT shiboken
LIBRARY DESTINATION "${LIB_INSTALL_DIR}"
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 42a04db7..2baa5b85 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -29,6 +29,8 @@
#include <string>
#include <cstring>
#include <cstddef>
+#include <set>
+#include <sstream>
#include <algorithm>
#include "threadstatesaver.h"
@@ -210,7 +212,7 @@ void SbkDeallocWrapper(PyObject* pyObj)
if (sbkObj->d->hasOwnership && sbkObj->d->validCppObject) {
SbkObjectType* sbkType = reinterpret_cast<SbkObjectType*>(pyObj->ob_type);
if (sbkType->d->is_multicpp) {
- Shiboken::DtorCallerVisitor visitor(sbkObj);
+ Shiboken::DeallocVisitor visitor(sbkObj);
Shiboken::walkThroughClassHierarchy(pyObj->ob_type, &visitor);
} else {
void* cptr = sbkObj->d->cptr[0];
@@ -444,8 +446,6 @@ void DtorCallerVisitor::visit(SbkObjectType* node)
void DtorCallerVisitor::done()
{
- Shiboken::Object::deallocData(m_pyObj, true);
-
std::list<std::pair<void*, SbkObjectType*> >::const_iterator it = m_ptrs.begin();
for (; it != m_ptrs.end(); ++it) {
Shiboken::ThreadStateSaver threadSaver;
@@ -454,6 +454,12 @@ void DtorCallerVisitor::done()
}
}
+void DeallocVisitor::done()
+{
+ Shiboken::Object::deallocData(m_pyObj, true);
+ DtorCallerVisitor::done();
+}
+
namespace Module { void init(); }
void init()
@@ -785,6 +791,27 @@ bool hasCppWrapper(SbkObject* pyObj)
return pyObj->d->containsCppWrapper;
}
+bool wasCreatedByPython(SbkObject* pyObj)
+{
+ return pyObj->d->cppObjectCreated;
+}
+
+void callCppDestructors(SbkObject* pyObj)
+{
+ SbkObjectType* sbkType = reinterpret_cast<SbkObjectType*>(Py_TYPE(pyObj));
+ if (sbkType->d->is_multicpp) {
+ Shiboken::DtorCallerVisitor visitor(pyObj);
+ Shiboken::walkThroughClassHierarchy(Py_TYPE(pyObj), &visitor);
+ } else {
+ Shiboken::ThreadStateSaver threadSaver;
+ threadSaver.save();
+ sbkType->d->cpp_dtor(pyObj->d->cptr[0]);
+ }
+ delete[] pyObj->d->cptr;
+ pyObj->d->cptr = 0;
+ invalidate(pyObj);
+}
+
bool hasOwnership(SbkObject* pyObj)
{
return pyObj->d->hasOwnership;
@@ -935,6 +962,16 @@ void* cppPointer(SbkObject* pyObj, PyTypeObject* desiredType)
return 0;
}
+std::vector<void*> cppPointers(SbkObject* pyObj)
+{
+ int n = getNumberOfCppBaseClasses(Py_TYPE(pyObj));
+ std::vector<void*> ptrs(n);
+ for (int i = 0; i < n; ++i)
+ ptrs[i] = pyObj->d->cptr[i];
+ return ptrs;
+}
+
+
bool setCppPointer(SbkObject* sbkObj, PyTypeObject* desiredType, void* cptr)
{
int idx = 0;
@@ -1282,6 +1319,64 @@ void clearReferences(SbkObject* self)
self->d->referredObjects->clear();
}
+std::string info(SbkObject* self)
+{
+ std::ostringstream s;
+ std::list<SbkObjectType*> bases;
+
+ if (ObjectType::isUserType(Py_TYPE(self)))
+ bases = getCppBaseClasses(Py_TYPE(self));
+ else
+ bases.push_back(reinterpret_cast<SbkObjectType*>(Py_TYPE(self)));
+
+ s << "C++ address....... ";
+ std::list<SbkObjectType*>::const_iterator it = bases.begin();
+ for (int i = 0; it != bases.end(); ++it, ++i)
+ s << ((PyTypeObject*)*it)->tp_name << "/" << self->d->cptr[i] << ' ';
+ s << "\n";
+
+ s << "hasOwnership...... " << bool(self->d->hasOwnership) << "\n"
+ "containsCppWrapper " << self->d->containsCppWrapper << "\n"
+ "validCppObject.... " << self->d->validCppObject << "\n"
+ "wasCreatedByPython " << self->d->cppObjectCreated << "\n";
+
+
+ if (self->d->parentInfo && self->d->parentInfo->parent) {
+ s << "parent............ ";
+ Shiboken::AutoDecRef parent(PyObject_Str((PyObject*)self->d->parentInfo->parent));
+ s << String::toCString(parent) << "\n";
+ }
+
+ if (self->d->parentInfo && self->d->parentInfo->children.size()) {
+ s << "children.......... ";
+ ChildrenList& children = self->d->parentInfo->children;
+ for (ChildrenList::const_iterator it = children.begin(); it != children.end(); ++it) {
+ Shiboken::AutoDecRef child(PyObject_Str((PyObject*)*it));
+ s << String::toCString(child) << ' ';
+ }
+ s << '\n';
+ }
+
+ if (self->d->referredObjects && self->d->referredObjects->size()) {
+ Shiboken::RefCountMap& map = *self->d->referredObjects;
+ s << "referred objects.. ";
+ Shiboken::RefCountMap::const_iterator it = map.begin();
+ for (; it != map.end(); ++it) {
+ if (it != map.begin())
+ s << " ";
+ s << '"' << it->first << "\" => ";
+ std::list<PyObject*>::const_iterator j = it->second.begin();
+ for (; j != it->second.end(); ++j) {
+ Shiboken::AutoDecRef obj(PyObject_Str(*j));
+ s << String::toCString(obj) << ' ';
+ }
+ s << ' ';
+ }
+ s << '\n';
+ }
+ return s.str();
+}
+
} // namespace Object
} // namespace Shiboken
diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h
index 835b1d1d..1d4b9643 100644
--- a/libshiboken/basewrapper.h
+++ b/libshiboken/basewrapper.h
@@ -1,7 +1,7 @@
/*
* This file is part of the Shiboken Python Bindings Generator project.
*
- * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
+ * Copyright (C) 2009-2011 Nokia Corporation and/or its subsidiary(-ies).
*
* Contact: PySide team <contact@pyside.org>
*
@@ -26,6 +26,7 @@
#include "sbkpython.h"
#include "bindingmanager.h"
#include <list>
+#include <vector>
#include <map>
#include <string>
@@ -210,9 +211,15 @@ LIBSHIBOKEN_API void setTypeUserData(SbkObjectType* self, void* userData,
namespace Object {
/**
+ * Returns a string with information about the internal state of the instance object, useful for debug purposes.
+ */
+LIBSHIBOKEN_API std::string info(SbkObject* self);
+
+/**
* Returns true if the object is an instance of a type created by the Shiboken generator.
*/
LIBSHIBOKEN_API bool checkType(PyObject* pyObj);
+
/**
* Returns true if this object type is an instance of an user defined type derived from an Shiboken type.
* \see Shiboken::ObjectType::isUserType
@@ -248,9 +255,22 @@ LIBSHIBOKEN_API void setHasCppWrapper(SbkObject* pyObj, bool value);
LIBSHIBOKEN_API bool hasCppWrapper(SbkObject* pyObj);
/**
+ * Return true if the Python object was created by Python, false otherwise.
+ * \note This function was added to libshiboken only to be used by shiboken.wasCreatedByPython()
+ */
+LIBSHIBOKEN_API bool wasCreatedByPython(SbkObject* pyObj);
+
+/**
+ * Call the C++ object destructor and invalidates the Python object.
+ * \note This function was added to libshiboken only to be used by shiboken.delete()
+ */
+LIBSHIBOKEN_API void callCppDestructors(SbkObject* pyObj);
+
+/**
* Return true if the Python is responsible for deleting the underlying C++ object.
*/
LIBSHIBOKEN_API bool hasOwnership(SbkObject* pyObj);
+
/**
* Sets python as responsible to delete the underlying C++ object.
* \note You this overload only when the PyObject can be a sequence and you want to
@@ -258,6 +278,7 @@ LIBSHIBOKEN_API bool hasOwnership(SbkObject* pyObj);
* \see getOwnership(SbkObject*)
*/
LIBSHIBOKEN_API void getOwnership(PyObject* pyObj);
+
/**
* Sets python as responsible to delete the underlying C++ object.
*/
@@ -286,6 +307,12 @@ LIBSHIBOKEN_API bool hasParentInfo(SbkObject* pyObj);
LIBSHIBOKEN_API void* cppPointer(SbkObject* pyObj, PyTypeObject* desiredType);
/**
+ * Return a list with all C++ pointers held from a Python object.
+ * \note This function was added to libshiboken only to be used by shiboken.getCppPointer()
+ */
+LIBSHIBOKEN_API std::vector<void*> cppPointers(SbkObject* pyObj);
+
+/**
* Set the C++ pointer of type \p desiredType of a Python object.
*/
LIBSHIBOKEN_API bool setCppPointer(SbkObject* sbkObj, PyTypeObject* desiredType, void* cptr);
diff --git a/libshiboken/basewrapper_p.h b/libshiboken/basewrapper_p.h
index 0344da53..9f96ba75 100644
--- a/libshiboken/basewrapper_p.h
+++ b/libshiboken/basewrapper_p.h
@@ -26,6 +26,8 @@
#include "sbkpython.h"
#include <list>
#include <map>
+#include <set>
+#include <string>
struct SbkObject;
struct SbkObjectType;
@@ -197,17 +199,26 @@ private:
PyTypeObject* m_desiredType;
};
+/// Call the destructor of each C++ object held by a Python object
class DtorCallerVisitor : public HierarchyVisitor
{
public:
DtorCallerVisitor(SbkObject* pyObj) : m_pyObj(pyObj) {}
void visit(SbkObjectType* node);
void done();
-private:
+protected:
std::list<std::pair<void*, SbkObjectType*> > m_ptrs;
SbkObject* m_pyObj;
};
+/// Dealloc of each C++ object held by a Python object, this implies a call to the C++ object destructor
+class DeallocVisitor : public DtorCallerVisitor
+{
+public:
+ DeallocVisitor(SbkObject* pyObj) : DtorCallerVisitor(pyObj) {}
+ void done();
+};
+
/// \internal Internal function used to walk on classes inheritance trees.
/**
* Walk on class hierarchy using a DFS algorithm.
diff --git a/libshiboken/sbkversion.h.in b/libshiboken/sbkversion.h.in
new file mode 100644
index 00000000..d58b4407
--- /dev/null
+++ b/libshiboken/sbkversion.h.in
@@ -0,0 +1,33 @@
+/*
+ * 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 SBKVERSION_H
+#define SBKVERSION_H
+
+#define SHIBOKEN_VERSION "@shiboken_MAJOR_VERSION@.@shiboken_MINOR_VERSION@.@shiboken_MICRO_VERSION@"
+#define SHIBOKEN_MAJOR_VERSION @shiboken_MAJOR_VERSION@
+#define SHIBOKEN_MINOR_VERSION @shiboken_MINOR_VERSION@
+#define SHIBOKEN_MICRO_VERSION @shiboken_MICRO_VERSION@
+#define SHIBOKEN_RELEASE_LEVEL "final"
+#define SHIBOKEN_SERIAL 0
+
+#endif