Source

pyobjc / pyobjc-core / PyObjCTest / test_leaks.py

"""
Check if we manage retainCounts correctly.
"""
from PyObjCTools.TestSupport import *
import objc, gc
from PyObjCTest.fnd import *

LeaksDel = 0

class LeaksClass (NSObject):
    def init(self):
        self = super(LeaksClass, self).init()
        return self

    def __del__(self):
        global LeaksDel

        LeaksDel = 1

class SlottedClass (NSObject):
    __slots__ = ('slot1',)

    def init(self):
        NSObject.pyobjc_instanceMethods.init(self)
        self.slot1 = LeaksClass.alloc().init()
        return self

class MemberClass (NSObject):

    def init(self):
        self = NSObject.pyobjc_instanceMethods.init(self)
        self.slot1 = LeaksClass.alloc().init()
        return self

class TestRetains(TestCase):
    def testPyClass(self):

        global LeaksDel

        LeaksDel = 0
        self.assertEqual(LeaksDel, 0)

        o = LeaksClass.alloc().init()
        self.assertIsNotNone(o)
        self.assertEqual(LeaksDel, 0)
        del o
        self.assertEqual(LeaksDel, 1)

    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)

    def testOCClass2(self):
        global LeaksDel

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

        pool = NSAutoreleasePool.alloc().init()
        self.assertIsNotNone(c)
        self.assertEqual(LeaksDel, 0)
        del c
        del pool
        self.assertEqual(LeaksDel, 1)

    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)

    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)



if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.