aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-09-15 15:26:40 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:14 -0300
commit6bf564204fe977dccf94ed58ddea7fd5eaf4a1fa (patch)
tree8b58405cdf2eb1c2dcd295540037c0896628da39 /libshiboken
parent24206196a34b4faeb4b720efcc84747f35accf94 (diff)
Using Py_TYPE macro instead of direct access to ob_type to work with Python 2.x and 3.x.
Enclosing PyCObject uses inside #ifdefs
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/basewrapper.cpp32
-rw-r--r--libshiboken/bindingmanager.cpp12
-rw-r--r--libshiboken/conversions.h4
-rw-r--r--libshiboken/sbkenum.cpp2
-rw-r--r--libshiboken/sbkenum.h2
5 files changed, 26 insertions, 26 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 7527e3bb1..c4394e526 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -258,6 +258,7 @@ void SbkObjectTypeDealloc(PyObject* pyObj)
PyObject* SbkObjectTypeTpNew(PyTypeObject* metatype, PyObject* args, PyObject* kwds)
{
+#ifndef IS_PY3K
// Check if all bases are new style before calling type.tp_new
// Was causing gc assert errors in test_bug704.py when
// this check happened after creating the type object.
@@ -280,6 +281,7 @@ PyObject* SbkObjectTypeTpNew(PyTypeObject* metatype, PyObject* args, PyObject* k
return 0;
}
}
+#endif
// The meta type creates a new type when the Python programmer extends a wrapped C++ class.
SbkObjectType* newType = reinterpret_cast<SbkObjectType*>(PyType_Type.tp_new(metatype, args, kwds));
@@ -424,9 +426,14 @@ bool importModule(const char* moduleName, PyTypeObject*** cppApiPtr)
if (cppApi.isNull())
return false;
+#ifdef IS_PY3K
+ if (PyCapsule_CheckExact(cppApi))
+ *cppApiPtr = reinterpret_cast<PyTypeObject**>(PyCapsule_GetPointer(cppApi, 0));
+#else
+ // Python 2.6 doesn't have PyCapsule API, so let's keep usign PyCObject on all Python 2.x
if (PyCObject_Check(cppApi))
*cppApiPtr = reinterpret_cast<PyTypeObject**>(PyCObject_AsVoidPtr(cppApi));
-
+#endif
return true;
}
@@ -487,10 +494,7 @@ void setErrorAboutWrongArguments(PyObject* args, const char* funcName, const cha
if (i)
params += ", ";
PyObject* arg = PyTuple_GET_ITEM(args, i);
- if (PyCObject_Check(arg))
- params += "pointer";
- else
- params += arg->ob_type->tp_name;
+ params += arg->ob_type->tp_name;
}
} else {
params = args->ob_type->tp_name;
@@ -816,7 +820,7 @@ void getOwnership(PyObject* pyObj)
void releaseOwnership(SbkObject* self)
{
// skip if the ownership have already moved to c++
- SbkObjectType* selfType = reinterpret_cast<SbkObjectType*>(self->ob_type);
+ SbkObjectType* selfType = reinterpret_cast<SbkObjectType*>(Py_TYPE(self));
if (!self->d->hasOwnership || selfType->d->type_behaviour == BEHAVIOUR_VALUETYPE)
return;
@@ -924,7 +928,7 @@ bool hasParentInfo(SbkObject* pyObj)
void* cppPointer(SbkObject* pyObj, PyTypeObject* desiredType)
{
- PyTypeObject* type = pyObj->ob_type;
+ PyTypeObject* type = Py_TYPE(pyObj);
int idx = 0;
if (reinterpret_cast<SbkObjectType*>(type)->d->is_multicpp)
idx = getTypeIndexOnHierarchy(type, desiredType);
@@ -936,8 +940,8 @@ void* cppPointer(SbkObject* pyObj, PyTypeObject* desiredType)
bool setCppPointer(SbkObject* sbkObj, PyTypeObject* desiredType, void* cptr)
{
int idx = 0;
- if (reinterpret_cast<SbkObjectType*>(sbkObj->ob_type)->d->is_multicpp)
- idx = getTypeIndexOnHierarchy(sbkObj->ob_type, desiredType);
+ if (reinterpret_cast<SbkObjectType*>(Py_TYPE(sbkObj))->d->is_multicpp)
+ idx = getTypeIndexOnHierarchy(Py_TYPE(sbkObj), desiredType);
bool alreadyInitialized = sbkObj->d->cptr[idx];
if (alreadyInitialized)
@@ -952,7 +956,7 @@ bool setCppPointer(SbkObject* sbkObj, PyTypeObject* desiredType, void* cptr)
bool isValid(PyObject* pyObj)
{
if (!pyObj || pyObj == Py_None
- || pyObj->ob_type->ob_type != &SbkObjectType_Type) {
+ || Py_TYPE(pyObj->ob_type) != &SbkObjectType_Type) {
return true;
}
@@ -979,13 +983,13 @@ bool isValid(SbkObject* pyObj, bool throwPyError)
SbkObjectPrivate* priv = pyObj->d;
if (!priv->cppObjectCreated && isUserType(reinterpret_cast<PyObject*>(pyObj))) {
if (throwPyError)
- PyErr_Format(PyExc_RuntimeError, "Base constructor of the object (%s) not called.", pyObj->ob_type->tp_name);
+ PyErr_Format(PyExc_RuntimeError, "Base constructor of the object (%s) not called.", Py_TYPE(pyObj)->tp_name);
return false;
}
if (!priv->validCppObject) {
if (throwPyError)
- PyErr_Format(PyExc_RuntimeError, "Internal C++ object (%s) already deleted.", pyObj->ob_type->tp_name);
+ PyErr_Format(PyExc_RuntimeError, "Internal C++ object (%s) already deleted.", Py_TYPE(pyObj)->tp_name);
return false;
}
@@ -1209,7 +1213,7 @@ void deallocData(SbkObject* self, bool cleanup)
void setTypeUserData(SbkObject* wrapper, void* userData, DeleteUserDataFunc d_func)
{
- SbkObjectType* ob_type = reinterpret_cast<SbkObjectType*>(wrapper->ob_type);
+ SbkObjectType* ob_type = reinterpret_cast<SbkObjectType*>(Py_TYPE(wrapper));
if (ob_type->d->user_data)
ob_type->d->d_func(ob_type->d->user_data);
@@ -1219,7 +1223,7 @@ void setTypeUserData(SbkObject* wrapper, void* userData, DeleteUserDataFunc d_fu
void* getTypeUserData(SbkObject* wrapper)
{
- return reinterpret_cast<SbkObjectType*>(wrapper->ob_type)->d->user_data;
+ return reinterpret_cast<SbkObjectType*>(Py_TYPE(wrapper))->d->user_data;
}
void keepReference(SbkObject* self, const char* key, PyObject* referredObject, bool append)
diff --git a/libshiboken/bindingmanager.cpp b/libshiboken/bindingmanager.cpp
index 09d6fc962..77a384b5f 100644
--- a/libshiboken/bindingmanager.cpp
+++ b/libshiboken/bindingmanager.cpp
@@ -108,8 +108,8 @@ static void showWrapperMap(const WrapperMap& wrapperMap)
for (iter = wrapperMap.begin(); iter != wrapperMap.end(); ++iter) {
fprintf(stderr, "key: %p, value: %p (%s, refcnt: %d)\n", iter->first,
iter->second,
- iter->second->ob_type->tp_name,
- (int) iter->second->ob_refcnt);
+ Py_TYPE(iter->second)->tp_name,
+ (int) ((PyObject*)iter->second)->ob_refcnt);
}
fprintf(stderr, "-------------------------------\n");
}
@@ -176,7 +176,7 @@ bool BindingManager::hasWrapper(const void* cptr)
void BindingManager::registerWrapper(SbkObject* pyObj, void* cptr)
{
- SbkObjectType* instanceType = reinterpret_cast<SbkObjectType*>(pyObj->ob_type);
+ SbkObjectType* instanceType = reinterpret_cast<SbkObjectType*>(Py_TYPE(pyObj));
SbkObjectTypePrivate* d = instanceType->d;
if (!d)
@@ -197,9 +197,9 @@ void BindingManager::registerWrapper(SbkObject* pyObj, void* cptr)
void BindingManager::releaseWrapper(SbkObject* sbkObj)
{
- SbkObjectType* sbkType = reinterpret_cast<SbkObjectType*>(sbkObj->ob_type);
+ SbkObjectType* sbkType = reinterpret_cast<SbkObjectType*>(Py_TYPE(sbkObj));
SbkObjectTypePrivate* d = sbkType->d;
- int numBases = ((d && d->is_multicpp) ? getNumberOfCppBaseClasses(sbkObj->ob_type) : 1);
+ int numBases = ((d && d->is_multicpp) ? getNumberOfCppBaseClasses(Py_TYPE(sbkObj)) : 1);
void** cptrs = reinterpret_cast<SbkObject*>(sbkObj)->d->cptr;
for (int i = 0; i < numBases; ++i) {
@@ -247,7 +247,7 @@ PyObject* BindingManager::getOverride(const void* cptr, const char* methodName)
if (method && PyMethod_Check(method)
&& reinterpret_cast<PyMethodObject*>(method)->im_self == reinterpret_cast<PyObject*>(wrapper)) {
PyObject* defaultMethod;
- PyObject* mro = wrapper->ob_type->tp_mro;
+ PyObject* mro = Py_TYPE(wrapper)->tp_mro;
// The first class in the mro (index 0) is the class being checked and it should not be tested.
// The last class in the mro (size - 1) is the base Python object class which should not be tested also.
diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h
index ebe5c6c56..fa42622fa 100644
--- a/libshiboken/conversions.h
+++ b/libshiboken/conversions.h
@@ -38,10 +38,6 @@
#define PyObject_Check(X) true
#include "autodecref.h"
-// Note: if there wasn't for the old-style classes, only a PyNumber_Check would suffice.
-#define SbkNumber_Check(X) \
- (PyNumber_Check(X) && (!PyInstance_Check(X) || PyObject_HasAttrString(X, "__trunc__")))
-
namespace Shiboken
{
/**
diff --git a/libshiboken/sbkenum.cpp b/libshiboken/sbkenum.cpp
index f8d284ed7..013e4d77e 100644
--- a/libshiboken/sbkenum.cpp
+++ b/libshiboken/sbkenum.cpp
@@ -290,7 +290,7 @@ PyTypeObject* newTypeWithName(const char* name, const char* cppName)
{
PyTypeObject* type = new PyTypeObject;
::memset(type, 0, sizeof(PyTypeObject));
- type->ob_type = &SbkEnumType_Type;
+ Py_TYPE(type) = &SbkEnumType_Type;
type->tp_basicsize = sizeof(SbkEnumObject);
type->tp_print = &SbkEnumObject_print;
type->tp_repr = &SbkEnumObject_repr;
diff --git a/libshiboken/sbkenum.h b/libshiboken/sbkenum.h
index 4fffd1bad..510ab6b87 100644
--- a/libshiboken/sbkenum.h
+++ b/libshiboken/sbkenum.h
@@ -39,7 +39,7 @@ namespace Shiboken
inline bool isShibokenEnum(PyObject* pyObj)
{
- return pyObj->ob_type->ob_type == &SbkEnumType_Type;
+ return Py_TYPE(pyObj->ob_type) == &SbkEnumType_Type;
}
namespace Enum