Ronald Oussoren avatar Ronald Oussoren committed 9e5821b

Add 'callable' attribute to selectors, giving access to the python function
implementing the method. TODO: Check if there are other non-exported attributes
of selectors.

Use this new attribute to implement classAdMethod in Python.

Comments (0)

Files changed (2)

pyobjc/Lib/objc/__init__.py

     pass
 
 del sys
+
+
+def classAddMethod(cls, name, method):
+    """
+    Add a single method to a class. 'name' is the ObjC selector
+    """
+    import types
+
+    if isinstance(method, selector):
+        sel = selector(method.callable, 
+                    selector=name, 
+                    signature=method.signature, 
+                    isClassMethod=method.isClassMethod)
+    else:
+        sel = selector(method, selector=name)
+
+    return classAddMethods(cls, [sel])

pyobjc/Modules/objc/selector.m

 	sel_dealloc(obj);
 }
 
+PyDoc_STRVAR(pysel_get_callable_doc, 
+"Returns the python 'function' that implements this method.\n"
+"\n"
+);
+static PyObject*
+pysel_get_callable(ObjCPythonSelector* self, void* closure __attribute__((__unused__)))
+{
+	Py_INCREF(self->callable);
+	return self->callable;
+}
+
+static PyGetSetDef pysel_getset[] = {
+	{
+		"callable",
+		(getter)pysel_get_callable,
+		(setter)NULL,
+		pysel_get_callable_doc,
+		0
+	},
+	{
+		NULL,
+		NULL,
+		NULL,
+		NULL,
+		0
+	}
+};
+
 PyTypeObject ObjCPythonSelector_Type = {
 	PyObject_HEAD_INIT(&PyType_Type)
 	0,					/* ob_size */
 	0,					/* tp_iternext */
 	0,					/* tp_methods */
 	0,					/* tp_members */
-	0,					/* tp_getset */
+	pysel_getset,				/* tp_getset */
 	&PyObjCSelector_Type,			/* tp_base */
 	0,					/* tp_dict */
 	(descrgetfunc)pysel_descr_get,		/* tp_descr_get */
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.