1import Foundation, objc
2import AppKit
3from Foundation import NSLog, NSAutoreleasePool, NSObject
4from threading import Thread
5import os
6from PyObjCTools.TestSupport import *
7
8class TestRegr (TestCase):
9    def testFSRepr(self):
10        fm = Foundation.NSFileManager.defaultManager()
11        self.assertRaises(TypeError, fm.stringWithFileSystemRepresentation_length_, b"/var")
12        self.assertEqual(b"/var".decode('ascii'), fm.stringWithFileSystemRepresentation_length_(b"/var/boo", 4))
13
14    def testThreadHang(self):
15
16        # Temporarily redirect stderr to a file, this allows us to check
17        # that NSLog actually wrote some text.
18        fp = os.open('/tmp/pyobjc-thread.txt', os.O_RDWR|os.O_CREAT, 0o666)
19        dupped = os.dup(2)
20        os.dup2(fp, 2)
21
22        try:
23
24            class ThreadHangObject(NSObject):
25                def init(self):
26                    self.t = MyThread()
27                    self.t.start()
28                    return self
29
30            aList = []
31            class MyThread(Thread):
32                def run(self):
33                    pool = NSAutoreleasePool.alloc().init()
34                    aList.append("before")
35                    NSLog(b"does this print?".decode('ascii'))
36                    aList.append("after")
37
38            o = ThreadHangObject.alloc().init()
39            o.t.join()
40
41
42        finally:
43            os.close(fp)
44            os.dup2(dupped, 2)
45
46        with open('/tmp/pyobjc-thread.txt', 'r') as fp:
47            data = fp.read()
48        self.assertTrue('does this print?' in data)
49
50        self.assertEqual(aList, ["before", "after"])
51
52    def testMemoryInit(self):
53        """
54        Regression test for bug #814683, that didn't initialize the memory
55        space for output parameters correctly.
56        """
57        if not hasattr(Foundation, 'NSPropertyListSerialization'): return
58
59        plist = 0
60
61        r = Foundation.NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(plist, Foundation.NSPropertyListXMLFormat_v1_0, None)
62        self.assertEqual(r[1], None)
63        r = Foundation.NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(plist, Foundation.NSPropertyListXMLFormat_v1_0, None)
64        self.assertEqual(r[1], None)
65
66    def testTypeOverrideProblem(self):
67        """
68        A bug in the medatata machinery caused a crash.
69        """
70        cls = AppKit.NSOpenGLPixelFormat
71        dir (cls)
72
73        o = cls.alloc().initWithAttributes_(
74                (AppKit.NSOpenGLPFAAccelerated,
75                AppKit.NSOpenGLPFANoRecovery, AppKit.NSOpenGLPFAColorSize, 32))
76
77if __name__ == "__main__":
78    main()
79