aboutsummaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-05-03 16:41:38 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:15:22 -0300
commitc2d2ed94548d0e42c3dcc949b1b4f4990684f735 (patch)
treeebc9348884370deab28345fc9121ed74f8457a29 /generator
parentf26d9a83d5255d9ff8ce251790227ebe996d21c8 (diff)
Improved garbage collector handling of Shiboken types.
Diffstat (limited to 'generator')
-rw-r--r--generator/cppgenerator.cpp35
-rw-r--r--generator/cppgenerator.h3
2 files changed, 33 insertions, 5 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index dbc2a9456..c26536a1c 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -540,6 +540,11 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl
if (!metaClass->typeEntry()->hashFunction().isEmpty())
writeHashFunction(s, metaClass);
+
+ // Write tp_traverse and tp_clear functions.
+ writeTpTraverseFunction(s, metaClass);
+ writeTpClearFunction(s, metaClass);
+
writeClassDefinition(s, metaClass);
s << endl;
@@ -2540,15 +2545,15 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass*
bool onlyPrivCtor = !metaClass->hasNonPrivateConstructor();
if (metaClass->isNamespace() || metaClass->hasPrivateDestructor()) {
- tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES";
+ tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_GC";
tp_dealloc = metaClass->hasPrivateDestructor() ?
"SbkDeallocWrapperWithPrivateDtor" : "0";
tp_init = "0";
} else {
if (onlyPrivCtor)
- tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES";
+ tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_GC";
else
- tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES";
+ tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_GC";
QString deallocClassName;
if (shouldGenerateCppWrapper(metaClass))
@@ -2635,8 +2640,8 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass*
s << INDENT << "/*tp_as_buffer*/ 0," << endl;
s << INDENT << "/*tp_flags*/ " << tp_flags << ',' << endl;
s << INDENT << "/*tp_doc*/ 0," << endl;
- s << INDENT << "/*tp_traverse*/ 0," << endl;
- s << INDENT << "/*tp_clear*/ 0," << endl;
+ s << INDENT << "/*tp_traverse*/ " << className << "_traverse," << endl;
+ s << INDENT << "/*tp_clear*/ " << className << "_clear," << endl;
s << INDENT << "/*tp_richcompare*/ " << tp_richcompare << ',' << endl;
s << INDENT << "/*tp_weaklistoffset*/ 0," << endl;
s << INDENT << "/*tp_iter*/ " << m_tpFuncs["__iter__"] << ',' << endl;
@@ -2836,6 +2841,26 @@ void CppGenerator::writeTypeAsNumberDefinition(QTextStream& s, const AbstractMet
s << INDENT << baseName << "_Type.super.as_number.nb_true_divide = " << nb["__div__"] << ';' << endl;
}
+void CppGenerator::writeTpTraverseFunction(QTextStream& s, const AbstractMetaClass* metaClass)
+{
+ QString baseName = cpythonBaseName(metaClass);
+ s << "static int ";
+ s << baseName << "_traverse(PyObject* self, visitproc visit, void* arg)" << endl;
+ s << '{' << endl;
+ s << INDENT << "return reinterpret_cast<PyTypeObject*>(&SbkObject_Type)->tp_traverse(self, visit, arg);" << endl;
+ s << '}' << endl;
+}
+
+void CppGenerator::writeTpClearFunction(QTextStream& s, const AbstractMetaClass* metaClass)
+{
+ QString baseName = cpythonBaseName(metaClass);
+ s << "static int ";
+ s << baseName << "_clear(PyObject* self)" << endl;
+ s << '{' << endl;
+ s << INDENT << "return reinterpret_cast<PyTypeObject*>(&SbkObject_Type)->tp_clear(self);" << endl;
+ s << '}' << endl;
+}
+
void CppGenerator::writeCopyFunction(QTextStream& s, const AbstractMetaClass *metaClass)
{
QString className = cpythonTypeName(metaClass).replace(QRegExp("_Type$"), "");
diff --git a/generator/cppgenerator.h b/generator/cppgenerator.h
index 485c42361..a5709ec52 100644
--- a/generator/cppgenerator.h
+++ b/generator/cppgenerator.h
@@ -142,6 +142,9 @@ private:
void writeTypeAsNumberDefinition(QTextStream& s, const AbstractMetaClass* metaClass);
+ void writeTpTraverseFunction(QTextStream& s, const AbstractMetaClass* metaClass);
+ void writeTpClearFunction(QTextStream& s, const AbstractMetaClass* metaClass);
+
void writeCopyFunction(QTextStream& s, const AbstractMetaClass *metaClass);
void writeGetterFunction(QTextStream& s, const AbstractMetaField* metaField);