Ronald Oussoren avatar Ronald Oussoren committed 238bdcd

Real fix for bug 1146224, including some unittests

Comments (0)

Files changed (3)

Lib/objc/test/test_varargs.py

+import objc
+import unittest
+
+NSObject = objc.lookUpClass("NSObject")
+
+class VarargsMethod (unittest.TestCase):
+    def testVariableArgumentCount(self):
+        class VarArgsClass1 (NSObject):
+            def instanceMethod1_(self, arg1, *args):
+                arg1.append(args)
+
+            def classMethod1_(cls, arg1, *args):
+                arg1.append(args)
+            classMethod1_ = classmethod(classMethod1_)
+
+            def instanceMethod2_(self, *args):
+                args[0].append(args[1:])
+
+            def classMethod2_(cls, *args):
+                args[0].append(args[1:])
+            classMethod2_ = classmethod(classMethod2_)
+
+        o = VarArgsClass1.alloc().init()
+        l = []
+        o.instanceMethod1_(l, 1, 2, 3)
+        self.assertEquals(l, [(1,2,3)])
+
+        l = []
+        VarArgsClass1.classMethod1_(l, 3, 4, 5)
+        self.assertEquals(l, [(3,4,5)])
+
+        l = []
+        o.instanceMethod2_(l, 1, 2, 3)
+        self.assertEquals(l, [(1,2,3)])
+
+        l = []
+        VarArgsClass1.classMethod2_(l, 3, 4, 5)
+        self.assertEquals(l, [(3,4,5)])
+
+    def testKeywordArguments(self):
+        class VarArgsClass2 (NSObject):
+            def instanceMethod1_(self, arg1, **kwds):
+                arg1.append(kwds)
+
+            def classMethod1_(cls, arg1, **kwds):
+                arg1.append(kwds)
+            classMethod1_ = classmethod(classMethod1_)
+
+
+        o = VarArgsClass2.alloc().init()
+        l = []
+        o.instanceMethod1_(l, a=1, c=2)
+        self.assertEquals(l, [{'a':1 ,'c':2}])
+
+        l = []
+        VarArgsClass2.classMethod1_(l, foo='bar', baz='foo')
+        self.assertEquals(l, [{'foo':'bar', 'baz':'foo'}])
+
+if __name__ == "__main__":
+    unittest.main()

Modules/objc/objc-class.m

 					continue;
 				} else if (!(((PyObjCSelector*)descr)->sel_flags & PyObjCSelector_kCLASS_METHOD)) {
 					continue;
+				} else if (PyObjCPythonSelector_Check(descr)) {
+					continue;
 				}
 			}
 

Modules/objc/selector.m

 		}
 	}
 
-	if (!PyFunction_Check(callable) && !PyMethod_Check(callable)) {
-		PyErr_SetString(PyExc_TypeError, 
-				"expecting function or method");
-		return NULL;
-	} else {
-		PyCodeObject* c = NULL;
-		if (PyFunction_Check(callable)) {
-			c = (PyCodeObject*)PyFunction_GetCode(callable);
-		} else {
-			PyObject* func;
-
-			func = PyMethod_Function(callable);
-			if (PyFunction_Check(func)) {
-				c = (PyCodeObject*)PyFunction_GetCode(func);
-			}
-		}
-
-
-		if (c != NULL && (c->co_flags & CO_VARARGS)) {
-			PyErr_SetString(PyExc_TypeError,
-				"Using function with *args as an objc method");
-			return NULL;
-		}
-		if (c != NULL && (c->co_flags & CO_VARKEYWORDS)) {
-			PyErr_SetString(PyExc_TypeError,
-				"Using function with **args as an objc method");
-			return NULL;
-		}
-	} 
-
-
-
 	if (pyname == NULL) {
 		/* No name specified, use the function name */
 		pyname = PyObject_GetAttrString(callable, "__name__");
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.