Source

pyobjc / pyobjc-core / Modules / objc / super-call.m

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * super-call.m -- Finding the right dispatch function for a method-call
 *
 * This file defines a registry that is used to find the right function to
 * call when a method is called. There are 3 variants:
 * - Call the method from python
 * - Call the superclass implementation from python
 * - Call the python implementation of a method from Objective-C
 *
 * The first will almost always be 'execute_and_pythonify_method', the other
 * two classes contain lots of functions because the normal runtime doesn't
 * allow for dynamicly creating these types of calls (see also: register.m)
 */
#include "pyobjc.h"

Py_ssize_t PyObjC_MappingCount = 0;

struct registry
{
	PyObjC_CallFunc 	call_to_objc;
	PyObjCFFI_ClosureFunc	call_to_python;
};
	
/* Dict mapping from signature-string to a 'struct registry' */
static PyObject* signature_registry = NULL;

/* List of 3-tuples: (Class, "selector", 'struct registry' */
static PyObject* special_registry  = NULL;

/*
 * Initialize the data structures
 */
static int
init_registry(void)
{
	if (signature_registry == NULL) {
		signature_registry = PyDict_New();
		if (signature_registry == NULL) return -1;
	}

	if (special_registry == NULL) {
		special_registry = PyList_New(0);
		if (special_registry == NULL) return -1;
	}

	return 0;
}

#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7

static void memblock_capsule_cleanup(void* ptr)
{
	PyMem_Free(ptr);
}
#else
static void memblock_capsule_cleanup(PyObject* ptr)
{
	PyMem_Free(PyCapsule_GetPointer(ptr, "objc.__memblock__"));
}
#endif


/*
 * Add a custom mapping for a method in a class
 */
int 
PyObjC_RegisterMethodMapping(Class class, SEL sel, 
	PyObjC_CallFunc call_to_objc,
	PyObjCFFI_ClosureFunc call_to_python)
{
	struct registry* v;
	const char* selname = sel_getName(sel);
	PyObject* pyclass;
	PyObject* entry;

	if (signature_registry == NULL) {
		if (init_registry() < 0) return -1;
	}

	if (!call_to_python) {
		PyErr_SetString(PyObjCExc_Error, 
			"PyObjC_RegisterMethodMapping: all functions required");
		return -1;
	}

	if (!call_to_objc) {
		call_to_objc = PyObjCFFI_Caller;
	}

	if (class == nil) {
		pyclass = Py_None; Py_INCREF(Py_None);
	} else {	
		pyclass = PyObjCClass_New(class);
		if (pyclass == NULL) return -1;
	}

	v = PyMem_Malloc(sizeof(*v));
	if (v == NULL) {
		PyErr_NoMemory();
		return -1;
	}
	v->call_to_objc  = call_to_objc;
	v->call_to_python = call_to_python;

	entry = PyTuple_New(3);
	if (entry == NULL) return -1;

	PyTuple_SET_ITEM(entry, 0, pyclass);
	PyTuple_SET_ITEM(entry, 1, PyBytes_InternFromString((char*)selname));
	PyTuple_SET_ITEM(entry, 2, PyCapsule_New(v, "objc.__memblock__", memblock_capsule_cleanup));

	if (PyErr_Occurred()) {
		Py_DECREF(entry);
		return -1;
	}

	if (PyList_Append(special_registry, entry) < 0) {
		Py_DECREF(entry);
		return -1;
	}

	PyObjC_MappingCount += 1;

	return 0;
}

