1. Ronald Oussoren
  2. pyobjc

Commits

Ronald Oussoren  committed 66eb3d4

Some small changes for python3 on OSX 10.5

The new tests in test3_protocols are ported from test_protocols and show a bug in the python3 protocol implementation

  • Participants
  • Parent commits 29b6447
  • Branches default

Comments (0)

Files changed (5)

File pyobjc-core/PyObjCTest/test3_protocol.py

View file
 # Most useful systems will at least have 'NSObject'.
 NSObject = objc.lookUpClass('NSObject')
 
+
+# XXX : This is a really dumb way to detect < 10.3
+if not NSObject.instancesRespondToSelector_('setValue:forKey:'):
+    # Defining protocols in an MH_BUNDLE makes < 10.3 explode
+    OC_TestProtocol = None
+else:
+    from PyObjCTest.protocol import OC_TestProtocol
+
+
 MyProto = objc.informal_protocol("MyProto", (
     objc.selector(None, selector=b"testMethod", signature=b"I@:", isRequired=1),
     objc.selector(None, selector=b"testMethod2:", signature=b"v@:i", isRequired=0)
 
 
 
+if sys.maxsize < 2 ** 32:
+    EmptyProtocol = objc.formal_protocol("EmptyProtocol", None, ())
+
+    MyProtocol = objc.formal_protocol("MyProtocol", None, (
+        objc.selector(None, selector=b"protoMethod", signature=b"I@:"),
+        objc.selector(None, selector=b"anotherProto:with:", signature=b"v@:ii"),
+    ))
+
+    MyOtherProtocol = objc.formal_protocol("MyOtherProtocol", 
+            (MyProtocol,), [
+                objc.selector(None, selector=b"yetAnother:", signature=b"i@:I")
+            ])
+
+    MyClassProtocol = objc.formal_protocol("MyClassProtocol", None, [
+        objc.selector(None, selector=b"anAnotherOne:", signature=b"i@:i"),
+        objc.selector(None, selector=b"aClassOne:", signature=b"@@:i", isClassMethod=1),
+    ])
+
+    if OC_TestProtocol is not None:
+
+        class TestFormalOCProtocols(TestCase):
+            
+            def testImplementFormalProtocol(self):
+
+                class MyClassNotImplementingProtocol(NSObject):
+                    pass
+
+                self.assertFalse(MyClassNotImplementingProtocol.pyobjc_classMethods.conformsToProtocol_(OC_TestProtocol))
+
+                try:
+                    class MyClassNotAlsoImplementingProtocol(NSObject, protocols=[OC_TestProtocol]):
+                        def method1(self): pass
+
+                    self.fail("class not implementing protocol, yet created")
+                except TypeError:
+                    pass
+
+                class MyClassImplementingProtocol(NSObject, protocols=[OC_TestProtocol]):
+                    def method1(self): pass
+                    def method2_(self, a): pass
+
+                self.assertTrue(MyClassImplementingProtocol.pyobjc_classMethods.conformsToProtocol_(OC_TestProtocol))
+
+
+
+                # The PyObjC implementation of formal protocols is slightly looser
+                # than Objective-C itself: you can inherit part of the protocol
+                # from the superclass.
+                # XXX: not really: you won't inherit the right signatures by default
+
+                class MyClassImplementingHalfOfProtocol(NSObject):
+                        def method1(self): pass
+                        method1 = objc.selector(method1, signature=b'i@:')
+
+                self.assertFalse(MyClassImplementingHalfOfProtocol.pyobjc_classMethods.conformsToProtocol_(OC_TestProtocol))
+
+                class MyClassImplementingAllOfProtocol(MyClassImplementingHalfOfProtocol, OC_TestProtocol):
+                        def method2_(self, v): pass
+
+                self.assertTrue(MyClassImplementingAllOfProtocol.pyobjc_classMethods.conformsToProtocol_(OC_TestProtocol))
+
+
+
+
+    class TestFormalProtocols (TestCase):
+        # Implement unittests for formal protocols here.
+        #
+
+        def testImplementAnotherObject(self):
+            anObject = NSObject.alloc().init()
+
+            try:
+                class MyClassImplementingAnotherObject(NSObject, protocols=[anObject]):
+                        pass
+                self.fail()
+            except TypeError: 
+                pass
+
+            try:
+                class MyClassImplementingAnotherObject(NSObject, protocols=[10]):
+                        pass
+                self.fail()
+            except TypeError: 
+                pass
+
+            try:
+                class MyClassImplementingAnotherObject(NSObject, protocols=[int]):
+                        pass
+                self.fail()
+            except TypeError: 
+                pass
+
+
+
+        def testIncorrectlyDefiningFormalProtocols(self):
+            # Some bad calls to objc.formal_protocol
+            self.assertRaises(TypeError, objc.formal_protocol, [], None, ())
+            self.assertRaises(TypeError, objc.formal_protocol, 'supers', (NSObject,) , ())
+            self.assertRaises(TypeError, objc.formal_protocol, 'supers', objc.protocolNamed('NSLocking') , ())
+            self.assertRaises(TypeError, objc.formal_protocol, 'supers', [
+                    objc.protocolNamed('NSLocking'),
+                    "hello",
+                ], ())
+            self.assertRaises(TypeError, objc.formal_protocol, 'supers', None, [
+                    objc.selector(None, selector=b'fooMethod:', signature=b'v@:i'),
+                    "hello",
+                ])
+
+        def testMethodInfo(self):
+            self.assertEqual(
+                    MyProtocol.descriptionForInstanceMethod_("protoMethod"),
+                        ("protoMethod", "I@:"))
+
+            self.assertEqual(
+                    MyProtocol.descriptionForInstanceMethod_("nosuchmethod"),
+                        None)
+
+            self.assertEqual(
+                    MyClassProtocol.descriptionForClassMethod_("aClassOne:"),
+                        ("aClassOne:", "@@:i"))
+
+            self.assertEqual(
+                    MyClassProtocol.descriptionForClassMethod_("nosuchmethod"),
+                        None)
+
+
 if __name__ == '__main__':
     main()

File pyobjc-core/PyObjCTest/test_archive_python.py

View file
         @onlyIf(0, "python unittest not relevant for archiving")
         def test_int_pickling_efficiency(self): pass
 
+        @onlyIf(0, "python unittest not relevant for archiving")
+        def test_negative_32b_binunicode(self): pass
+
+        @onlyIf(0, "python unittest not relevant for archiving")
+        def test_negative_32b_binput(self): pass
+
+        @onlyIf(0, "python unittest not relevant for archiving")
+        def test_negative_32b_binbytes(self): pass
+
 
     #
     # Disable testing of plain Archiving for now, need full support

File pyobjc-core/PyObjCTest/test_assocations.py

View file
         return "<copy!>"
 
 class TestAssocations (TestCase):
+
+    @min_os_level("10.6")
     def testKeysAreIdentityBased(self):
         o = NSObject.alloc().init()
 
         self.assertEqual(objc.getAssociatedObject(o, key1), "foo")
         self.assertEqual(objc.getAssociatedObject(o, key2), None)
 
+    @min_os_level("10.6")
     def testGetSet(self):
         o = NSObject.alloc().init()
 
         objc.setAssociatedObject(o, key1, None)
         self.assertEqual(objc.getAssociatedObject(o, key1), None)
 
+    @min_os_level("10.6")
     def testClearing(self):
 
         o = NSObject.alloc().init()
         self.assertEqual(objc.getAssociatedObject(o, key1), None)
         self.assertEqual(objc.getAssociatedObject(o, key2), None)
 
+    @min_os_level("10.6")
     def testPolicy(self):
         o = NSObject.alloc().init()
         key1 = "key1"

File pyobjc-core/PyObjCTest/test_pickle.py

View file
 except ImportError:
     cPickle = None
 
+try:
+    long
+except NameError:
+    # Python 3.x
+    long = int
+
 class TestPickleNumber (TestCase):
 
     def testPickleInt(self):

File pyobjc-core/PyObjCTest/test_protocol.py

View file
         self.assertRaises(TypeError, self.doIncompleteClass)
 
 
-    @onlyIf(sys.version_info[:2] < (3,3), "not valid for python 3.3 and later")
+    @onlyIf(sys.version_info[:2] < (3,2), "not valid for python 3.3 and later")
     def testOptional(self):
         class ProtoClass3 (NSObject, MyProto):
             def testMethod(self):
 
 
 
-if sys.maxsize < 2 ** 32:
+if sys.maxsize < 2 ** 32 and sys.version_info[0] == 2:
     EmptyProtocol = objc.formal_protocol("EmptyProtocol", None, ())
 
     MyProtocol = objc.formal_protocol("MyProtocol", None, (