Exemplo n.º 1
0
    def testOCClass1(self):
        global LeaksDel

        LeaksDel = 0
        self.assertEqual(LeaksDel, 0)
        pool = NSAutoreleasePool.alloc().init()
        c = NSMutableArray.arrayWithArray_([LeaksClass.alloc().init()])
        del pool

        pool = NSAutoreleasePool.alloc().init()
        self.assertIsNotNone(c)
        self.assertEqual(LeaksDel, 0)
        del c
        del pool
        self.assertEqual(LeaksDel, 1)
Exemplo n.º 2
0
    def test_weakref_to_objc(self):

        pool = NSAutoreleasePool.alloc().init()

        o = NSObject.alloc().init()
        a = NSArray.arrayWithObject_(o)

        r = objc.WeakRef(o)
        self.assertIs(r(), o)

        del o
        del pool
        pool = NSAutoreleasePool.alloc().init()

        self.assertIsInstance(r(), NSObject)
        del a

        del pool
        self.assertIs(r(), None)
Exemplo n.º 3
0
    def test_weakref_to_objc(self):

        pool = NSAutoreleasePool.alloc().init()

        o = NSObject.alloc().init()
        a = NSArray.arrayWithObject_(o)

        r = objc.WeakRef(o)
        self.assertIs(r(), o)

        del o
        del pool
        pool = NSAutoreleasePool.alloc().init()

        self.assertIsInstance(r(), NSObject)
        del a

        del pool
        self.assertIs(r(), None)
Exemplo n.º 4
0
    def testSlots(self):
        global LeaksDel

        LeaksDel = 0
        pool = NSAutoreleasePool.alloc().init()

        o = SlottedClass.alloc().init()
        self.assertEqual(LeaksDel, 0)
        del o
        del pool
        self.assertEqual(LeaksDel, 1)
Exemplo n.º 5
0
    def testMembers(self):
        global LeaksDel

        LeaksDel = 0
        pool = NSAutoreleasePool.alloc().init()

        o = MemberClass.alloc().init()
        self.assertEqual(LeaksDel, 0)
        del o
        del pool
        gc.collect()
        self.assertEqual(LeaksDel, 1)
Exemplo n.º 6
0
    def testWithPythonInit(self):
        global gDeallocCounter

        pool = NSAutoreleasePool.alloc().init()
        try:
            v = copying.OC_CopyHelper.newObjectOfClass_(OC_LeakTest_20090704_init)
            self.assertIsInstance(v, OC_LeakTest_20090704_init)

            gDeallocCounter = 0
            del v

        finally:
            del pool

        self.assertNotEqual(gDeallocCounter, 0)
Exemplo n.º 7
0
    def testNoPythonInit(self):
        # This test is basicly a self-test of the test-case, the
        # test even passed before the regression was fixed.

        global gDeallocCounter

        pool = NSAutoreleasePool.alloc().init()
        try:
            v = copying.OC_CopyHelper.newObjectOfClass_(OC_LeakTest_20090704_noinit)
            self.assertIsInstance(v, OC_LeakTest_20090704_noinit)

            gDeallocCounter = 0
            del v

        finally:
            del pool

        self.assertNotEqual(gDeallocCounter, 0)
Exemplo n.º 8
0
        def testObserving(self):
            outer_pool = NSAutoreleasePool.alloc().init()  # noqa: F841
            observer = PyObjCTestObserver.alloc().init()

            o = PyObjCTestObserved1.alloc().init()

            self.assertEqual(o.bar(), None)
            o.setBar_("hello")
            self.assertEqual(o.bar(), "hello")

            # See below
            PyObjCTestObserved1.FOOBASE = "base3"
            try:
                o.setFoo_("yyy")
                self.assertEqual(o.foo(), "base3yyy")
            finally:
                PyObjCTestObserved1.FOOBASE = "base"

            # XXX: To be debugged, when flags == 0 everything is fine,
            # otherwise we leak a reference
            o.addObserver_forKeyPath_options_context_(
                observer,
                "bar",
                (NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld),
                0,
            )
            o.addObserver_forKeyPath_options_context_(
                observer,
                "foo",
                (NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld),
                0,
            )

            try:
                o.setBar_("world")
                self.assertEqual(o.bar(), "world")

                o.setFoo_("xxx")
                self.assertEqual(o.foo(), "basexxx")

                # Change a "class" attribute, and make sure the object sees
                # that change (e.g. the fact that Cocoa changes the ISA pointer
                # should be mostly invisible)
                PyObjCTestObserved1.FOOBASE = "base2"

                o.setFoo_("yyy")
                self.assertEqual(o.foo(), "base2yyy")

            finally:
                o.removeObserver_forKeyPath_(observer, "bar")
                o.removeObserver_forKeyPath_(observer, "foo")
                PyObjCTestObserved1.FOOBASE = "base"

            self.assertEqual(len(observer.observed), 3)

            self.assertEqual(
                observer.observed[0],
                ("bar", o, {"kind": 1, "new": "world", "old": "hello"}, 0),
            )
            self.assertEqual(
                observer.observed[1],
                ("foo", o, {"kind": 1, "new": "basexxx", "old": "base3yyy"}, 0),
            )
            self.assertEqual(
                observer.observed[2],
                ("foo", o, {"kind": 1, "new": "base2yyy", "old": "basexxx"}, 0),
            )
            self.assertEqual(o.bar(), "world")

            del observer

            before = DEALLOCS
            del o
            del outer_pool
            self.assertEqual(DEALLOCS, before + 1, "Leaking an observed object")