int PyObjC_RegisterSignatureMapping(
	char* signature,
	PyObjC_CallFunc call_to_objc,
	PyObjCFFI_ClosureFunc call_to_python)
{
	struct registry* v;
	PyObject*  entry;
	char signature_buf[1024];
	int r;

	if (special_registry == NULL) {
		if (init_registry() < 0) return -1;
	}

	r = PyObjCRT_SimplifySignature(
			signature, 
			signature_buf, 
			sizeof(signature_buf));
	if (r == -1) {
		PyErr_SetString(PyObjCExc_Error, "cannot simplify signature");
		return -1;
	}	

	if (!call_to_objc || !call_to_python) {
		PyErr_SetString(PyObjCExc_Error, 
		   "PyObjC_RegisterSignatureMapping: all functions required");
		return -1;
	}

	v = PyMem_Malloc(sizeof(*v));
	if (v == NULL) {
		PyErr_NoMemory();
		return -1;
	}
	v->call_to_objc  = call_to_objc;
	v->call_to_python = call_to_python;

	entry = PyCapsule_New(v, "objc.__memblock__", memblock_capsule_cleanup);
	if (entry == NULL) {
		PyMem_Free(v);
		return -1;
	}

	PyObject* key = PyBytes_FromString(signature_buf);
	if (key == NULL) {
		return -1;
	}

	if (PyDict_SetItem(signature_registry, key, entry) < 0){
		Py_DECREF(key);
		Py_DECREF(entry);
		return -1;
	}
	Py_DECREF(key);
	Py_DECREF(entry); 
	PyObjC_MappingCount += 1;

	return 0;
}


static struct registry*
search_special(Class class, SEL sel)
{
	PyObject* result = NULL;
	PyObject* special_class = NULL;
	Py_ssize_t special_len;
	Py_ssize_t i;

	if (special_registry == NULL) goto error;

	if (class) {
		special_class = PyObjCClass_New(class);
		if (special_class == NULL) goto error;

	}

	special_len = PyList_Size(special_registry);

	for (i = 0; i < special_len; i++) {
		PyObject* entry = PyList_GetItem(special_registry, i);
		PyObject* pyclass = PyTuple_GET_ITEM(entry, 0);
		PyObject* pysel = PyTuple_GET_ITEM(entry, 1);

		if (pyclass == NULL || pysel == NULL) continue;
		
		if (
			(PyUnicode_Check(pysel) && PyObjC_is_ascii_string(pysel, sel_getName(sel)))
#if PY_MAJOR_VERSION == 2
		     || (PyString_Check(pysel) && strcmp(PyString_AsString(pysel), sel_getName(sel)) == 0)
#else
		     || (PyBytes_Check(pysel) && strcmp(PyBytes_AsString(pysel), sel_getName(sel)) == 0)
#endif
		     ) {

			if (!special_class) {
				special_class = pyclass;
				Py_INCREF(special_class);
				result = PyTuple_GET_ITEM(entry, 2);

			} else if (pyclass == Py_None) {
				Py_DECREF(special_class);
				special_class = pyclass;
				Py_INCREF(special_class);
				result = PyTuple_GET_ITEM(entry, 2);

			} else if (PyType_IsSubtype(
					(PyTypeObject*)special_class,
					(PyTypeObject*)pyclass
				    )) {
				Py_DECREF(special_class);
				special_class = pyclass;
				Py_INCREF(special_class);
				result = PyTuple_GET_ITEM(entry, 2);
			}
		}
	}
	Py_XDECREF(special_class);
	if (!result) goto error;

	return PyCapsule_GetPointer(result, "objc.__memblock__");

error:
	PyErr_Format(PyObjCExc_Error,
		"PyObjC: don't know how to call method '%s'", 
			sel_getName(sel));
	return NULL;
}


PyObjC_CallFunc 
PyObjC_FindCallFunc(Class class, SEL sel)
{
/*
 * TODO: Should add special case code for NSUndoManager: If this
 * is a selector that is forwarded to the 'target' we should get
 * the caller for the target instead of for this object!
 */
	struct registry* special;

	if (special_registry == NULL) return PyObjCFFI_Caller;

	special = search_special(class, sel);
	if (special) {
		return special->call_to_objc;
	} else {
		PyErr_Clear();
	}

	return PyObjCFFI_Caller;
}

static struct registry*
find_signature(const char* signature)
{
	PyObject* o;
	struct registry* r;
	char signature_buf[1024];
	int res;

	res = PyObjCRT_SimplifySignature(
			signature, 
			signature_buf, 
			sizeof(signature_buf));
	if (res == -1) {
		PyErr_SetString(PyObjCExc_Error, "cannot simplify signature");
		return NULL;
	}	

	if (signature_registry == NULL) goto error;

	PyObject* key = PyBytes_FromString(signature_buf);
	if (key == NULL) {
		return NULL;
	}
	o = PyDict_GetItem(signature_registry, key);
	Py_DECREF(key);
	if (o == NULL) goto error;

	r = PyCapsule_GetPointer(o, "objc.__memblock__");
	return r;

error:
	PyErr_Format(PyObjCExc_Error,
		"PyObjC: don't know how to call a method with "
		"signature '%s'", signature);
	return NULL;
}

