1/*
2 * Implementation of 'native' and 'python' selectors
3 *
4 * TODO:
5 * - Maybe it is better to fold the three types into one, especially because
6 *   only one of them is exposed to python code.
7 */
8#include "pyobjc.h"
9
10#include "compile.h" /* from Python */
11#include "opcode.h"
12
13#include <objc/Object.h>
14
15/*
16 * First section deals with registering replacement signatures for methods.
17 * This is meant to be used to add _C_IN, _C_OUT and _C_INOUT specifiers for
18 * pass-by-reference parameters.
19 *
20 * We register class names because the actual class may not yet be available.
21 * The list of replacements is not sorted in any way, it is expected to be
22 * short and the list won't be checked very often.\
23 *
24 * Alternative implementation: { SEL: [ (class_name, signature), ... ], ... }
25 */
26static PyObject* replacement_signatures = NULL;
27
28int
29ObjC_SignatureForSelector(char* class_name, SEL selector, char* signature)
30{
31	PyObject* replacement;
32	PyObject* name_str;
33	PyObject* selector_str;;
34	int r;
35
36	if (replacement_signatures == NULL) {
37		replacement_signatures = PyObjC_NewRegistry();
38		if (replacement_signatures == NULL) {
39			return -1;
40		}
41	}
42
43	replacement = PyBytes_FromString(signature);
44	if (replacement == NULL)  {
45		return -1;
46	}
47
48	name_str = PyText_FromString(class_name);
49	if (name_str == NULL)  {
50		Py_DECREF(replacement);
51		return -1;
52	}
53
54	selector_str = PyText_FromString(sel_getName(selector));
55	if (name_str == NULL)  {
56		Py_DECREF(replacement);
57		Py_DECREF(name_str);
58		return -1;
59	}
60
61	r = PyObjC_AddToRegistry(replacement_signatures, name_str, selector_str,
62			replacement);
63
64	Py_DECREF(replacement);
65	Py_DECREF(name_str);
66	Py_DECREF(selector_str);
67
68	return r;
69}
70
71static char*
72PyObjC_FindReplacementSignature(Class cls, SEL selector)
73{
74	PyObject* replacement;
75	char* result;
76
77
78	if (replacement_signatures == NULL) {
79		return NULL;
80	}
81
82	replacement = PyObjC_FindInRegistry(
83			replacement_signatures, cls, selector);
84	if (replacement == NULL) {
85		return NULL;
86	}
87
88	result = PyBytes_AsString(replacement);
89	Py_DECREF(replacement);
90
91	return result;
92}
93
94static char* pysel_default_signature(PyObject* callable);
95
96/* Need to check instance-method implementation! */
97/* Maybe we can subclass 'PyMethod_Type' */
98
99/*
100 * Base type for objective-C selectors
101 *
102 * selectors are callable objects with the following attributes:
103 * - 'signature': The objective-C signature of the method
104 * - 'selector':  The name in the objective-C runtime
105 */
106
107PyObjCMethodSignature*
108PyObjCSelector_GetMetadata(PyObject* _self)
109{
110	PyObjCSelector* self = (PyObjCSelector*)_self;
111
112	if (self->sel_methinfo == NULL) {
113		self->sel_methinfo = PyObjCMethodSignature_ForSelector(
114			self->sel_class,
115			(self->sel_flags & PyObjCSelector_kCLASS_METHOD) != 0,
116			self->sel_selector,
117			self->sel_python_signature,
118			PyObjCNativeSelector_Check(self)
119			);
120		if (self->sel_methinfo == NULL) return NULL;
121
122		if (PyObjCPythonSelector_Check(_self)) {
123			Py_ssize_t i;
124
125			((PyObjCPythonSelector*)_self)->numoutput = 0;
126			for (i = 0; i < Py_SIZE(((PyObjCPythonSelector*)_self)->sel_methinfo); i++) {
127				if (((PyObjCPythonSelector*)_self)->sel_methinfo->argtype[i].type[0] == _C_OUT) {
128					((PyObjCPythonSelector*)_self)->numoutput ++;
129				}
130			}
131		}
132	}
133	return self->sel_methinfo;
134}
135
136
137PyDoc_STRVAR(sel_metadata_doc, "Return a dict that describes the metadata for this method, including metadata for the 2 hidden ObjC parameters (self and _sel) ");
138static PyObject* sel_metadata(PyObject* self)
139{
140	PyObject* result = PyObjCMethodSignature_AsDict(PyObjCSelector_GetMetadata(self));
141	int r;
142
143	if (((PyObjCSelector*)self)->sel_flags & PyObjCSelector_kCLASS_METHOD) {
144		r = PyDict_SetItemString(result, "classmethod", Py_True);
145	} else {
146		r = PyDict_SetItemString(result, "classmethod", Py_False);
147	}
148	if (r == -1) {
149		Py_DECREF(result);
150		return NULL;
151	}
152
153	if (((PyObjCSelector*)self)->sel_flags & PyObjCSelector_kHIDDEN) {
154		r = PyDict_SetItemString(result, "hidden", Py_True);
155	} else {
156		r = PyDict_SetItemString(result, "hidden", Py_False);
157	}
158	if (r == -1) {
159		Py_DECREF(result);
160		return NULL;
161	}
162
163	if (((PyObjCSelector*)self)->sel_flags & PyObjCSelector_kRETURNS_UNINITIALIZED) {
164		r = PyDict_SetItemString(result, "return_uninitialized_object", Py_True);
165		if (r == -1) {
166			Py_DECREF(result);
167			return NULL;
168		}
169	}
170	return result;
171}
172
173static PyMethodDef sel_methods[] = {
174	{
175		  "__metadata__",
176	    	  (PyCFunction)sel_metadata,
177	          METH_NOARGS,
178		  sel_metadata_doc
179	},
180	{ 0, 0, 0, 0 }
181};
182
183static PyObject*
184pysel_new(PyTypeObject* type, PyObject* args, PyObject* kwds);
185
186PyDoc_STRVAR(base_self_doc, "'self' object for bound methods, None otherwise");
187static PyObject*
188base_self(PyObject* _self, void* closure __attribute__((__unused__)))
189{
190	PyObjCSelector* self = (PyObjCSelector*)_self;
191	if (self->sel_self) {
192		Py_INCREF(self->sel_self);
193		return self->sel_self;
194	} else {
195		Py_INCREF(Py_None);
196		return Py_None;
197	}
198}
199
200PyDoc_STRVAR(base_signature_doc, "Objective-C signature for the method");
201static PyObject*
202base_signature(PyObject* _self, void* closure __attribute__((__unused__)))
203{
204	PyObjCSelector* self = (PyObjCSelector*)_self;
205	return PyBytes_FromString(self->sel_python_signature);
206}
207
208
209PyDoc_STRVAR(base_native_signature_doc, "original Objective-C signature for the method");
210static PyObject*
211base_native_signature(PyObject* _self, void* closure __attribute__((__unused__)))
212{
213	PyObjCSelector* self = (PyObjCSelector*)_self;
214	if (self->sel_native_signature == NULL) {
215		Py_INCREF(Py_None);
216		return Py_None;
217	}
218
219	return PyBytes_FromString(self->sel_native_signature);
220}
221
222static int
223base_signature_setter(PyObject* _self, PyObject* newVal, void* closure __attribute__((__unused__)))
224{
225	PyObjCNativeSelector* self = (PyObjCNativeSelector*)_self;
226	char* t;
227	if (!PyBytes_Check(newVal)) {
228		PyErr_SetString(PyExc_TypeError, "signature must be byte string");
229		return -1;
230	}
231
232	t = PyObjCUtil_Strdup(PyBytes_AsString(newVal));
233	if (t == NULL) {
234		PyErr_NoMemory();
235		return -1;
236	}
237
238	PyMem_Free((char*)self->sel_python_signature);
239	self->sel_python_signature = t;
240	return 0;
241}
242
243PyDoc_STRVAR(base_hidden_doc, "If True the method is not directly accessible as an object attribute");
244static PyObject*
245base_hidden(PyObject* _self, void* closure __attribute__((__unused__)))
246{
247	return PyBool_FromLong(((PyObjCSelector*)_self)->sel_flags & PyObjCSelector_kHIDDEN);
248}
249static int
250base_hidden_setter(PyObject* _self, PyObject* newVal, void* closure __attribute__((__unused__)))
251{
252	if (PyObject_IsTrue(newVal)) {
253		((PyObjCSelector*)_self)->sel_flags |= PyObjCSelector_kHIDDEN;
254	} else {
255		((PyObjCSelector*)_self)->sel_flags &= ~PyObjCSelector_kHIDDEN;
256	}
257	return 0;
258}
259
260PyDoc_STRVAR(base_selector_doc, "Objective-C name for the method");
261static PyObject*
262base_selector(PyObject* _self, void* closure __attribute__((__unused__)))
263{
264	PyObjCSelector* self = (PyObjCSelector*)_self;
265	return PyBytes_FromString(sel_getName(self->sel_selector));
266}
267
268PyDoc_STRVAR(base_class_doc, "Objective-C Class that defines the method");
269static PyObject*
270base_class(PyObject* _self, void* closure __attribute__((__unused__)))
271{
272	PyObjCNativeSelector* self = (PyObjCNativeSelector*)_self;
273	if (self->sel_class != nil) {
274		return PyObjCClass_New(self->sel_class);
275	}
276	Py_INCREF(Py_None);
277	return Py_None;
278}
279
280PyDoc_STRVAR(base_class_method_doc,
281	"True if this is a class method, False otherwise");
282static PyObject*
283base_class_method(PyObject* _self, void* closure __attribute__((__unused__)))
284{
285	PyObjCNativeSelector* self = (PyObjCNativeSelector*)_self;
286	return PyBool_FromLong(0 != (self->sel_flags & PyObjCSelector_kCLASS_METHOD));
287}
288
289PyDoc_STRVAR(base_required_doc,
290	"True if this is a required method, False otherwise");
291static PyObject*
292base_required(PyObject* _self, void* closure __attribute__((__unused__)))
293{
294	PyObjCNativeSelector* self = (PyObjCNativeSelector*)_self;
295	return PyBool_FromLong(0 != (self->sel_flags & PyObjCSelector_kREQUIRED));
296}
297
298
299
300
301static PyGetSetDef base_getset[] = {
302	{
303		"isHidden",
304		base_hidden,
305		base_hidden_setter,
306		base_hidden_doc,
307		0
308	},
309	{
310		"isRequired",
311		base_required,
312		0,
313		base_required_doc,
314		0
315	},
316	{
317		"isClassMethod",
318		base_class_method,
319		0,
320		base_class_method_doc,
321		0
322	},
323	{
324		"definingClass",
325		base_class,
326		0,
327		base_class_doc,
328		0
329	},
330	{
331		"signature",
332		base_signature,
333		base_signature_setter,
334		base_signature_doc,
335		0
336	},
337	{
338		"native_signature",
339		base_native_signature,
340		0,
341		base_native_signature_doc,
342		0
343	},
344	{
345		"self",
346		base_self,
347		0,
348		base_self_doc,
349		0
350	},
351	{
352		"selector",
353		base_selector,
354		0,
355		base_selector_doc,
356		0
357	},
358	{
359		"__name__",
360		base_selector,
361		0,
362		base_selector_doc,
363		0
364	},
365	{ 0, 0, 0, 0, 0 }
366};
367
368
369static void
370sel_dealloc(PyObject* object)
371{
372	PyObjCSelector* self = (PyObjCSelector*)object;
373	Py_XDECREF(self->sel_methinfo);
374	self->sel_methinfo = NULL;
375
376	PyMem_Free((char*)self->sel_python_signature);
377	self->sel_python_signature = NULL;
378
379	if (self->sel_native_signature != NULL) {
380		PyMem_Free((char*)self->sel_native_signature);
381		self->sel_native_signature = NULL;
382	}
383	if (self->sel_self) {
384		Py_DECREF(self->sel_self);
385		self->sel_self = NULL;
386	}
387	Py_TYPE(object)->tp_free(object);
388}
389
390static int
391base_descr_set(
392		PyObject* self __attribute__((__unused__)),
393		PyObject* obj __attribute__((__unused__)), PyObject* value)
394{
395	if (value == NULL) {
396		PyErr_SetString(PyExc_TypeError, "cannot delete a method");
397	} else {
398		/* Dummy, setattr of the class deals with this */
399		PyErr_SetString(PyExc_TypeError, "cannot change a method");
400	}
401	return -1;
402}
403
404
405PyDoc_STRVAR(base_selector_type_doc,
406"selector(function, [, selector] [, signature] [, isClassMethod=0]\n"
407"    [, returnType] [, argumentTypes] [, isRequired=True]) -> selector\n"
408"\n"
409"Return an Objective-C method from a function. The other arguments \n"
410"specify attributes of the Objective-C method.\n"
411"\n"
412"function:\n"
413"  A function object with at least one argument. The first argument will\n"
414"  be used to pass 'self'. This argument may be None when defining an\n"
415"  informal_protocol object. The function must not be a ``staticmethod``\n"
416"  instance. \n"
417"selector:\n"
418"  The name of the Objective-C method. The default value of this\n"
419"  attribute is the name of the function, with all underscores replaced\n"
420"  by colons.\n"
421"signature:\n"
422"  Method signature for the Objective-C method. This should be a raw\n"
423"  Objective-C method signature, including specifications for 'self' and\n"
424"  '_cmd'. The default value a signature that describes a method with\n"
425"  arguments of type 'id' and a return-value of the same type.\n"
426"argumentTypes, returnType:\n"
427"  Alternative method for specifying the method signature. returnType is\n"
428"  the return type and argumentTypes describes the list of arguments. The \n"
429"  returnType is optional and defaults to 'void' (e.g. no return value).\n"
430"  Both are specified using a subset of the Py_BuildValue syntax:\n"
431"  - s, z, S: an NSString (id)\n"
432"  - b: a byte (char)\n"
433"  - h: a short integer (short int)\n"
434"  - i: an integer (int)\n"
435"  - l: a long integer (long int)\n"
436"  - c: a single character (char)\n"
437"  - f: a single precision float (float)\n"
438"  - d: a double precision float (double)\n"
439"  - O: any object (id)\n"
440"  It is not allowed to specify both 'argumentTypes' and 'signature'\n"
441"isClassMethod:\n"
442"  True if the method is a class method, false otherwise. The default is \n"
443"  False, unless the function is an instance of ``classmethod``.\n"
444"isRequired:\n"
445"  True if this is a required method in an informal protocol, False\n"
446"  otherwise. The default value is 'True'. This argument is only used\n"
447"  when defining an 'informal_protocol' object.\n"
448);
449PyTypeObject PyObjCSelector_Type = {
450	PyVarObject_HEAD_INIT(&PyType_Type, 0)
451	"objc.selector",			/* tp_name */
452	sizeof(PyObjCSelector),			/* tp_basicsize */
453	0,					/* tp_itemsize */
454	/* methods */
455	sel_dealloc,	 			/* tp_dealloc */
456	0,					/* tp_print */
457	0,					/* tp_getattr */
458	0,					/* tp_setattr */
459	0,					/* tp_compare */
460	0,					/* tp_repr */
461	0,					/* tp_as_number */
462	0,					/* tp_as_sequence */
463	0,		       			/* tp_as_mapping */
464	0,					/* tp_hash */
465	0,					/* tp_call */
466	0,					/* tp_str */
467	PyObject_GenericGetAttr,		/* tp_getattro */
468	0,					/* tp_setattro */
469	0,					/* tp_as_buffer */
470	Py_TPFLAGS_DEFAULT,			/* tp_flags */
471 	base_selector_type_doc,			/* tp_doc */
472 	0,					/* tp_traverse */
473 	0,					/* tp_clear */
474	0,					/* tp_richcompare */
475	0,					/* tp_weaklistoffset */
476	0,					/* tp_iter */
477	0,					/* tp_iternext */
478	sel_methods,				/* tp_methods */
479	0,					/* tp_members */
480	base_getset,				/* tp_getset */
481	0,					/* tp_base */
482	0,					/* tp_dict */
483	0,					/* tp_descr_get */
484	base_descr_set,				/* tp_descr_set */
485	0,					/* tp_dictoffset */
486	0,					/* tp_init */
487	0,					/* tp_alloc */
488	pysel_new,				/* tp_new */
489	0,		        		/* tp_free */
490	0,					/* tp_is_gc */
491	0,                                      /* tp_bases */
492	0,                                      /* tp_mro */
493	0,                                      /* tp_cache */
494	0,                                      /* tp_subclasses */
495	0,                                      /* tp_weaklist */
496	0                                       /* tp_del */
497#if PY_VERSION_HEX >= 0x02060000
498	, 0                                     /* tp_version_tag */
499#endif
500
501};
502
503
504/*
505 * Selector type for 'native' selectors (that is, selectors that are not
506 * implemented as python methods)
507 */
508static PyObject*
509objcsel_repr(PyObject* _self)
510{
511	PyObjCNativeSelector* sel = (PyObjCNativeSelector*)_self;
512	PyObject *rval;
513	if (sel->sel_self == NULL) {
514		rval = PyText_FromFormat("<unbound native-selector %s in %s>", sel_getName(sel->sel_selector), class_getName(sel->sel_class));
515	} else {
516#if PY_MAJOR_VERSION == 2
517		PyObject* selfrepr = PyObject_Repr(sel->sel_self);
518		if (selfrepr == NULL) {
519			return NULL;
520		}
521		if (!PyString_Check(selfrepr)) {
522			Py_DECREF(selfrepr);
523			return NULL;
524		}
525		rval = PyText_FromFormat("<native-selector %s of %s>", sel_getName(sel->sel_selector), PyString_AS_STRING(selfrepr));
526		Py_DECREF(selfrepr);
527#else
528		rval = PyUnicode_FromFormat("<native-selector %s of %R>", sel_getName(sel->sel_selector),
529					sel->sel_self);
530#endif
531	}
532	return rval;
533}
534
535
536static PyObject*
537objcsel_call(PyObject* _self, PyObject* args, PyObject* kwds)
538{
539	PyObjCNativeSelector* self = (PyObjCNativeSelector*)_self;
540	PyObject* pyself = self->sel_self;
541	PyObjC_CallFunc execute = NULL;
542	PyObject* res;
543	PyObject* pyres;
544
545	if (kwds != NULL && PyObject_Size(kwds) != 0) {
546		PyErr_SetString(PyExc_TypeError,
547		    "Objective-C selectorrs don't support keyword arguments");
548		return NULL;
549	}
550
551	if (pyself == NULL) {
552		int       argslen;
553		argslen = PyTuple_Size(args);
554		if (argslen < 1) {
555			PyErr_SetString(PyExc_TypeError,
556				"Missing argument: self");
557			return NULL;
558		}
559		pyself = PyTuple_GET_ITEM(args, 0);
560		if (pyself == NULL) {
561			return NULL;
562		}
563	}
564
565	if (self->sel_call_func) {
566		execute = self->sel_call_func;
567	} else {
568		execute = PyObjC_FindCallFunc(
569				self->sel_class,
570				self->sel_selector);
571		if (execute == NULL) return NULL;
572		self->sel_call_func = execute;
573	}
574
575	if (self->sel_self != NULL) {
576		pyres = res = execute((PyObject*)self, self->sel_self, args);
577		if (pyres != NULL
578			&& PyTuple_Check(pyres)
579			&& PyTuple_GET_SIZE(pyres) >= 1
580			&& PyTuple_GET_ITEM(pyres, 0) == pyself) {
581			pyres = pyself;
582		}
583
584		if (PyObjCObject_Check(self->sel_self) && (((PyObjCObject*)self->sel_self)->flags & PyObjCObject_kUNINITIALIZED)) {
585			if (self->sel_self != pyres && !PyErr_Occurred()) {
586				PyObjCObject_ClearObject(self->sel_self);
587			}
588		}
589	} else {
590		PyObject* arglist;
591		PyObject* myClass;
592		Py_ssize_t i;
593		Py_ssize_t argslen;
594
595		argslen = PyTuple_Size(args);
596		arglist = PyTuple_New(argslen - 1);
597		for (i = 1; i < argslen; i++) {
598			PyObject* v = PyTuple_GET_ITEM(args, i);
599			if (v == NULL) {
600				Py_DECREF(arglist);
601				return NULL;
602			}
603
604			PyTuple_SET_ITEM(arglist, i-1, v);
605			Py_INCREF(v);
606		}
607
608		myClass = PyObjCClass_New(self->sel_class);
609		if (!(PyObject_IsInstance(pyself, myClass)
610#if PY_MAJOR_VERSION == 2
611			|| (PyString_Check(pyself) && class_isSubclassOf(self->sel_class, [NSString class]))
612#endif
613			|| (PyUnicode_Check(pyself) && class_isSubclassOf(self->sel_class, [NSString class]))
614		)) {
615			Py_DECREF(arglist);
616			Py_DECREF(myClass);
617			PyErr_Format(PyExc_TypeError,
618				"Expecting instance of %s as self, got one "
619				"of %s", class_getName(self->sel_class),
620				Py_TYPE(pyself)->tp_name);
621			return NULL;
622		}
623		Py_DECREF(myClass);
624
625
626		pyres = res = execute((PyObject*)self, pyself, arglist);
627		if (pyres != NULL
628			&& PyTuple_Check(pyres)
629			&& PyTuple_GET_SIZE(pyres) > 1
630			&& PyTuple_GET_ITEM(pyres, 0) == pyself) {
631			pyres = pyself;
632		}
633
634		if (PyObjCObject_Check(pyself) && (((PyObjCObject*)pyself)->flags & PyObjCObject_kUNINITIALIZED)) {
635			if (pyself != pyres && !PyErr_Occurred()) {
636				PyObjCObject_ClearObject(pyself);
637			}
638		}
639
640		Py_DECREF(arglist);
641	}
642
643	if (pyres && PyObjCObject_Check(pyres)) {
644		if (self->sel_flags & PyObjCSelector_kRETURNS_UNINITIALIZED) {
645			((PyObjCObject*)pyres)->flags |= PyObjCObject_kUNINITIALIZED;
646		} else if (((PyObjCObject*)pyself)->flags & PyObjCObject_kUNINITIALIZED) {
647			((PyObjCObject*)pyself)->flags &=
648				~PyObjCObject_kUNINITIALIZED;
649			if (self->sel_self && self->sel_self != pyres && !PyErr_Occurred()) {
650				PyObjCObject_ClearObject(self->sel_self);
651			}
652		}
653	}
654
655	return res;
656}
657
658static PyObject*
659objcsel_descr_get(PyObject* _self, PyObject* volatile obj, PyObject* class)
660{
661	PyObjCNativeSelector* meth = (PyObjCNativeSelector*)_self;
662	PyObjCNativeSelector* result;
663
664	if (meth->sel_self != NULL || obj == Py_None) {
665		Py_INCREF(meth);
666		return (PyObject*)meth;
667	}
668
669	if (class != nil && PyType_Check(class) && PyType_IsSubtype((PyTypeObject*)class, &PyObjCClass_Type)) {
670		class = PyObjCClass_ClassForMetaClass(class);
671	}
672
673	/* Bind 'self' */
674	if (meth->sel_flags & PyObjCSelector_kCLASS_METHOD) {
675		obj = class;
676	}
677	result = PyObject_New(PyObjCNativeSelector, &PyObjCNativeSelector_Type);
678	result->sel_selector   = meth->sel_selector;
679	result->sel_python_signature  = PyObjCUtil_Strdup(meth->sel_python_signature);
680	if (result->sel_python_signature == NULL) {
681		Py_DECREF(result);
682		return NULL;
683	}
684
685	if (meth->sel_native_signature != NULL) {
686		result->sel_native_signature = PyObjCUtil_Strdup(meth->sel_native_signature);
687		if (result->sel_native_signature == NULL) {
688			Py_DECREF(result);
689			return NULL;
690		}
691	} else {
692		result->sel_native_signature = NULL;
693	}
694	result->sel_flags = meth->sel_flags;
695	result->sel_class = meth->sel_class;
696
697	if (meth->sel_call_func == NULL) {
698		meth->sel_call_func = PyObjC_FindCallFunc(meth->sel_class,
699			meth->sel_selector);
700	}
701	result->sel_call_func = meth->sel_call_func;
702
703	result->sel_methinfo = PyObjCSelector_GetMetadata((PyObject*)meth);
704	if (result->sel_methinfo) {
705		Py_INCREF(result->sel_methinfo);
706	} else {
707		PyErr_Clear();
708	}
709
710	result->sel_self = obj;
711	if (result->sel_self) {
712		Py_INCREF(result->sel_self);
713	}
714
715	return (PyObject*)result;
716}
717
718
719
720PyTypeObject PyObjCNativeSelector_Type = {
721	PyVarObject_HEAD_INIT(&PyType_Type, 0)
722	"objc.native_selector",			/* tp_name */
723	sizeof(PyObjCNativeSelector),		/* tp_basicsize */
724	0,					/* tp_itemsize */
725	/* methods */
726	sel_dealloc,				/* tp_dealloc */
727	0,					/* tp_print */
728	0,					/* tp_getattr */
729	0,					/* tp_setattr */
730	0,					/* tp_compare */
731	objcsel_repr,				/* tp_repr */
732	0,					/* tp_as_number */
733	0,					/* tp_as_sequence */
734	0,		       			/* tp_as_mapping */
735	0,					/* tp_hash */
736	objcsel_call,				/* tp_call */
737	0,					/* tp_str */
738	PyObject_GenericGetAttr,		/* tp_getattro */
739	0,					/* tp_setattro */
740	0,					/* tp_as_buffer */
741	Py_TPFLAGS_DEFAULT,			/* tp_flags */
742 	0,					/* tp_doc */
743 	0,					/* tp_traverse */
744 	0,					/* tp_clear */
745	0,					/* tp_richcompare */
746	0,					/* tp_weaklistoffset */
747	0,					/* tp_iter */
748	0,					/* tp_iternext */
749	0,					/* tp_methods */
750	0,					/* tp_members */
751	0,					/* tp_getset */
752	&PyObjCSelector_Type,			/* tp_base */
753	0,					/* tp_dict */
754	objcsel_descr_get,			/* tp_descr_get */
755	0,					/* tp_descr_set */
756	0,					/* tp_dictoffset */
757	0,					/* tp_init */
758	0,					/* tp_alloc */
759	0,					/* tp_new */
760	0,		        		/* tp_free */
761	0,					/* tp_is_gc */
762	0,                                      /* tp_bases */
763	0,                                      /* tp_mro */
764	0,                                      /* tp_cache */
765	0,                                      /* tp_subclasses */
766	0,                                      /* tp_weaklist */
767	0                                       /* tp_del */
768#if PY_VERSION_HEX >= 0x02060000
769	, 0                                     /* tp_version_tag */
770#endif
771
772};
773
774
775
776static Class Object_class = nil;
777
778PyObject*
779PyObjCSelector_FindNative(PyObject* self, const char* name)
780{
781	volatile SEL   sel = PyObjCSelector_DefaultSelector(name);
782	PyObject* retval;
783
784	NSMethodSignature* methsig;
785	char  buf[1024];
786
787	if (PyObjCObject_Check(self)) {
788		if (PyObjCClass_HiddenSelector((PyObject*)Py_TYPE(self), sel, NO)) {
789			PyErr_Format(PyExc_AttributeError,
790				"No attribute %s", name);
791			return NULL;
792		}
793	} else {
794		if (PyObjCClass_HiddenSelector(self, sel, YES)) {
795			PyErr_Format(PyExc_AttributeError,
796				"No attribute %s", name);
797			return NULL;
798		}
799	}
800
801	if (Object_class == nil) {
802		Object_class = objc_getClass("Object");
803	}
804
805	if (name[0] == '_' && name[1] == '_') {
806		/* No known Objective-C class has methods whose name
807		 * starts with '__' or '_:'. This allows us to shortcut
808		 * lookups for special names, which speeds up tools like
809		 * pydoc.
810		 */
811		PyErr_Format(PyExc_AttributeError,
812			"No attribute %s", name);
813		return NULL;
814	}
815
816	if (PyObjCClass_Check(self)) {
817		Class cls = PyObjCClass_GetClass(self);
818
819		if (!cls) {
820			PyErr_Format(PyExc_AttributeError,
821				"No attribute %s", name);
822			return NULL;
823		}
824		if (strcmp(class_getName(cls), "_NSZombie") == 0) {
825			PyErr_Format(PyExc_AttributeError,
826				"No attribute %s", name);
827			return NULL;
828		}
829
830		if (strcmp(class_getName(cls), "NSProxy") == 0) {
831			if (sel == @selector(methodSignatureForSelector:)) {
832				PyErr_Format(PyExc_AttributeError,
833					"Accessing NSProxy.%s is not supported",
834					name);
835				return NULL;
836			}
837		}
838
839		NS_DURING
840			if ([cls instancesRespondToSelector:sel]) {
841				methsig = [cls instanceMethodSignatureForSelector:sel];
842				retval = PyObjCSelector_NewNative(cls, sel,
843					PyObjC_NSMethodSignatureToTypeString(methsig, buf, sizeof(buf)), 0);
844			} else if ((Object_class != nil) && (cls != Object_class) && nil != (methsig = [(NSObject*)cls methodSignatureForSelector:sel])) {
845				retval = PyObjCSelector_NewNative(cls, sel,
846					PyObjC_NSMethodSignatureToTypeString(
847						methsig, buf, sizeof(buf)), 1);
848			} else {
849				PyErr_Format(PyExc_AttributeError,
850					"No attribute %s", name);
851				retval = NULL;
852			}
853		NS_HANDLER
854			PyErr_Format(PyExc_AttributeError,
855				"No attribute %s", name);
856			retval = NULL;
857
858		NS_ENDHANDLER
859
860		return retval;
861
862	} else if (PyObjCObject_Check(self)) {
863		id object;
864
865		object = PyObjCObject_GetObject(self);
866
867		NS_DURING
868			if (nil != (methsig = [object methodSignatureForSelector:sel])){
869				PyObjCNativeSelector* res;
870
871				res =  (PyObjCNativeSelector*)PyObjCSelector_NewNative(
872					object_getClass(object), sel,
873					PyObjC_NSMethodSignatureToTypeString(methsig,
874						buf, sizeof(buf)), 0);
875				if (res != NULL) {
876					/* Bind the method to self */
877					res->sel_self = self;
878					Py_INCREF(res->sel_self);
879				}
880				retval = (PyObject*)res;
881			} else {
882				PyErr_Format(PyExc_AttributeError,
883					"No attribute %s", name);
884				retval = NULL;
885			}
886
887		NS_HANDLER
888			PyErr_Format(PyExc_AttributeError,
889				"No attribute %s", name);
890			retval = NULL;
891
892		NS_ENDHANDLER
893
894		return retval;
895	} else {
896		PyErr_SetString(PyExc_RuntimeError,
897			"PyObjCSelector_FindNative called on plain "
898			"python object");
899		return NULL;
900	}
901}
902
903
904PyObject*
905PyObjCSelector_NewNative(Class class,
906			SEL selector, const char* signature, int class_method)
907{
908	PyObjCNativeSelector* result;
909	const char* native_signature = signature;
910	char* repl_sig;
911
912	repl_sig = PyObjC_FindReplacementSignature(class, selector);
913	if (repl_sig) {
914		signature = repl_sig;
915	}
916
917	if (signature == NULL) {
918		PyErr_SetString(PyObjCExc_Error, "Selector with NULL or too long signature");
919		return NULL;
920	}
921
922	result = PyObject_New(PyObjCNativeSelector, &PyObjCNativeSelector_Type);
923	if (result == NULL) return NULL;
924
925	result->sel_selector = selector;
926	result->sel_python_signature = PyObjCUtil_Strdup(signature);
927	result->sel_native_signature = PyObjCUtil_Strdup(native_signature);
928	if (result->sel_python_signature == NULL) {
929		Py_DECREF(result);
930		return NULL;
931	}
932	result->sel_self = NULL;
933	result->sel_class = class;
934	result->sel_call_func = NULL;
935	result->sel_methinfo = NULL;
936	result->sel_flags = 0;
937	if (class_method) {
938		result->sel_flags |= PyObjCSelector_kCLASS_METHOD;
939	}
940	if (sel_isEqual(selector, @selector(alloc)) || sel_isEqual(selector, @selector(allocWithZone:))) {
941		  result->sel_flags |= PyObjCSelector_kRETURNS_UNINITIALIZED;
942	}
943	result->sel_call_func = NULL;
944	return (PyObject*)result;
945}
946
947
948static char gSheetMethodSignature[] = { _C_VOID, _C_ID, _C_SEL, _C_ID, _C_INT, _C_PTR , _C_VOID, 0 };
949
950PyObject*
951PyObjCSelector_New(PyObject* callable,
952	SEL selector, const char* signature, int class_method, Class cls)
953{
954	PyObjCPythonSelector* result;
955	if (signature == NULL) {
956		const char* selname = sel_getName(selector);
957		size_t len = strlen(selname);
958		if (len > 30 && strcmp(selname+len-30, "DidEnd:returnCode:contextInfo:") == 0) {
959			signature = PyObjCUtil_Strdup(gSheetMethodSignature);
960		} else {
961			signature = pysel_default_signature(callable);
962		}
963	} else {
964		signature = PyObjCUtil_Strdup(signature);
965	}
966	if (signature == NULL) return NULL;
967
968	result = PyObject_New(PyObjCPythonSelector, &PyObjCPythonSelector_Type);
969	if (result == NULL) return NULL;
970
971	result->sel_selector = selector;
972	result->sel_python_signature = signature;
973	result->sel_native_signature = PyObjCUtil_Strdup(signature);
974	if (result->sel_native_signature == NULL) {
975		Py_DECREF(result);
976		return NULL;
977	}
978	PyObjC_RemoveInternalTypeCodes((char*)result->sel_native_signature);
979
980	result->sel_self = NULL;
981	result->sel_class = cls;
982	result->sel_flags = 0;
983	result->sel_methinfo = NULL; /* We might not know the class right now */
984
985	if (PyObjCPythonSelector_Check(callable)) {
986		callable = ((PyObjCPythonSelector*)callable)->callable;
987	}
988	if (PyFunction_Check(callable)) {
989		result->argcount = ((PyCodeObject*)PyFunction_GetCode(callable))->co_argcount;
990	}  else if (PyMethod_Check(callable)) {
991		if (PyMethod_Self(callable) == NULL) {
992			result->argcount = ((PyCodeObject*)PyFunction_GetCode(PyMethod_Function(callable)))->co_argcount;
993		} else {
994			result->argcount = ((PyCodeObject*)PyFunction_GetCode(PyMethod_Function(callable)))->co_argcount - 1;
995		}
996	} else if (callable == Py_None) {
997		result->argcount = 0;
998
999	} else {
1000		/* Should not happen... */
1001		result->argcount = 0;
1002		const char* s = sel_getName(selector);
1003		while ((s = strchr(s, ':')) != NULL) {
1004			result->argcount++;
1005			s++;
1006		}
1007	}
1008
1009	if (class_method) {
1010		result->sel_flags |= PyObjCSelector_kCLASS_METHOD;
1011	}
1012	if (sel_isEqual(selector, @selector(alloc)) || sel_isEqual(selector, @selector(allocWithZone:))) {
1013		  result->sel_flags |= PyObjCSelector_kRETURNS_UNINITIALIZED;
1014	}
1015
1016	result->callable = callable;
1017	Py_INCREF(result->callable);
1018
1019	return (PyObject*)result;
1020}
1021
1022
1023/*
1024 * Selector type for python selectors (that is, selectors that are
1025 * implemented as python methods)
1026 *
1027 * This one can be allocated from python code.
1028 */
1029
1030static long
1031pysel_hash(PyObject* o)
1032{
1033	PyObjCPythonSelector* self = (PyObjCPythonSelector*)o;
1034	long h = 0;
1035
1036	if (self->sel_self) {
1037		h ^= PyObject_Hash(self->sel_self);
1038	}
1039	h ^= (long)(self->sel_class);
1040	h ^= PyObject_Hash(self->callable);
1041
1042	return h;
1043}
1044
1045
1046static PyObject* pysel_richcompare(PyObject* a, PyObject* b, int op)
1047{
1048	if (op == Py_EQ || op == Py_NE) {
1049		if (PyObjCPythonSelector_Check(a) && PyObjCPythonSelector_Check(b)) {
1050			PyObjCPythonSelector* sel_a = (PyObjCPythonSelector*)a;
1051			PyObjCPythonSelector* sel_b = (PyObjCPythonSelector*)b;
1052			int same = 1;
1053			int r;
1054
1055			if (sel_a->sel_selector != sel_b->sel_selector) {
1056				same = 0;
1057			}
1058			if (sel_a->sel_class != sel_b->sel_class) {
1059				same = 0;
1060			}
1061			if (sel_a->sel_self != sel_b->sel_self) {
1062				same = 0;
1063			}
1064			r = PyObject_RichCompareBool(
1065					sel_a->callable,
1066					sel_b->callable, Py_EQ);
1067			if (r == -1) {
1068				return NULL;
1069			}
1070			if (!r) {
1071				same = 0;
1072			}
1073
1074			if ((op == Py_EQ && !same) || (op == Py_NE && same)) {
1075				Py_INCREF(Py_False);
1076				return Py_False;
1077			} else {
1078				Py_INCREF(Py_False);
1079				return Py_True;
1080			}
1081
1082		} else {
1083			if (op == Py_EQ) {
1084				Py_INCREF(Py_False);
1085				return Py_False;
1086			} else {
1087				Py_INCREF(Py_False);
1088				return Py_True;
1089			}
1090		}
1091	} else {
1092		PyErr_SetString(PyExc_TypeError, "Cannot use '<', '<=', '>=' and '>' with objc.selector");
1093		return NULL;
1094	}
1095}
1096
1097static PyObject*
1098pysel_repr(PyObject* _self)
1099{
1100	PyObjCPythonSelector* sel = (PyObjCPythonSelector*)_self;
1101	PyObject *rval;
1102
1103	if (sel->sel_self == NULL) {
1104		if (sel->sel_class) {
1105			rval = PyText_FromFormat("<unbound selector %s of %s at %p>", sel_getName(sel->sel_selector), class_getName(sel->sel_class), sel);
1106		} else {
1107			rval = PyText_FromFormat("<unbound selector %s at %p>", sel_getName(sel->sel_selector), sel);
1108		}
1109	} else {
1110#if PY_MAJOR_VERSION == 2
1111		PyObject* selfrepr = PyObject_Repr(sel->sel_self);
1112		if (selfrepr == NULL) {
1113			return NULL;
1114		}
1115		if (!PyString_Check(selfrepr)) {
1116			Py_DECREF(selfrepr);
1117			return NULL;
1118		}
1119		rval = PyText_FromFormat("<selector %s of %s>", sel_getName(sel->sel_selector), PyString_AS_STRING(selfrepr));
1120		Py_DECREF(selfrepr);
1121#else
1122		rval = PyText_FromFormat("<selector %s of %R>", sel_getName(sel->sel_selector), sel->sel_self);
1123#endif
1124	}
1125	return rval;
1126}
1127
1128
1129/*
1130 * Calling the method from Python is sligtly complicated by the fact that
1131 * output arguments are optionally present (both in the method signature
1132 * and the actual argument list).
1133 *
1134 * pysel_call needs to compensate for this, which is done by this function.
1135 */
1136static PyObject*
1137compensate_arglist(PyObject* _self, PyObject* args, PyObject* kwds)
1138{
1139	/* XXX: need to do a full metadata processing run here to get exactly the right
1140	 * semantics, we also have to do a metadata run on the result!
1141	 */
1142	PyObjCPythonSelector* self = (PyObjCPythonSelector*)_self;
1143	BOOL argsmatch;
1144	Py_ssize_t i;
1145	Py_ssize_t first_arg;
1146
1147	if (self->sel_methinfo == NULL) {
1148		/* Make sure we actually have metadata */
1149		PyObjCSelector_GetMetadata(_self);
1150	}
1151	if (self->numoutput == 0) {
1152		Py_INCREF(args);
1153		return args;
1154	}
1155
1156
1157	if (kwds && PyDict_Size(kwds) != 0) {
1158		/* XXX: we cannot do anything here without reimplementing Python's argument
1159		 * matching code...
1160		 */
1161		Py_INCREF(args);
1162		return args;
1163	}
1164
1165	argsmatch = ((PyTuple_Size(args) + (self->sel_self?1:0)) == self->argcount);
1166
1167	first_arg = self->sel_self ? 0 : 1;
1168
1169	if (self->argcount == Py_SIZE(self->sel_methinfo)-1) { /* the selector has an implicit '_sel' argument as well */
1170		/* All arguments are present, including output arguments */
1171		if (argsmatch) {
1172			for (i = 2; i < Py_SIZE(self->sel_methinfo); i++) {
1173				if (self->sel_methinfo->argtype[i].type[0] == _C_OUT) {
1174					PyObject* a = PyTuple_GET_ITEM(args, first_arg + i - 2);
1175					if (a != Py_None && a != PyObjC_NULL) {
1176						PyErr_Format(PyExc_TypeError,
1177							"argument %" PY_FORMAT_SIZE_T "d is an output argument but is passed a value other than None or objc.NULL (%s)",
1178							i-1-first_arg, PyObject_REPR(args));
1179						return NULL;
1180					}
1181				}
1182			}
1183			Py_INCREF(args);
1184			return args;
1185		} else {
1186			if ((PyTuple_Size(args) + (self->sel_self?1:0)) != (self->argcount - self->numoutput)) {
1187				/* There's the wrong number of arguments */
1188				PyErr_Format(PyExc_TypeError,
1189					"expecting %" PY_FORMAT_SIZE_T "d arguments, got %" PY_FORMAT_SIZE_T "d", self->argcount - (self->sel_self?1:0), PyTuple_Size(args));
1190				return NULL;
1191			}
1192
1193			PyObject* real_args;
1194			Py_ssize_t pyarg;
1195
1196
1197			real_args = PyTuple_New(self->argcount - (self->sel_self?1:0));
1198			if (real_args == NULL) {
1199				return NULL;
1200			}
1201
1202			pyarg = 0;
1203			if (self->sel_self == NULL) {
1204				pyarg = 1;
1205				PyTuple_SET_ITEM(real_args, 0, PyTuple_GET_ITEM(args, 0));
1206				Py_INCREF(PyTuple_GET_ITEM(args, 0));
1207			}
1208
1209			PyObjCMethodSignature* methinfo = PyObjCSelector_GetMetadata(_self);
1210			for (i = 2; i < Py_SIZE(methinfo); i++) {
1211				if (methinfo->argtype[i].type[0] == _C_OUT) {
1212					PyTuple_SET_ITEM(real_args, i-2+first_arg, Py_None);
1213					Py_INCREF(Py_None);
1214				} else {
1215					PyTuple_SET_ITEM(real_args, i-2+first_arg, PyTuple_GET_ITEM(args, pyarg));
1216					Py_INCREF(PyTuple_GET_ITEM(args, pyarg));
1217					pyarg++;
1218				}
1219			}
1220
1221			return real_args;
1222		}
1223
1224	} else {
1225		/* Not all arguments are present, output arguments should
1226		 * be excluded.
1227		 */
1228		if (argsmatch) {
1229			Py_INCREF(args);
1230			return args;
1231		} else {
1232			if (PyTuple_Size(args) + (self->sel_self?1:0) != self->argcount + self->numoutput) {
1233				/* There's the wrong number of arguments */
1234				PyErr_Format(PyExc_TypeError,
1235					"expecting %" PY_FORMAT_SIZE_T "d arguments, got %" PY_FORMAT_SIZE_T "d", self->argcount - (self->sel_self?1:0), PyTuple_Size(args));
1236				return NULL;
1237			}
1238			PyObject* real_args;
1239			Py_ssize_t pyarg;
1240
1241			real_args = PyTuple_New(self->argcount - (self->sel_self?1:0));
1242			if (real_args == NULL) {
1243				return NULL;
1244			}
1245
1246			pyarg = 0;
1247			if (self->sel_self == NULL) {
1248				pyarg = 1;
1249				PyTuple_SET_ITEM(real_args, 0, PyTuple_GET_ITEM(args, 0));
1250				Py_INCREF(PyTuple_GET_ITEM(args, 0));
1251			}
1252
1253			PyObjCMethodSignature* methinfo = PyObjCSelector_GetMetadata(_self);
1254			for (i = 2; i < Py_SIZE(methinfo); i++) {
1255				if (methinfo->argtype[i].type[0] != _C_OUT) {
1256					PyTuple_SET_ITEM(real_args, pyarg, PyTuple_GET_ITEM(args, i-2+first_arg));
1257					Py_INCREF(PyTuple_GET_ITEM(args, i-2+first_arg));
1258					pyarg++;
1259				}
1260			}
1261
1262			return real_args;
1263		}
1264	}
1265}
1266
1267static PyObject*
1268pysel_call(PyObject* _self, PyObject* args, PyObject* kwargs)
1269{
1270	PyObjCPythonSelector* self = (PyObjCPythonSelector*)_self;
1271	PyObject* result;
1272
1273	if (self->callable == NULL) {
1274		PyErr_Format(PyExc_TypeError,
1275			"Calling abstract methods with selector %s",
1276			sel_getName(self->sel_selector));
1277		return NULL;
1278	}
1279
1280	args = compensate_arglist(_self, args, kwargs);
1281	if (args == NULL) {
1282		return NULL;
1283	}
1284
1285	if (!PyMethod_Check(self->callable)) {
1286		if (self->sel_self == NULL) {
1287			PyObject* self_arg;
1288			if (PyTuple_Size(args) < 1) {
1289				Py_DECREF(args);
1290				PyErr_SetString(PyObjCExc_Error, "need self argument");
1291				return NULL;
1292			}
1293			self_arg = PyTuple_GET_ITEM(args, 0);
1294			if (!PyObjCObject_Check(self_arg) && !PyObjCClass_Check(self_arg)) {
1295				Py_DECREF(args);
1296				PyErr_Format(PyExc_TypeError,
1297					"Expecting an Objective-C class or "
1298					"instance as self, got a %s",
1299					Py_TYPE(self_arg)->tp_name);
1300				return NULL;
1301			}
1302		}
1303
1304		/* normal function code will perform other checks */
1305	}
1306
1307	/*
1308	 * Assume callable will check arguments
1309	 */
1310	if (self->sel_self == NULL) {
1311		result  = PyObject_Call(self->callable, args, kwargs);
1312		Py_DECREF(args);
1313
1314	} else {
1315		Py_ssize_t argc = PyTuple_Size(args);
1316		PyObject* actual_args = PyTuple_New(argc+1);
1317		Py_ssize_t i;
1318
1319		if (actual_args == NULL) {
1320			return NULL;
1321		}
1322		Py_INCREF(self->sel_self);
1323		PyTuple_SetItem(actual_args, 0, self->sel_self);
1324		for (i = 0; i < argc; i++) {
1325			PyObject* v = PyTuple_GET_ITEM(args, i);
1326			Py_XINCREF(v);
1327			PyTuple_SET_ITEM(actual_args, i+1, v);
1328		}
1329		result = PyObject_Call(self->callable,
1330			actual_args, kwargs);
1331		Py_DECREF(actual_args);
1332		Py_DECREF(args);
1333	}
1334
1335	if ( result && (self->sel_self) && (PyObjCObject_Check(self->sel_self)) &&
1336	     ((PyObjCObject*)self->sel_self)->flags & PyObjCObject_kUNINITIALIZED) {
1337
1338	     ((PyObjCObject*)self->sel_self)->flags &= ~PyObjCObject_kUNINITIALIZED;
1339
1340	}
1341
1342	return result;
1343}
1344
1345static char*
1346pysel_default_signature(PyObject* callable)
1347{
1348	PyCodeObject* func_code;
1349	Py_ssize_t    arg_count;
1350	char*         result;
1351	const unsigned char *buffer;
1352	Py_ssize_t    buffer_len;
1353	Py_ssize_t    i;
1354	int           was_none;
1355
1356	if (PyFunction_Check(callable)) {
1357		func_code = (PyCodeObject*)PyFunction_GetCode(callable);
1358	} else if (PyMethod_Check(callable)) {
1359		func_code = (PyCodeObject*)PyFunction_GetCode(PyMethod_Function(callable));
1360	} else {
1361		PyErr_SetString(PyExc_TypeError,
1362			"Cannot calculate default method signature");
1363		return NULL;
1364	}
1365
1366	arg_count = func_code->co_argcount;
1367	if (arg_count < 1) {
1368		PyErr_SetString(PyExc_TypeError,
1369			"Objective-C callable methods must take at least one argument");
1370		return NULL;
1371	}
1372
1373
1374	/* arguments + return-type + selector */
1375	result = PyMem_Malloc(arg_count+3);
1376	if (result == 0) {
1377		PyErr_NoMemory();
1378		return NULL;
1379	}
1380
1381	/* We want: v@:@... (final sequence of arg_count-1 @-chars) */
1382	memset(result, _C_ID, arg_count+2);
1383	result[0] = _C_VOID;
1384	result[2] = _C_SEL;
1385	result[arg_count+2] = '\0';
1386
1387	if (PyObject_AsReadBuffer(func_code->co_code, (const void **)&buffer, &buffer_len)) {
1388		return NULL;
1389	}
1390
1391	/*
1392	   Scan bytecode to find return statements.  If any non-bare return
1393	   statement exists, then set the return type to @ (id).
1394	*/
1395	was_none = 0;
1396	for (i=0; i<buffer_len; ++i) {
1397		int op = buffer[i];
1398		if (op == LOAD_CONST && buffer[i+1] == 0 && buffer[i+2] == 0) {
1399			was_none = 1;
1400		} else {
1401			if (op == RETURN_VALUE) {
1402				if (!was_none) {
1403					result[0] = _C_ID;
1404					break;
1405				}
1406			}
1407			was_none = 0;
1408		}
1409		if (op >= HAVE_ARGUMENT) {
1410			i += 2;
1411		}
1412	}
1413	return result;
1414}
1415
1416static SEL
1417pysel_default_selector(PyObject* callable)
1418{
1419	char buf[1024];
1420	char* cur;
1421	PyObject* name = PyObject_GetAttrString(callable, "__name__");
1422
1423	if (name == NULL) return NULL;
1424
1425	if (PyUnicode_Check(name)) {
1426		PyObject* bytes = PyUnicode_AsEncodedString(name, NULL, NULL);
1427		if (bytes == NULL) {
1428			return NULL;
1429		}
1430		strncpy(buf, PyBytes_AS_STRING(bytes), sizeof(buf)-1);
1431		Py_DECREF(bytes);
1432
1433
1434#if PY_MAJOR_VERSION == 2
1435	} else if (PyString_Check(name)) {
1436		strncpy(buf, PyString_AS_STRING(name), sizeof(buf)-1);
1437#endif
1438	} else  {
1439		return NULL;
1440	}
1441
1442
1443	cur = strchr(buf, '_');
1444	while (cur != NULL) {
1445		*cur = ':';
1446		cur = strchr(cur, '_');
1447	}
1448	return sel_registerName(buf);
1449}
1450
1451SEL
1452PyObjCSelector_DefaultSelector(const char* methname)
1453{
1454	char buf[1024];
1455	char* cur;
1456	Py_ssize_t ln;
1457
1458	strncpy(buf, methname, sizeof(buf)-1);
1459	ln = strlen(buf);
1460
1461	cur = buf + ln;
1462	if (cur - buf > 3) {
1463		if (cur[-1] != '_') {
1464			return sel_registerName(buf);
1465		}
1466
1467		if (cur[-1] == '_' && cur[-2] == '_') {
1468			cur[-2] = '\0';
1469			if (PyObjC_IsPythonKeyword(buf)) {
1470				return sel_registerName(buf);
1471			}
1472			cur[-2] = '_';
1473		}
1474	}
1475
1476	/* Skip leading underscores, '_doFoo_' is probably '_doFoo:' in
1477	 * Objective-C, not ':doFoo:'.
1478	 *
1479	 * Also if the name starts and ends with two underscores, return
1480	 * it unmodified. This avoids mangling of Python's special methods.
1481	 *
1482	 * Both are heuristics and could be the wrong choice, but either
1483	 * form is very unlikely to exist in ObjC code.
1484	 */
1485	cur = buf;
1486
1487	if (ln > 5) {
1488		if (cur[0] == '_' && cur[1] == '_' &&
1489		    cur[ln-1] == '_' && cur[ln-2] == '_') {
1490			return sel_registerName(buf);
1491		}
1492	}
1493
1494	while (*cur == '_') {
1495		cur++;
1496	}
1497
1498	/* Replace all other underscores by colons */
1499	cur = strchr(cur, '_');
1500	while (cur != NULL) {
1501		*cur = ':';
1502		cur = strchr(cur, '_');
1503	}
1504	return sel_registerName(buf);
1505}
1506
1507static char
1508pytype_to_objc(char val)
1509{
1510	switch (val) {
1511	case 's': case 'z': case 'S': return _C_ID;
1512	case 'b': return _C_CHR;
1513	case 'h': return _C_SHT;
1514	case 'i': return _C_INT;
1515	case 'l': return _C_LNG;
1516	case 'c': return _C_CHR;
1517	case 'f': return _C_FLT;
1518	case 'd': return _C_DBL;
1519	case 'O': return _C_ID;
1520	default:
1521		PyErr_Format(PyExc_ValueError, "Unrecognized type character: %c", val);
1522		return 0;
1523	}
1524}
1525
1526static char*
1527python_signature_to_objc(char* rettype, char* argtypes, char* buf,
1528	size_t buflen)
1529{
1530	char* result = buf;
1531
1532	if (buflen < 4) {
1533		PyErr_SetString(PyExc_RuntimeError,
1534			"Too small buffer for python_signature_to_objc");
1535		return NULL;
1536	}
1537	if (rettype) {
1538		if (*rettype == 0) {
1539			*buf = _C_VOID;
1540		} else if (rettype[1] != 0) {
1541			PyErr_SetString(PyExc_ValueError,
1542				"Only recognizing simple type specifiers");
1543			return NULL;
1544		}
1545		*buf = pytype_to_objc(*rettype);
1546		if (*buf == 0) return NULL;
1547	} else {
1548		*buf = _C_VOID;
1549	}
1550	buf++;
1551
1552	/* self and selector, required */
1553	*buf++ = '@';
1554	*buf++ = ':';
1555
1556	buflen -= 3;
1557
1558	if (!argtypes) {
1559		*buf++ = '\0';
1560		return result;
1561	}
1562
1563	/* encode arguments */
1564	while (buflen > 0 && *argtypes) {
1565		*buf = pytype_to_objc(*argtypes++);
1566		if (*buf == 0) return NULL;
1567		buf++;
1568		buflen --;
1569	}
1570
1571	if (buflen == 0) {
1572		PyErr_SetString(PyExc_RuntimeError,
1573			"Too small buffer for python_signature_to_objc");
1574		return NULL;
1575	}
1576	*buf = 0;
1577	return result;
1578}
1579
1580
1581/* TODO: Check value of 'signature' */
1582static PyObject*
1583pysel_new(PyTypeObject* type __attribute__((__unused__)),
1584	  PyObject* args, PyObject* kwds)
1585{
1586static	char*	keywords[] = { "function", "selector", "signature",
1587				"isClassMethod", "argumentTypes",
1588				"returnType", "isRequired", "isHidden", NULL };
1589	PyObjCPythonSelector* result;
1590	PyObject* callable;
1591	char*     signature = NULL;
1592	char* 	  argtypes = NULL;
1593	char*     rettype = NULL;
1594	char*	  selector = NULL;
1595	SEL       objc_selector;
1596	int	  class_method = 0;
1597	char      signature_buf[1024];
1598	int       required = 1;
1599	int       hidden = 0;
1600
1601	if (!PyArg_ParseTupleAndKeywords(args, kwds,
1602				"O|"Py_ARG_BYTES Py_ARG_BYTES"issii:selector",
1603			keywords, &callable, &selector, &signature,
1604			&class_method, &argtypes, &rettype, &required, &hidden)) {
1605		return NULL;
1606	}
1607
1608	if (signature != NULL && (rettype != NULL || argtypes != NULL)) {
1609		PyErr_SetString(PyExc_TypeError,
1610			"selector: provide either the objective-C signature, "
1611			"or the python signature but not both");
1612		return NULL;
1613	}
1614
1615	if (rettype || argtypes) {
1616		signature = python_signature_to_objc(rettype, argtypes,
1617			signature_buf, sizeof(signature_buf));
1618		if (signature == NULL) return NULL;
1619	} else if (signature != NULL) {
1620		/* Check if the signature string is valid */
1621		const char* cur;
1622
1623		cur = signature;
1624		while (*cur != '\0') {
1625			cur = PyObjCRT_SkipTypeSpec(cur);
1626			if (cur == NULL) {
1627				PyErr_SetString(
1628					PyExc_ValueError,
1629					"invalid signature");
1630				return NULL;
1631			}
1632		}
1633	}
1634
1635
1636	if (callable != Py_None && !PyCallable_Check(callable)) {
1637		PyErr_SetString(PyExc_TypeError,
1638			"argument 'method' must be callable");
1639		return NULL;
1640	}
1641
1642	if (PyObject_TypeCheck(callable, &PyClassMethod_Type)) {
1643		/* Special treatment for 'classmethod' instances */
1644		PyObject* tmp = PyObject_CallMethod(callable, "__get__", "OO",
1645				Py_None, &PyList_Type);
1646		if (tmp == NULL) {
1647			return NULL;
1648		}
1649
1650		if (PyFunction_Check(tmp)) {
1651			/* A 'staticmethod' instance, cannot convert */
1652			Py_DECREF(tmp);
1653			PyErr_SetString(PyExc_TypeError,
1654					"cannot use staticmethod as the "
1655					"callable for a selector.");
1656			return NULL;
1657		}
1658
1659#if PY_MAJOR_VERSION == 2
1660		callable = PyObject_GetAttrString(tmp, "im_func");
1661#else
1662		callable = PyObject_GetAttrString(tmp, "__funct__");
1663#endif
1664		Py_DECREF(tmp);
1665		if (callable == NULL) {
1666			return NULL;
1667		}
1668	} else {
1669		Py_INCREF(callable);
1670	}
1671
1672	if (selector == NULL) {
1673		objc_selector = pysel_default_selector(callable);
1674	} else {
1675		objc_selector = sel_registerName(selector);
1676	}
1677
1678	result = (PyObjCPythonSelector*)PyObjCSelector_New(callable,
1679			objc_selector, signature, class_method, nil);
1680	Py_DECREF(callable);
1681	if (!result) {
1682		return NULL;
1683	}
1684	if (required) {
1685		result->sel_flags |= PyObjCSelector_kREQUIRED;
1686	}
1687	if (hidden) {
1688		result->sel_flags |= PyObjCSelector_kHIDDEN;
1689	}
1690	return (PyObject *)result;
1691}
1692
1693static PyObject*
1694pysel_descr_get(PyObject* _meth, PyObject* obj, PyObject* class)
1695{
1696	PyObjCPythonSelector* meth = (PyObjCPythonSelector*)_meth;
1697	PyObjCPythonSelector* result;
1698
1699	if (meth->sel_self != NULL || obj == Py_None) {
1700		Py_INCREF(meth);
1701		return (PyObject*)meth;
1702	}
1703
1704	/* Bind 'self' */
1705	if (meth->sel_flags & PyObjCSelector_kCLASS_METHOD) {
1706		obj = class;
1707		if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject*)obj, &PyObjCClass_Type)) {
1708			obj = PyObjCClass_ClassForMetaClass(obj);
1709		}
1710	}
1711	result = PyObject_New(PyObjCPythonSelector, &PyObjCPythonSelector_Type);
1712	result->sel_selector   = meth->sel_selector;
1713	result->sel_class   = meth->sel_class;
1714	result->sel_python_signature  = PyObjCUtil_Strdup(meth->sel_python_signature);
1715	if (result->sel_python_signature == NULL) {
1716		Py_DECREF(result);
1717		return NULL;
1718	}
1719	if (meth->sel_native_signature) {
1720		result->sel_native_signature  = PyObjCUtil_Strdup(meth->sel_native_signature);
1721		if (result->sel_native_signature == NULL) {
1722			Py_DECREF(result);
1723			return NULL;
1724		}
1725	} else {
1726		result->sel_native_signature = NULL;
1727	}
1728
1729	result->sel_methinfo = PyObjCSelector_GetMetadata((PyObject*)meth);
1730	Py_XINCREF(result->sel_methinfo);
1731	result->argcount = meth->argcount;
1732	result->numoutput = meth->numoutput;
1733
1734	result->sel_self       = obj;
1735	result->sel_flags = meth->sel_flags;
1736	result->callable = meth->callable;
1737	if (result->sel_self) {
1738		Py_INCREF(result->sel_self);
1739	}
1740	if (result->callable) {
1741		Py_INCREF(result->callable);
1742	}
1743	return (PyObject*)result;
1744}
1745
1746
1747static void
1748pysel_dealloc(PyObject* obj)
1749{
1750	Py_DECREF(((PyObjCPythonSelector*)obj)->callable);
1751	(((PyObjCPythonSelector*)obj)->callable) = NULL;
1752	sel_dealloc(obj);
1753}
1754
1755PyDoc_STRVAR(pysel_get_callable_doc,
1756"Returns the python 'function' that implements this method.\n"
1757"\n"
1758);
1759static PyObject*
1760pysel_get_callable(PyObject* _self, void* closure __attribute__((__unused__)))
1761{
1762	PyObjCPythonSelector* self = (PyObjCPythonSelector*)_self;
1763	Py_INCREF(self->callable);
1764	return self->callable;
1765}
1766
1767PyDoc_STRVAR(pysel_docstring_doc,
1768	"The document string for a method");
1769static PyObject*
1770pysel_docstring(PyObject* _self, void* closure __attribute__((__unused__)))
1771{
1772	PyObjCPythonSelector* self = (PyObjCPythonSelector*)_self;
1773
1774	PyObject* docstr = PyObject_GetAttrString(self->callable, "__doc__");
1775	return docstr;
1776}
1777
1778static PyGetSetDef pysel_getset[] = {
1779	{
1780		"callable",
1781		pysel_get_callable,
1782		0,
1783		pysel_get_callable_doc,
1784		0
1785	},
1786	{
1787		"__doc__",
1788		pysel_docstring,
1789		0,
1790		pysel_docstring_doc,
1791		0
1792	},
1793
1794	{
1795		NULL,
1796		NULL,
1797		NULL,
1798		NULL,
1799		0
1800	}
1801};
1802
1803PyTypeObject PyObjCPythonSelector_Type = {
1804	PyVarObject_HEAD_INIT(&PyType_Type, 0)
1805	"objc.python_selector",			/* tp_name */
1806	sizeof(PyObjCPythonSelector),		/* tp_basicsize */
1807	0,					/* tp_itemsize */
1808	/* methods */
1809	pysel_dealloc,	 			/* tp_dealloc */
1810	0,					/* tp_print */
1811	0,					/* tp_getattr */
1812	0,					/* tp_setattr */
1813	0,					/* tp_compare */
1814	pysel_repr,				/* tp_repr */
1815	0,					/* tp_as_number */
1816	0,					/* tp_as_sequence */
1817	0,		       			/* tp_as_mapping */
1818	pysel_hash,				/* tp_hash */
1819	pysel_call,				/* tp_call */
1820	0,					/* tp_str */
1821	PyObject_GenericGetAttr,		/* tp_getattro */
1822	0,					/* tp_setattro */
1823	0,					/* tp_as_buffer */
1824	Py_TPFLAGS_DEFAULT,			/* tp_flags */
1825 	0,					/* tp_doc */
1826 	0,					/* tp_traverse */
1827 	0,					/* tp_clear */
1828	pysel_richcompare,			/* tp_richcompare */
1829	0,					/* tp_weaklistoffset */
1830	0,					/* tp_iter */
1831	0,					/* tp_iternext */
1832	0,					/* tp_methods */
1833	0,					/* tp_members */
1834	pysel_getset,				/* tp_getset */
1835	&PyObjCSelector_Type,			/* tp_base */
1836	0,					/* tp_dict */
1837	pysel_descr_get,			/* tp_descr_get */
1838	0,					/* tp_descr_set */
1839	0,					/* tp_dictoffset */
1840	0,					/* tp_init */
1841	0,					/* tp_alloc */
1842	0,					/* tp_new */
1843	0,		        		/* tp_free */
1844	0,					/* tp_is_gc */
1845	0,                                      /* tp_bases */
1846	0,                                      /* tp_mro */
1847	0,                                      /* tp_cache */
1848	0,                                      /* tp_subclasses */
1849	0,                                      /* tp_weaklist */
1850	0                                       /* tp_del */
1851#if PY_VERSION_HEX >= 0x02060000
1852	, 0                                     /* tp_version_tag */
1853#endif
1854
1855};
1856
1857const char* PyObjCSelector_Signature(PyObject* obj)
1858{
1859	return ((PyObjCSelector*)obj)->sel_python_signature;
1860}
1861
1862Class
1863PyObjCSelector_GetClass(PyObject* sel)
1864{
1865	if (!PyObjCNativeSelector_Check(sel)) {
1866		PyErr_SetString(PyExc_TypeError, "Expecting PyObjCSelector");
1867		return NULL;
1868	}
1869	return ((PyObjCNativeSelector*)sel)->sel_class;
1870}
1871
1872SEL
1873PyObjCSelector_GetSelector(PyObject* sel)
1874{
1875	if (!PyObjCSelector_Check(sel)) {
1876		PyErr_SetString(PyExc_TypeError, "Expecting PyObjCSelector");
1877		return NULL;
1878	}
1879	return ((PyObjCSelector*)sel)->sel_selector;
1880}
1881
1882
1883int   PyObjCSelector_Required(PyObject* obj)
1884{
1885	return (((PyObjCSelector*)obj)->sel_flags & PyObjCSelector_kREQUIRED) != 0;
1886}
1887
1888int   PyObjCSelector_IsClassMethod(PyObject* obj)
1889{
1890	return (PyObjCSelector_GetFlags(obj) & PyObjCSelector_kCLASS_METHOD) != 0;
1891}
1892
1893int   PyObjCSelector_IsHidden(PyObject* obj)
1894{
1895	return (PyObjCSelector_GetFlags(obj) & PyObjCSelector_kHIDDEN) != 0;
1896}
1897
1898int   PyObjCSelector_GetFlags(PyObject* obj)
1899{
1900	return ((PyObjCSelector*)obj)->sel_flags;
1901}
1902
1903
1904/*
1905 * Find the signature of 'selector' in the list of protocols.
1906 */
1907static const char*
1908find_protocol_signature(PyObject* protocols, SEL selector, int is_class_method)
1909{
1910	Py_ssize_t len;
1911	Py_ssize_t i;
1912	PyObject* proto;
1913	PyObject* info;
1914
1915	if (!PyList_Check(protocols)) {
1916		PyErr_Format(PyObjCExc_InternalError,
1917			"Protocol-list is not a 'list', but '%s'",
1918			Py_TYPE(protocols)->tp_name);
1919		return NULL;
1920	}
1921
1922	/* First try the explicit protocol definitions */
1923	len = PyList_GET_SIZE(protocols);
1924	for (i = 0; i < len; i++) {
1925		proto = PyList_GET_ITEM(protocols, i);
1926		if (proto == NULL) {
1927			PyErr_Clear();
1928			continue;
1929		}
1930
1931		if (PyObjCFormalProtocol_Check(proto)) {
1932			const char* signature;
1933
1934			signature = PyObjCFormalProtocol_FindSelectorSignature(
1935					proto, selector, is_class_method
1936			);
1937			if (signature != NULL) {
1938				return (char*)signature;
1939			}
1940		}
1941
1942		info = PyObjCInformalProtocol_FindSelector(proto, selector, is_class_method);
1943		if (info != NULL) {
1944			return PyObjCSelector_Signature(info);
1945		}
1946	}
1947
1948	/* Then check if another protocol users this selector */
1949	proto = PyObjCInformalProtocol_FindProtocol(selector);
1950	if (proto == NULL) {
1951		PyErr_Clear();
1952		return NULL;
1953	}
1954
1955	info = PyObjCInformalProtocol_FindSelector(proto, selector, is_class_method);
1956	if (info != NULL) {
1957		if (PyList_Append(protocols, proto) < 0) {
1958			return NULL;
1959		}
1960		Py_INCREF(proto);
1961		return PyObjCSelector_Signature(info);
1962	}
1963
1964	return NULL;
1965}
1966
1967PyObject*
1968PyObjCSelector_FromFunction(
1969	PyObject* pyname,
1970	PyObject* callable,
1971	PyObject* template_class,
1972	PyObject* protocols)
1973{
1974	SEL	  selector;
1975	Method    meth;
1976	int       is_class_method = 0;
1977	Class     oc_class = PyObjCClass_GetClass(template_class);
1978	PyObject* value;
1979	PyObject* super_sel;
1980
1981	if (oc_class == NULL) {
1982		return NULL;
1983	}
1984
1985
1986	if (PyObjCPythonSelector_Check(callable)) {
1987		PyObjCPythonSelector* result;
1988
1989		if (((PyObjCPythonSelector*)callable)->callable == NULL || ((PyObjCPythonSelector*)callable)->callable == Py_None) {
1990			PyErr_SetString(PyExc_ValueError, "selector object without callable");
1991			return NULL;
1992		}
1993		result = PyObject_New(PyObjCPythonSelector, &PyObjCPythonSelector_Type);
1994		result->sel_selector = ((PyObjCPythonSelector*)callable)->sel_selector;
1995		result->numoutput = ((PyObjCPythonSelector*)callable)->numoutput;
1996		result->argcount = ((PyObjCPythonSelector*)callable)->argcount;
1997		result->sel_methinfo = PyObjCSelector_GetMetadata(callable);
1998		Py_XINCREF(result->sel_methinfo);
1999		result->sel_class   = oc_class;
2000		result->sel_python_signature  = PyObjCUtil_Strdup(
2001				((PyObjCPythonSelector*)callable)->sel_python_signature);
2002		if (result->sel_python_signature == NULL) {
2003			Py_DECREF(result);
2004			return NULL;
2005		}
2006		result->sel_native_signature = NULL;
2007		result->sel_self       = NULL;
2008		result->sel_flags = ((PyObjCPythonSelector*)callable)->sel_flags;
2009		result->callable = ((PyObjCPythonSelector*)callable)->callable;
2010		if (result->callable) {
2011			Py_INCREF(result->callable);
2012		}
2013		if (PyObjCClass_HiddenSelector(template_class, PyObjCSelector_GetSelector(callable), PyObjCSelector_IsClassMethod(callable))) {
2014			((PyObjCSelector*)result)->sel_flags |= PyObjCSelector_kHIDDEN;
2015		}
2016		return (PyObject*)result;
2017	}
2018
2019	if (!PyFunction_Check(callable) && !PyMethod_Check(callable) &&
2020		(Py_TYPE(callable) != &PyClassMethod_Type)) {
2021
2022		PyErr_SetString(PyExc_TypeError,
2023				"expecting function, method or classmethod");
2024		return NULL;
2025	}
2026
2027
2028	if (Py_TYPE(callable) == &PyClassMethod_Type) {
2029		/*
2030		 * This is a 'classmethod' or 'staticmethod'. 'classmethods'
2031		 * will be converted to class 'selectors', 'staticmethods' are
2032		 * returned as-is.
2033		 */
2034		PyObject* tmp;
2035		is_class_method = 1;
2036		tmp = PyObject_CallMethod(callable, "__get__", "OO",
2037				Py_None, template_class);
2038		if (tmp == NULL) {
2039			return NULL;
2040		}
2041
2042		if (PyFunction_Check(tmp)) {
2043			/* A 'staticmethod', don't convert to a selector */
2044			Py_DECREF(tmp);
2045			Py_INCREF(callable);
2046			return callable;
2047		}
2048
2049#if PY_MAJOR_VERSION == 2
2050		callable = PyObject_GetAttrString(tmp, "im_func");
2051#else
2052		callable = PyObject_GetAttrString(tmp, "__func__");
2053#endif
2054		Py_DECREF(tmp);
2055		if (callable == NULL) {
2056			return NULL;
2057		}
2058	}
2059
2060	if (pyname == NULL) {
2061		/* No name specified, use the function name */
2062		pyname = PyObject_GetAttrString(callable, "__name__");
2063		if (pyname == NULL) {
2064			return NULL;
2065		}
2066		if (PyUnicode_Check(pyname)) {
2067			PyObject* bytes = PyUnicode_AsEncodedString(pyname, NULL, NULL);
2068			if (bytes == NULL) {
2069				return NULL;
2070			}
2071			selector = PyObjCSelector_DefaultSelector(
2072					PyBytes_AsString(bytes));
2073			Py_DECREF(bytes);
2074
2075#if PY_MAJOR_VERSION == 2
2076		} else if (PyString_Check(pyname)) {
2077			selector = PyObjCSelector_DefaultSelector(
2078					PyString_AsString(pyname));
2079#endif
2080		} else {
2081			PyErr_SetString(PyExc_TypeError, "Function name is not a string");
2082			return NULL;
2083		}
2084	} else if (PyUnicode_Check(pyname)) {
2085		PyObject* bytes = PyUnicode_AsEncodedString(pyname, NULL, NULL);
2086		if (bytes == NULL) {
2087			return NULL;
2088		}
2089		selector = PyObjCSelector_DefaultSelector(
2090				PyBytes_AsString(bytes));
2091		Py_DECREF(bytes);
2092
2093#if PY_MAJOR_VERSION == 2
2094	} else if (PyString_Check(pyname)) {
2095		selector = PyObjCSelector_DefaultSelector(
2096			PyString_AS_STRING(pyname));
2097#endif
2098	} else {
2099		PyErr_SetString(PyExc_TypeError,
2100			"method name must be a string");
2101		return NULL;
2102	}
2103
2104	/* XXX: This seriously fails if a class method has a different signature
2105	 * than an instance method of the same name!
2106	 *
2107	 *
2108	 * We eagerly call PyObjCClass_FindSelector because some ObjC
2109	 * classes are not fully initialized until they are actually used,
2110	 * and the code below doesn't seem to count but PyObjCClass_FindSelector
2111	 * is.
2112	 */
2113	super_sel = PyObjCClass_FindSelector(template_class, selector, is_class_method);
2114	if (super_sel == NULL) {
2115		PyErr_Clear();
2116	}
2117
2118	if (is_class_method) {
2119		meth = class_getClassMethod(oc_class, selector);
2120	} else {
2121		meth = class_getInstanceMethod(oc_class, selector);
2122
2123		if (!meth && !sel_isEqual(selector, @selector(copyWithZone:)) && !sel_isEqual(selector, @selector(mutableCopyWithZone:))) {
2124		        /* Look for a classmethod, but don't do that for copyWithZone:
2125			 * because that method is commonly defined in Python, and
2126			 * overriding "NSObject +copyWithZone:" is almost certainly
2127			 * not the intended behaviour.
2128			 */
2129			meth = class_getClassMethod(oc_class, selector);
2130			if (meth) {
2131				is_class_method = 1;
2132			}
2133		}
2134	}
2135
2136	if (meth) {
2137		/* The function overrides a method in the
2138		 * objective-C class.
2139		 *
2140		 * Get the signature through the python wrapper,
2141		 * the user may have specified a more exact
2142		 * signature!
2143		 */
2144		const char* typestr = NULL;
2145
2146		if (super_sel == NULL) {
2147			/* FIXME: This isn't optimal when hiding methods with non-standard types */
2148			PyObject* met = PyObjCClass_HiddenSelector(template_class, selector, is_class_method);
2149			if (met == NULL) {
2150				typestr = method_getTypeEncoding(meth);
2151			} else {
2152				typestr = ((PyObjCMethodSignature*)met)->signature;
2153			}
2154		} else {
2155			typestr = PyObjCSelector_Signature(super_sel);
2156		}
2157
2158		value = PyObjCSelector_New(
2159			callable,
2160			selector,
2161			typestr,
2162			is_class_method,
2163			oc_class);
2164		Py_XDECREF(super_sel);
2165	} else {
2166		const char* signature = NULL;
2167
2168		PyErr_Clear(); /* The call to PyObjCClass_FindSelector failed */
2169		if (protocols != NULL) {
2170			signature = find_protocol_signature(
2171					protocols, selector, is_class_method);
2172			if (signature == NULL && PyErr_Occurred()) {
2173				return NULL;
2174			}
2175		}
2176
2177		value = PyObjCSelector_New(
2178			callable,
2179			selector,
2180			signature,
2181			is_class_method,
2182			oc_class);
2183	}
2184	if (PyObjCClass_HiddenSelector(template_class, selector, PyObjCSelector_IsClassMethod(value))) {
2185		((PyObjCSelector*)value)->sel_flags |= PyObjCSelector_kHIDDEN;
2186	}
2187
2188	return value;
2189}
2190
2191PyObject* PyObjCSelector_Copy(PyObject* selector)
2192{
2193	if (PyObjCPythonSelector_Check(selector)) {
2194		return pysel_descr_get(selector, NULL, NULL);
2195	} else if (PyObjCNativeSelector_Check(selector)) {
2196		return objcsel_descr_get(selector, NULL, NULL);
2197	} else {
2198		PyErr_SetString(PyExc_TypeError, "copy non-selector");
2199		return NULL;
2200	}
2201}
2202