Commits

Ronald Oussoren committed f469eb2

python3.x getattr doesn't like bytes as second argument

  • Participants
  • Parent commits 40780e1
  • Tags pyobjc-2.5

Comments (0)

Files changed (2)

File pyobjc-framework-Cocoa/Lib/Foundation/_nsobject.py

 import sys
 
 if sys.version_info[0] == 2:
+    def _str(v): return v
     exec("""\
 def _raise(exc_type, exc_value, exc_trace):
     raise exc_type, exc_value, exc_trace
 """)
 else:
+    def _str(v): 
+        if isinstance(v, str):
+            return v
+        return v.decode('ascii')
+
     def _raise(exc_type, exc_value, exc_trace):
         raise exc_type(exc_value).with_traceback(exc_trace)
 
+
 NSObject = objc.lookUpClass('NSObject')
 
 class NSObject (objc.Category(NSObject)):
             # XXX: PyObjC's methodForSelector implementation doesn't work
             # with Python methods, using getattr instead
             #m = self.methodForSelector_(sel)
-            m = getattr(self, sel)
+            m = getattr(self, _str(sel))
             m(arg)
         except:
             import traceback
         try:
             sel, arg, result = callinfo
             #m = self.methodForSelector_(sel)
-            m = getattr(self, sel)
+            m = getattr(self, _str(sel))
             r = m(arg)
             result.append((True, r))
         except:

File pyobjc-framework-Cocoa/PyObjCTest/test_nsobject_additions.py

 
 if sys.version_info[0] == 2:
     from StringIO import StringIO
+    def _str(v): return v
 else:
     from io import StringIO
+    def _str(v): 
+        if isinstance(v, str):
+            return v
+        return v.decode('ascii')
 
 class TheadingHelperTestHelper (Foundation.NSObject):
     def init(self):
         assert isinstance(wait, bool)
 
         self.calls.append((selector, thread, object, wait))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def performSelector_onThread_withObject_waitUntilDone_modes_(self,
             selector, thread, object, wait, modes):
         assert isinstance(wait, bool)
 
         self.calls.append((selector, thread, object, wait, modes))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def performSelector_withObject_afterDelay_(self,
             selector, object, delay):
         assert isinstance(selector, bytes)
 
         self.calls.append((selector, object, delay))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def performSelector_withObject_afterDelay_inModes_(self,
             selector, object, delay, modes):
         assert isinstance(selector, bytes)
 
         self.calls.append((selector, object, delay, modes))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def performSelectorInBackground_withObject_(self,
             selector, object):
         self.calls.append((selector, object))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def performSelectorOnMainThread_withObject_waitUntilDone_(self,
             selector, object, wait):
         self.calls.append((selector, object, wait))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def performSelectorOnMainThread_withObject_waitUntilDone_modes_(self,
             selector, object, wait, modes):
         self.calls.append((selector, object, wait, modes))
-        getattr(self, selector)(object)
+        getattr(self, _str(selector))(object)
 
     def sel1_(self, arg):
         pass
         self.assertEqual(r, 0.5)
 
         self.assertEqual(obj.calls, [
-            (b'_pyobjc_performOnThreadWithResult:', (b'sel2:', 3, [(True, 6)]), True),
-            (b'_pyobjc_performOnThreadWithResult:', (b'sel3:', 2.0, [(True, 0.5)]), True),
+            (b'_pyobjc_performOnThreadWithResult:', ('sel2:', 3, [(True, 6)]), True),
+            (b'_pyobjc_performOnThreadWithResult:', ('sel3:', 2.0, [(True, 0.5)]), True),
         ])
 
         # Raise an exception
         self.assertEqual(r, 0.5)
 
         self.assertEqual(obj.calls, [
-            (b'_pyobjc_performOnThreadWithResult:', (b'sel2:', 3, [(True, 6)]), True, 1),
-            (b'_pyobjc_performOnThreadWithResult:', (b'sel3:', 2.0, [(True, 0.5)]), True, 2),
+            (b'_pyobjc_performOnThreadWithResult:', ('sel2:', 3, [(True, 6)]), True, 1),
+            (b'_pyobjc_performOnThreadWithResult:', ('sel3:', 2.0, [(True, 0.5)]), True, 2),
         ])
 
         # Raise an exception
         self.assertEqual(r, 0.5)
 
         self.assertEqual(obj.calls, [
-            (b'_pyobjc_performOnThreadWithResult:', thr, (b'sel2:', 3, [(True, 6)]), True),
-            (b'_pyobjc_performOnThreadWithResult:', thr, (b'sel3:', 2.0, [(True, 0.5)]), True),
+            (b'_pyobjc_performOnThreadWithResult:', thr, ('sel2:', 3, [(True, 6)]), True),
+            (b'_pyobjc_performOnThreadWithResult:', thr, ('sel3:', 2.0, [(True, 0.5)]), True),
         ])
 
         # Raise an exception
         self.assertEqual(r, 0.5)
 
         self.assertEqual(obj.calls, [
-            (b'_pyobjc_performOnThreadWithResult:', thr, (b'sel2:', 3, [(True, 6)]), True, 1),
-            (b'_pyobjc_performOnThreadWithResult:', thr, (b'sel3:', 2.0, [(True, 0.5)]), True, 2),
+            (b'_pyobjc_performOnThreadWithResult:', thr, ('sel2:', 3, [(True, 6)]), True, 1),
+            (b'_pyobjc_performOnThreadWithResult:', thr, ('sel3:', 2.0, [(True, 0.5)]), True, 2),
         ])
 
         # Raise an exception