summaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/basewrapper.cpp69
-rw-r--r--libshiboken/basewrapper.h9
-rw-r--r--libshiboken/basewrapper_p.h4
-rw-r--r--libshiboken/bindingmanager.cpp18
4 files changed, 59 insertions, 41 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 0e2e7871..7c0e7a4e 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -36,7 +36,7 @@ namespace Shiboken
static void SbkBaseWrapperType_dealloc(PyObject* pyObj);
static PyObject* SbkBaseWrapperType_TpNew(PyTypeObject* metatype, PyObject* args, PyObject* kwds);
static void incRefPyObject(PyObject* pyObj);
-static void decRefPyObjectlist(const std::list<PyObject*> &pyObj);
+static void decRefPyObjectlist(const std::list<SbkBaseWrapper*> &pyObj);
extern "C"
{
@@ -251,7 +251,7 @@ static void _destroyParentInfo(SbkBaseWrapper* obj, bool removeFromParent)
if (!obj->d->containsCppWrapper && child->d->containsCppWrapper && child->d->parentInfo) {
child->d->parentInfo->parent = 0;
child->d->parentInfo->hasWrapperRef = true;
- SbkBaseWrapper_setOwnership(child, false);
+ child->d->hasOwnership = false;
} else {
_destroyParentInfo(child, false);
Py_DECREF(child);
@@ -412,7 +412,7 @@ void keepReference(SbkBaseWrapper* self, const char* key, PyObject* referredObje
}
if (!isNone) {
- std::list<PyObject*> values = splitPyObject(referredObject);
+ std::list<SbkBaseWrapper*> values = splitPyObject(referredObject);
if (append && (iter != refCountMap.end()))
refCountMap[key].insert(refCountMap[key].end(), values.begin(), values.end());
else
@@ -639,18 +639,20 @@ bool canCallConstructor(PyTypeObject* myType, PyTypeObject* ctorType)
return true;
}
-std::list<PyObject*> splitPyObject(PyObject* pyObj)
+std::list<SbkBaseWrapper*> splitPyObject(PyObject* pyObj)
{
- std::list<PyObject*> result;
+ std::list<SbkBaseWrapper*> result;
if (PySequence_Check(pyObj)) {
AutoDecRef lst(PySequence_Fast(pyObj, "Invalid keep reference object."));
- for(int i = 0, i_max = PySequence_Fast_GET_SIZE(lst.object()); i < i_max; i++) {
- PyObject* item = PySequence_Fast_GET_ITEM(lst.object(), i);
- if (isShibokenType(item))
- result.push_back(item);
+ if (!lst.isNull()) {
+ for(int i = 0, i_max = PySequence_Fast_GET_SIZE(lst.object()); i < i_max; i++) {
+ PyObject* item = PySequence_Fast_GET_ITEM(lst.object(), i);
+ if (isShibokenType(item))
+ result.push_back(reinterpret_cast<SbkBaseWrapper*>(item));
+ }
}
} else {
- result.push_back(pyObj);
+ result.push_back(reinterpret_cast<SbkBaseWrapper*>(pyObj));
}
return result;
}
@@ -666,29 +668,24 @@ static void incRefPyObject(PyObject* pyObj)
}
}
-static void decRefPyObjectlist(const std::list<PyObject*> &lst)
+static void decRefPyObjectlist(const std::list<SbkBaseWrapper*> &lst)
{
- std::list<PyObject*>::const_iterator iter = lst.begin();
+ std::list<SbkBaseWrapper*>::const_iterator iter = lst.begin();
while(iter != lst.end()) {
Py_DECREF(*iter);
- iter++;
+ ++iter;
}
}
-void SbkBaseWrapper_setOwnership(SbkBaseWrapper* pyobj, bool owner)
+static void setSequenceOwnership(PyObject* pyObj, bool owner)
{
- pyobj->d->hasOwnership = owner;
-}
-
-void SbkBaseWrapper_setOwnership(PyObject* pyobj, bool owner)
-{
- if (PySequence_Check(pyobj)) {
- std::list<PyObject*> objs = splitPyObject(pyobj);
- std::list<PyObject*>::const_iterator it;
- for(it=objs.begin(); it != objs.end(); it++)
- SbkBaseWrapper_setOwnership(reinterpret_cast<SbkBaseWrapper*>(*it), owner);
- } else if (isShibokenType(pyobj)) {
- SbkBaseWrapper_setOwnership(reinterpret_cast<SbkBaseWrapper*>(pyobj), owner);
+ if (PySequence_Check(pyObj)) {
+ std::list<SbkBaseWrapper*> objs = splitPyObject(pyObj);
+ std::list<SbkBaseWrapper*>::const_iterator it = objs.begin();
+ for(; it != objs.end(); ++it)
+ (*it)->d->hasOwnership = owner;
+ } else if (isShibokenType(pyObj)) {
+ reinterpret_cast<SbkBaseWrapper*>(pyObj)->d->hasOwnership = owner;
}
}
@@ -710,6 +707,26 @@ bool hasCppWrapper(SbkBaseWrapper* pyObj)
return pyObj->d->containsCppWrapper;
}
+void getOwnership(SbkBaseWrapper* pyObj)
+{
+ pyObj->d->hasOwnership = true;
+}
+
+void getOwnership(PyObject* pyObj)
+{
+ setSequenceOwnership(pyObj, true);
+}
+
+void releaseOwnership(SbkBaseWrapper* pyObj)
+{
+ pyObj->d->hasOwnership = false;
+}
+
+void releaseOwnership(PyObject* pyObj)
+{
+ setSequenceOwnership(pyObj, false);
+}
+
} // namespace Wrapper
} // namespace Shiboken
diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h
index 75fa9040..5d0d4728 100644
--- a/libshiboken/basewrapper.h
+++ b/libshiboken/basewrapper.h
@@ -219,16 +219,17 @@ LIBSHIBOKEN_API void deallocWrapperWithPrivateDtor(PyObject* self);
LIBSHIBOKEN_API bool importModule(const char* moduleName, PyTypeObject*** cppApiPtr);
LIBSHIBOKEN_API void setErrorAboutWrongArguments(PyObject* args, const char* funcName, const char** cppOverloads);
-/// Support sequence protocol
-LIBSHIBOKEN_API void SbkBaseWrapper_setOwnership(PyObject* pyobj, bool owner);
-LIBSHIBOKEN_API void SbkBaseWrapper_setOwnership(SbkBaseWrapper* pyobj, bool owner);
-
namespace Wrapper {
LIBSHIBOKEN_API void setValidCpp(SbkBaseWrapper* pyObj, bool value);
LIBSHIBOKEN_API void setHasCppWrapper(SbkBaseWrapper* pyObj, bool value);
LIBSHIBOKEN_API bool hasCppWrapper(SbkBaseWrapper* pyObj);
+LIBSHIBOKEN_API void getOwnership(PyObject* pyobj);
+LIBSHIBOKEN_API void getOwnership(SbkBaseWrapper* pyobj);
+LIBSHIBOKEN_API void releaseOwnership(PyObject* pyobj);
+LIBSHIBOKEN_API void releaseOwnership(SbkBaseWrapper* pyobj);
+
} // namespace Wrapper
} // namespace Shiboken
diff --git a/libshiboken/basewrapper_p.h b/libshiboken/basewrapper_p.h
index a07f5482..1a72781d 100644
--- a/libshiboken/basewrapper_p.h
+++ b/libshiboken/basewrapper_p.h
@@ -35,7 +35,7 @@ namespace Shiboken
* This mapping associates a method and argument of an wrapper object with the wrapper of
* said argument when it needs the binding to help manage its reference counting.
*/
-typedef std::map<std::string, std::list<PyObject*> > RefCountMap;
+typedef std::map<std::string, std::list<SbkBaseWrapper*> > RefCountMap;
/// Linked list of SbkBaseWrapper pointers
@@ -86,7 +86,7 @@ namespace Shiboken
/**
* Utility function uset to transform PyObject which suppot sequence protocol in a std::list
**/
-std::list<PyObject*> splitPyObject(PyObject* pyObj);
+std::list<SbkBaseWrapper*> splitPyObject(PyObject* pyObj);
struct SbkBaseWrapperType;
diff --git a/libshiboken/bindingmanager.cpp b/libshiboken/bindingmanager.cpp
index 22bfde74..5e9be699 100644
--- a/libshiboken/bindingmanager.cpp
+++ b/libshiboken/bindingmanager.cpp
@@ -250,10 +250,10 @@ PyObject* BindingManager::getOverride(const void* cptr, const char* methodName)
void BindingManager::invalidateWrapper(PyObject* pyobj)
{
- std::list<PyObject*> objs = splitPyObject(pyobj);
- std::list<PyObject*>::const_iterator it;
- for(it=objs.begin(); it != objs.end(); it++)
- invalidateWrapper(reinterpret_cast<SbkBaseWrapper*>(*it));
+ std::list<SbkBaseWrapper*> objs = splitPyObject(pyobj);
+ std::list<SbkBaseWrapper*>::const_iterator it = objs.begin();
+ for(; it != objs.end(); it++)
+ invalidateWrapper(*it);
}
void BindingManager::invalidateWrapper(SbkBaseWrapper* wrapper)
@@ -315,10 +315,10 @@ void BindingManager::destroyWrapper(SbkBaseWrapper* wrapper)
void BindingManager::transferOwnershipToCpp(PyObject* wrapper)
{
- std::list<PyObject*> objs = splitPyObject(wrapper);
- std::list<PyObject*>::const_iterator it;
- for(it=objs.begin(); it != objs.end(); it++)
- transferOwnershipToCpp(reinterpret_cast<SbkBaseWrapper*>(*it));
+ std::list<SbkBaseWrapper*> objs = splitPyObject(wrapper);
+ std::list<SbkBaseWrapper*>::const_iterator it = objs.begin();
+ for(; it != objs.end(); it++)
+ transferOwnershipToCpp(*it);
}
void BindingManager::transferOwnershipToCpp(SbkBaseWrapper* wrapper)
@@ -327,7 +327,7 @@ void BindingManager::transferOwnershipToCpp(SbkBaseWrapper* wrapper)
Shiboken::removeParent(wrapper);
if (wrapper->d->containsCppWrapper)
- SbkBaseWrapper_setOwnership(wrapper, false);
+ wrapper->d->hasOwnership = false;
else
invalidateWrapper(wrapper);
}