Commits

Ronald Oussoren committed 2c705a0

Add a testcase for bug #1206669

Comments (0)

Files changed (2)

Lib/objc/test/test_initialized.py

+import unittest
+from objc.test.initialize import OC_TestInitialize
+
+class OC_TestInitializePython (OC_TestInitialize):
+    def init(self):
+        return super(OC_TestInitializePython, self).init()
+
+class TestInitializing (unittest.TestCase):
+    #
+    # These tests make sure that we don't call retain/release on objects
+    # that are not yet initialized.
+    #
+    def testDontRetainUnitialized1(self):
+        start = OC_TestInitialize.numUninitialized()
+        self.assertEquals(start, 0)
+
+        o = OC_TestInitialize.alloc()
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+        o = o.init()
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+        s = o.dummy()
+        self.assertEquals(s, u"hello")
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+    def testDontRetainUnitialized2(self):
+        start = OC_TestInitialize.numUninitialized()
+        self.assertEquals(start, 0)
+
+        o = OC_TestInitialize.makeInstance()
+        self.assert_(isinstance(o, OC_TestInitialize))
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+        s = o.dummy()
+        self.assertEquals(s, u"hello")
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+    def testDontRetainUnitialized3(self):
+        start = OC_TestInitialize.numUninitialized()
+        self.assertEquals(start, 0)
+
+        o = OC_TestInitializePython.makeInstance()
+        self.assert_(isinstance(o, OC_TestInitializePython))
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+        s = o.dummy()
+        self.assertEquals(s, u"hello")
+        v = OC_TestInitialize.numUninitialized()
+        self.assertEquals(v, start)
+
+if __name__ == "__main__":
+    unittest.main()

Modules/objc/test/initialize.m

+/*
+ * This module is used in the unittests for object initialize.
+ */
+#include "Python.h"
+#include "pyobjc-api.h"
+
+#import <Foundation/Foundation.h>
+
+static int numUninitialized = 0;
+
+@interface OC_TestInitialize : NSObject
+{
+	int       isInitialized;
+}
+-init;
+-retain;
+-(void)release;
+-autorelease;
+-(int)isInitialized;
++(int)numUninitialized;
+-(id)dummy;
++(id)makeInstance;
+
+@end
+
+@implementation OC_TestInitialize 
+
+-init
+{
+	self = [super init];
+	if (!self) return self;
+
+	isInitialized = 1;
+	return self;
+}
+
+-retain
+{
+	if (!isInitialized) {
+		numUninitialized ++;
+	}
+	return [super retain];
+}
+
+-(void)release
+{
+	if (!isInitialized) {
+		numUninitialized ++;
+	}
+	[super release];
+}
+
+-(id)autorelease
+{
+	if (!isInitialized) {
+		numUninitialized ++;
+	}
+	return [super autorelease];
+}
+
+-(int)isInitialized
+{
+	return isInitialized;
+}
+
++(int)numUninitialized
+{
+	return numUninitialized;
+}
+
+-(id)dummy
+{
+	return @"hello";
+}
+
++(id)makeInstance
+{
+	return [[[self alloc] init] autorelease];
+}
+
+@end
+
+
+static PyMethodDef initialize_methods[] = {
+	{ NULL, NULL, NULL, NULL }
+};
+
+void initinitialize(void);
+void initinitialize(void)
+{
+	PyObject* m;
+
+	m = Py_InitModule4("initialize", initialize_methods, 
+			NULL, NULL, PYTHON_API_VERSION);
+
+	PyObjC_ImportAPI(m);
+	PyModule_AddObject(m, "OC_TestInitialize", 
+		PyObjCClass_New([OC_TestInitialize class]));
+}