extern IMP 
PyObjC_MakeIMP(Class class, Class super_class, PyObject* sel, PyObject* imp)
{
	struct registry* generic;
	struct registry* special;
	SEL aSelector = PyObjCSelector_GetSelector(sel);
	PyObjCFFI_ClosureFunc func = NULL;
	IMP retval;
	PyObjCMethodSignature* methinfo;

	if (super_class != nil) {
		special = search_special(super_class, aSelector);
		if (special) {
			func = special->call_to_python;
		} else {
			PyErr_Clear();
		}
	}

	if (func == NULL) {
		generic = find_signature(PyObjCSelector_Signature(sel));
		if (generic != NULL) {
			func = generic->call_to_python;
		} 
	}

	if (func == PyObjCUnsupportedMethod_IMP) {
		PyErr_Format(PyExc_TypeError,
			"Implementing %s in Python is not supported",
			sel_getName(aSelector));
		return NULL;
	}

	if (func != NULL) {
		methinfo = PyObjCMethodSignature_ForSelector(
				class, (PyObjCSelector_GetFlags(sel) & PyObjCSelector_kCLASS_METHOD) != 0,
				PyObjCSelector_GetSelector(sel),
				PyObjCSelector_Signature(sel),
				PyObjCNativeSelector_Check(sel));
		if (methinfo == NULL) {
			return NULL;
		}
		retval = PyObjCFFI_MakeClosure(methinfo, func, imp);
		if (retval != NULL) {
			Py_INCREF(imp);
		}
		Py_DECREF(methinfo);
		return retval;
	} else {
		PyErr_Clear();
		methinfo = PyObjCMethodSignature_ForSelector(
				class, (PyObjCSelector_GetFlags(sel) & PyObjCSelector_kCLASS_METHOD) != 0,
				PyObjCSelector_GetSelector(sel),
				PyObjCSelector_Signature(sel),
				PyObjCNativeSelector_Check(sel));
		if (methinfo == NULL) {
			return NULL;
		}
		retval = PyObjCFFI_MakeIMPForSignature(
				methinfo, PyObjCSelector_GetSelector(sel), imp);
		Py_DECREF(methinfo);
		return retval;
	}
}

void  
PyObjCUnsupportedMethod_IMP(
	ffi_cif* cif __attribute__((__unused__)), 
	void* resp __attribute__((__unused__)), 
	void** args, 
	void* userdata __attribute__((__unused__)))
{
	[NSException raise:NSInvalidArgumentException
		format:@"Implementing %s from Python is not supported for %@",
			sel_getName(*(SEL*)args[1]),
			*(id*)args[0]]; 
}

PyObject* 
PyObjCUnsupportedMethod_Caller(
	PyObject* meth, 
	PyObject* self, 
	PyObject* args __attribute__((__unused__)))
{
#if PY_MAJOR_VERSION == 2
	PyObject* repr;

	repr = PyObject_Repr(self);
	if (repr == NULL || !PyString_Check(repr)) {
		Py_XDECREF(repr);
		PyErr_Format(PyExc_TypeError,
			"Cannot call '%s' on instances of '%s' from Python",
			sel_getName(PyObjCSelector_GetSelector(meth)),
			Py_TYPE(self)->tp_name);
		return NULL;
	}

	PyErr_Format(PyExc_TypeError,
		"Cannot call '%s' on '%s' from Python",
		sel_getName(PyObjCSelector_GetSelector(meth)),
		PyString_AS_STRING(repr));
	Py_DECREF(repr);
	return NULL;
#else
	PyErr_Format(PyExc_TypeError,
		"Cannot call '%s' on '%R' from Python",
		sel_getName(PyObjCSelector_GetSelector(meth)),
		self);
	return NULL;
#endif

}