aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuciano Wolf <luciano.wolf@openbossa.org>2010-04-23 15:33:05 -0300
committerLuciano Wolf <luciano.wolf@openbossa.org>2010-04-23 15:33:05 -0300
commit55e4336840093a118da89602b43b91f36a115344 (patch)
tree00082a17ecee3f787b9aa00171104e46066783e1
parent4939c254ad0710783bc8cc1ce51a9228990ad5e2 (diff)
parentda4915fd1ddfd1e04a6ef21b0117d83fc7fe2d5d (diff)
Merge remote branch 'luck/fixprivctor'
-rw-r--r--cppgenerator.cpp12
-rw-r--r--tests/libsample/privatector.h61
-rw-r--r--tests/samplebinding/CMakeLists.txt1
-rw-r--r--tests/samplebinding/global.h1
-rwxr-xr-xtests/samplebinding/privatector_test.py79
-rw-r--r--tests/samplebinding/typesystem_sample.xml1
6 files changed, 151 insertions, 4 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index 50f50e6f6..08a597d66 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -1813,6 +1813,8 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass*
QString cppClassName = metaClass->qualifiedCppName();
QString className = cpythonTypeName(metaClass).replace(QRegExp("_Type$"), "");
QString baseClassName('0');
+ AbstractMetaFunctionList ctors = metaClass->queryFunctions(AbstractMetaClass::Constructors);
+ bool onlyPrivCtor = !metaClass->hasNonPrivateConstructor();
if (metaClass->hasArithmeticOperatorOverload()
|| metaClass->hasLogicalOperatorOverload()
@@ -1833,7 +1835,10 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass*
"Shiboken::deallocWrapperWithPrivateDtor" : "0";
tp_init = "0";
} else {
- tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES";//|Py_TPFLAGS_HAVE_GC";
+ if (onlyPrivCtor)
+ tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES";
+ else
+ tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES";
QString deallocClassName;
if (shouldGenerateCppWrapper(metaClass))
@@ -1843,14 +1848,13 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass*
tp_dealloc = "&Shiboken::deallocWrapper";
cpp_dtor = "&Shiboken::callCppDestructor<" + metaClass->qualifiedCppName() + " >";
- AbstractMetaFunctionList ctors = metaClass->queryFunctions(AbstractMetaClass::Constructors);
- tp_init = ctors.isEmpty() ? "0" : cpythonFunctionName(ctors.first());
+ tp_init = onlyPrivCtor ? "0" : cpythonFunctionName(ctors.first());
}
if (classNeedsGetattroFunction(metaClass))
tp_getattro = cpythonGetattroFunctionName(metaClass);
- if (metaClass->hasPrivateDestructor())
+ if (metaClass->hasPrivateDestructor() || onlyPrivCtor)
tp_new = "0";
else
tp_new = "Shiboken::SbkBaseWrapper_TpNew";
diff --git a/tests/libsample/privatector.h b/tests/libsample/privatector.h
new file mode 100644
index 000000000..5591f15f3
--- /dev/null
+++ b/tests/libsample/privatector.h
@@ -0,0 +1,61 @@
+/*
+ * This file is part of the Shiboken Python Binding Generator project.
+ *
+ * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation. Please
+ * review the following information to ensure the GNU Lesser General
+ * Public License version 2.1 requirements will be met:
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * As a special exception to the GNU Lesser General Public License
+ * version 2.1, the object code form of a "work that uses the Library"
+ * may incorporate material from a header file that is part of the
+ * Library. You may distribute such object code under terms of your
+ * choice, provided that the incorporated material (i) does not exceed
+ * more than 5% of the total size of the Library; and (ii) is limited to
+ * numerical parameters, data structure layouts, accessors, macros,
+ * inline functions and templates.
+ *
+ * This program 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 program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef PRIVATECTOR_H
+#define PRIVATECTOR_H
+
+#include "libsamplemacros.h"
+
+class LIBSAMPLE_API PrivateCtor
+{
+public:
+ static PrivateCtor* instance()
+ {
+ static PrivateCtor self;
+ self.m_instanciations++;
+ return &self;
+ }
+
+ int instanceCalls()
+ {
+ return m_instanciations;
+ }
+
+private:
+ int m_instanciations;
+
+ PrivateCtor() : m_instanciations(0) {}
+};
+
+#endif
diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt
index a2f58f91c..46d666fb4 100644
--- a/tests/samplebinding/CMakeLists.txt
+++ b/tests/samplebinding/CMakeLists.txt
@@ -45,6 +45,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/point_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/pointerholder_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/polygon_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/sample/privatector_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/privatedtor_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/protectednonpolymorphic_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphic_wrapper.cpp
diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h
index dd93c9a4a..2ad921331 100644
--- a/tests/samplebinding/global.h
+++ b/tests/samplebinding/global.h
@@ -24,6 +24,7 @@
#include "point.h"
#include "pointerholder.h"
#include "polygon.h"
+#include "privatector.h"
#include "privatedtor.h"
#include "protected.h"
#include "reference.h"
diff --git a/tests/samplebinding/privatector_test.py b/tests/samplebinding/privatector_test.py
new file mode 100755
index 000000000..ea713670f
--- /dev/null
+++ b/tests/samplebinding/privatector_test.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# This file is part of the Shiboken Python Bindings Generator project.
+#
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+#
+# Contact: PySide team <contact@pyside.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public License
+# version 2.1 as published by the Free Software Foundation. Please
+# review the following information to ensure the GNU Lesser General
+# Public License version 2.1 requirements will be met:
+# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+# #
+# This program 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 program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+'''Test cases for a class with only a private constructor.'''
+
+import gc
+import sys
+import unittest
+
+from sample import PrivateCtor
+
+
+class PrivateCtorTest(unittest.TestCase):
+ '''Test case for PrivateCtor class'''
+
+ def testPrivateCtorInstanciation(self):
+ '''Test if instanciation of class with a private constructor raises an exception.'''
+ self.assertRaises(TypeError, PrivateCtor)
+
+ def testPrivateCtorInheritance(self):
+ '''Test if inheriting from PrivateCtor raises an exception.'''
+ def inherit():
+ class Foo(PrivateCtor):
+ pass
+ self.assertRaises(TypeError, inherit)
+
+ def testPrivateCtorInstanceMethod(self):
+ '''Test if PrivateCtor.instance() method return the proper singleton.'''
+ pd1 = PrivateCtor.instance()
+ calls = pd1.instanceCalls()
+ self.assertEqual(type(pd1), PrivateCtor)
+ pd2 = PrivateCtor.instance()
+ self.assertEqual(pd2, pd1)
+ self.assertEqual(pd2.instanceCalls(), calls + 1)
+
+ def testPrivateCtorRefCounting(self):
+ '''Test refcounting of the singleton returned by PrivateCtor.instance().'''
+ pd1 = PrivateCtor.instance()
+ calls = pd1.instanceCalls()
+ refcnt = sys.getrefcount(pd1)
+ pd2 = PrivateCtor.instance()
+ self.assertEqual(pd2.instanceCalls(), calls + 1)
+ self.assertEqual(sys.getrefcount(pd2), sys.getrefcount(pd1))
+ self.assertEqual(sys.getrefcount(pd2), refcnt + 1)
+ del pd1
+ self.assertEqual(sys.getrefcount(pd2), refcnt)
+ del pd2
+ gc.collect()
+ pd3 = PrivateCtor.instance()
+ self.assertEqual(type(pd3), PrivateCtor)
+ self.assertEqual(pd3.instanceCalls(), calls + 2)
+ self.assertEqual(sys.getrefcount(pd3), refcnt)
+
+if __name__ == '__main__':
+ unittest.main()
+
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index 0f5327334..38311bd7c 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -1018,6 +1018,7 @@
<value-type name="VoidHolder"/>
+ <object-type name="PrivateCtor" />
<object-type name="PrivateDtor" />
<object-type name="Base1"/>