1/*
2 * The module entry point for ``_objc``. This file contains ``init_objc``
3 * and the implementation of a number of exported functions.
4 */
5#include "pyobjc.h"
6#include "OC_NSBundleHack.h"
7#include <objc/Protocol.h>
8#include <objc/objc-sync.h>
9
10#include <stddef.h>
11#include <ctype.h>
12#include <sys/socket.h>
13#include <netinet/in.h>
14
15#import <Foundation/NSAutoreleasePool.h>
16#import <Foundation/NSBundle.h>
17#import <Foundation/NSProcessInfo.h>
18#import <Foundation/NSString.h>
19
20#import <mach-o/dyld.h>
21#import <mach-o/getsect.h>
22#import <mach-o/loader.h>
23#import <objc/Protocol.h>
24
25#include <dlfcn.h>
26
27
28int PyObjC_VerboseLevel = 0;
29int PyObjC_HideProtected = 1;
30BOOL PyObjC_useKVO = YES;
31BOOL PyObjC_nativeProperties = NO;
32
33PyObject* PyObjCClass_DefaultModule = NULL;
34PyObject* PyObjC_NSNumberWrapper = NULL;
35#if PY_MAJOR_VERSION == 2
36PyObject* PyObjCStrBridgeWarning = NULL;
37int PyObjC_StrBridgeEnabled = 1;
38#endif
39
40
41PyObject* PyObjC_TypeStr2CFTypeID = NULL;
42
43static NSAutoreleasePool* global_release_pool = nil;
44
45@interface OC_NSAutoreleasePoolCollector: NSObject
46  /*
47   * This class is used to automaticly reset the
48   * global pool when an outer autorelease pool is
49   * recycled. This avoids problems when a python
50   * plugin is loaded in an Objective-C program.
51   */
52{}
53+(void)newAutoreleasePool;
54+(void)targetForBecomingMultiThreaded:(id)sender;
55@end
56@implementation OC_NSAutoreleasePoolCollector
57+(void)newAutoreleasePool
58{
59	self = [[self alloc] init];
60	global_release_pool = [[NSAutoreleasePool alloc] init];
61	(void)[self autorelease];
62}
63
64-(void)dealloc
65{
66	global_release_pool = nil;
67	[super dealloc];
68}
69
70+(void)targetForBecomingMultiThreaded:(id)sender
71{
72    [sender self];
73}
74
75@end
76
77PyDoc_STRVAR(pyobjc_id_doc,
78  "pyobjc_id(obj) -> int\n"
79  "\n"
80  "Return the id of the underlying NSObject as an int."
81);
82
83static PyObject*
84pyobjc_id(PyObject* self __attribute__((__unused__)), PyObject* args,
85PyObject *kwds)
86{
87	static char* keywords[] = { "obj", NULL };
88	PyObject *o;
89	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O",
90		keywords, &o)) {
91		return NULL;
92	}
93	if (!PyObjCObject_Check(o)) {
94		PyErr_SetString(PyExc_TypeError, "not an Objective-C object");
95		return NULL;
96	}
97	return PyInt_FromLong((long)PyObjCObject_GetObject(o));
98}
99
100
101PyDoc_STRVAR(repythonify_doc,
102  "repythonify(obj, type='@') -> object\n"
103  "\n"
104  "Put an object through the bridge by calling \n"
105  "depythonify_c_value then pythonify_c_value.\n"
106  "This is for internal use only."
107);
108
109static PyObject*
110repythonify(PyObject* self __attribute__((__unused__)), PyObject* args,
111PyObject *kwds)
112{
113	static char* keywords[] = { "obj", "type", NULL };
114	const char *type = "@";
115	PyObject *rval;
116	void *datum;
117	Py_ssize_t size;
118	PyObject *o;
119	if (!PyArg_ParseTupleAndKeywords(args, kwds,
120				"O|"Py_ARG_BYTES,
121		keywords, &o, &type)) {
122		return NULL;
123	}
124	size = PyObjCRT_SizeOfType(type);
125	if (size < 1) {
126		PyErr_SetString(PyExc_ValueError, "Can not calculate size for type");
127		return NULL;
128	}
129	datum = PyMem_Malloc(size);
130	if (datum == NULL) {
131		return PyErr_NoMemory();
132	}
133	if (depythonify_c_value(type, o, datum)) {
134		PyMem_Free(datum);
135		return NULL;
136	}
137	rval = pythonify_c_value(type, datum);
138	PyMem_Free(datum);
139	return rval;
140}
141
142#if PY_MAJOR_VERSION == 2
143PyDoc_STRVAR(setStrBridgeEnabled_doc,
144  "setStrBridgeEnabled(bool)\n"
145  "\n"
146  "False disables the transparent str bridge (not default) \n"
147  "True enables the transparent str bridge\n");
148
149static PyObject*
150setStrBridgeEnabled(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
151{
152	static char* keywords[] = { "enabled", NULL };
153	PyObject *o;
154	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:setStrBridgeEnabled",
155		keywords, &o)) {
156		return NULL;
157	}
158	PyObjC_StrBridgeEnabled = PyObject_IsTrue(o);
159	Py_INCREF(Py_None);
160	return Py_None;
161}
162
163PyDoc_STRVAR(getStrBridgeEnabled_doc,
164	"getStrBridgeEnabled() -> bool\n"
165	"\n"
166	"Return the status of the transparent str bridge.");
167
168static PyObject*
169getStrBridgeEnabled(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
170{
171	static char* keywords[] = { NULL };
172
173	if (!PyArg_ParseTupleAndKeywords(args, kwds, ":getStrBridgeEnabled",
174			keywords)) {
175		return NULL;
176	}
177
178	return PyBool_FromLong(PyObjC_StrBridgeEnabled);
179}
180#endif /* !Py3k */
181
182PyDoc_STRVAR(lookUpClass_doc,
183  "lookUpClass(class_name) -> class\n"
184  "\n"
185  "Search for the named classes in the Objective-C runtime and return it.\n"
186  "Raises noclass_error when the class doesn't exist.");
187
188static PyObject*
189lookUpClass(PyObject* self __attribute__((__unused__)),
190	PyObject* args, PyObject* kwds)
191{
192	static 	char* keywords[] = { "class_name", NULL };
193	char* class_name = NULL;
194	Class objc_class;
195
196	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s",
197			keywords, &class_name)) {
198		return NULL;
199	}
200
201	objc_class = objc_lookUpClass(class_name);
202	if (objc_class == NULL) {
203		PyErr_SetString(PyObjCExc_NoSuchClassError, class_name);
204		return NULL;
205	}
206	return PyObjCClass_New(objc_class);
207}
208
209
210PyDoc_STRVAR(classAddMethods_doc,
211	 "classAddMethods(targetClass, methodsArray)\n"
212	 "\n"
213	 "Adds methods in methodsArray to class. The effect is similar to how \n"
214	 "categories work. If class already implements a method as defined in \n"
215	 "methodsArray, the original implementation will be replaced by the \n"
216	 "implementation from methodsArray.");
217
218static PyObject*
219classAddMethods(PyObject* self __attribute__((__unused__)),
220	PyObject* args, PyObject* keywds)
221{
222	static 	char* kwlist[] = { "targetClass", "methodsArray", NULL };
223	PyObject* classObject = NULL;
224	PyObject* methodsArray = NULL;
225
226	if (!PyArg_ParseTupleAndKeywords(args, keywds,
227			"OO:classAddMethods", kwlist,
228			&classObject, &methodsArray)) {
229		return NULL;
230	}
231
232	if (!PyObjCClass_Check(classObject)) {
233		PyErr_SetString(PyExc_TypeError, "base class is not an Objective-C class");
234		return NULL;
235	}
236
237	methodsArray = PySequence_Fast(
238			methodsArray, "methodsArray must be a sequence");
239	if (methodsArray == NULL) return NULL;
240
241	int r = PyObjCClass_AddMethods(classObject,
242			PySequence_Fast_ITEMS(methodsArray),
243			PySequence_Fast_GET_SIZE(methodsArray));
244	Py_DECREF(methodsArray);
245
246	if (r == -1) {
247		return NULL;
248	}
249
250	Py_INCREF(Py_None);
251	return Py_None;
252}
253
254
255
256PyDoc_STRVAR(remove_autorelease_pool_doc,
257  "removeAutoreleasePool()\n"
258  "\n"
259  "This removes the global NSAutoreleasePool.  You should do this\n"
260  "at the end of a plugin's initialization script.\n");
261static PyObject*
262remove_autorelease_pool(PyObject* self __attribute__((__unused__)),
263	PyObject* args, PyObject* kwds)
264{
265	static char* keywords[] = { NULL };
266	if (!PyArg_ParseTupleAndKeywords(args, kwds, "", keywords)) {
267		return NULL;
268	}
269
270	PyObjC_DURING
271		[global_release_pool release];
272		global_release_pool = nil;
273	PyObjC_HANDLER
274		PyObjCErr_FromObjC(localException);
275	PyObjC_ENDHANDLER
276
277	if (PyErr_Occurred()) return NULL;
278
279	Py_INCREF(Py_None);
280	return Py_None;
281}
282
283PyDoc_STRVAR(recycle_autorelease_pool_doc,
284  "recycleAutoreleasePool()\n"
285  "\n"
286  "This 'releases' the global autorelease pool and creates a new one.\n"
287  "This method is for system use only\n");
288static PyObject*
289recycle_autorelease_pool(PyObject* self __attribute__((__unused__)),
290	PyObject* args, PyObject* kwds)
291{
292	static	char* keywords[] = { NULL };
293
294	if (!PyArg_ParseTupleAndKeywords(args, kwds, "", keywords)) {
295		return NULL;
296	}
297
298	if (global_release_pool != NULL) {
299
300		PyObjC_DURING
301			[global_release_pool release];
302			[OC_NSAutoreleasePoolCollector newAutoreleasePool];
303		PyObjC_HANDLER
304			PyObjCErr_FromObjC(localException);
305		PyObjC_ENDHANDLER
306
307		if (PyErr_Occurred()) return NULL;
308	}
309
310	Py_INCREF(Py_None);
311	return Py_None;
312}
313
314PyDoc_STRVAR(set_class_extender_doc,
315	"setClassExtender(func) -> None\n"
316	"\n"
317	"Register a function that will be called to update the class\n"
318	"dict of new Objective-C classes and class-proxies. This will\n"
319	"replace any existing callback.\n"
320	"The function will be called like this:\n"
321	"\tclass_extender(superclass, class_name, class_dict)\n"
322	"superclass:\n"
323	"  The superclass for the new class, or None if this is the top of\n"
324	"  a class hierarchy.\n"
325	"class_name:\n"
326	"  Name of the new class\n"
327	"class_dict:\n"
328	"  The proposed class dictionary. The callback is supposed to update\n"
329	"  this dictionary.\n"
330	"");
331static PyObject*
332set_class_extender(PyObject* self __attribute__((__unused__)),
333	PyObject* args, PyObject* kwds)
334{
335static 	char* keywords[] = { "callback", NULL };
336	PyObject* callback;
337
338	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:setClassExtender",
339			keywords, &callback)) {
340		return NULL;
341	}
342
343	if (!PyCallable_Check(callback)) {
344		PyErr_SetString(PyExc_TypeError, "Expecting callable");
345		return NULL;
346	}
347
348	Py_INCREF(callback);
349	Py_XDECREF(PyObjC_ClassExtender);
350	PyObjC_ClassExtender = callback;
351
352	Py_INCREF(Py_None);
353	return Py_None;
354}
355
356
357PyDoc_STRVAR(getClassList_doc,
358  "getClassList() -> [ cls, ...] n"
359  "\n"
360  "Return a list with all Objective-C classes known to the runtime.\n"
361);
362static PyObject*
363getClassList(PyObject* self __attribute__((__unused__)))
364{
365	return PyObjC_GetClassList();
366}
367
368PyDoc_STRVAR(set_signature_for_selector_doc,
369	"setSignatureForSelector(class_name, selector, signature) -> None\n"
370	"\n"
371	"Register a replacement signature for a specific selector. This \n"
372	"can be used to provide a more exact signature for a method.\n"
373	"\n"
374	"This function is deprecated, use the new metadata machinery.\n"
375	"");
376static PyObject*
377set_signature_for_selector(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
378{
379static 	char* keywords[] = { "class_name", "selector", "signature", NULL };
380	char* class_name;
381	char* selector;
382	char* signature;
383	SEL   sel;
384
385	if (!PyArg_ParseTupleAndKeywords(args, kwds, "sss:setSignatureForSelector",
386			keywords, &class_name, &selector, &signature)) {
387		return NULL;
388	}
389
390	if (PyErr_WarnEx(PyExc_DeprecationWarning,
391		"Use the new metadata machinery", 1) < 0) {
392
393		return NULL;
394	}
395
396	sel = sel_getUid(selector);
397
398	if (ObjC_SignatureForSelector(class_name, sel, signature) < 0) {
399		return NULL;
400	}
401
402	Py_INCREF(Py_None);
403	return Py_None;
404}
405
406PyDoc_STRVAR(setNSNumberWrapper_doc,
407	"_setNSNumberWrapper(wrapper) -> None\n"
408	"\n"
409	"Set the NSNumber wrapper function to the new value."
410);
411static PyObject*
412setNSNumberWrapper(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
413{
414static 	char* keywords[] = { "wrapper", NULL };
415	PyObject* o;
416
417	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", keywords, &o)) {
418		return NULL;
419	}
420
421	Py_XDECREF(PyObjC_NSNumberWrapper);
422	Py_INCREF(o);
423	PyObjC_NSNumberWrapper = o;
424
425	Py_INCREF(Py_None);
426	return Py_None;
427}
428
429PyDoc_STRVAR(getNSNumberWrapper_doc,
430	"_getNSNumberWrapper() -> wrapper\n"
431	"\n"
432	"Get the current NSNumber wrapper function."
433);
434static PyObject*
435getNSNumberWrapper(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
436{
437static 	char* keywords[] = { NULL };
438
439	if (!PyArg_ParseTupleAndKeywords(args, kwds, "",
440			keywords)) {
441		return NULL;
442	}
443
444	if (PyObjC_NSNumberWrapper == NULL) {
445		Py_INCREF(Py_None);
446		return Py_None;
447	}
448	Py_INCREF(PyObjC_NSNumberWrapper);
449	return PyObjC_NSNumberWrapper;
450}
451
452PyDoc_STRVAR(setHideProtected_doc,
453	"setHideProtected(bool) -> None\n"
454	"\n"
455	"If true methods whose name starts with an underscore will not "
456	"visible for introspection using dir() or the class __dict__.");
457static PyObject*
458setHideProtected(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
459{
460static 	char* keywords[] = { "flag", NULL };
461	PyObject* o;
462
463	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O",
464			keywords, &o)) {
465		return NULL;
466	}
467
468	PyObjC_HideProtected = PyObject_IsTrue(o);
469
470	Py_INCREF(Py_None);
471	return Py_None;
472}
473
474
475PyDoc_STRVAR(setObjCPointerIsError_doc,
476	"setObjCPointerIsError(bool) -> None\n"
477	"\n"
478	"If the argument is True PyObjC will raise an exception when it tries to wrap a C pointer it doesn't know about."
479);
480static PyObject*
481setObjCPointerIsError(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
482{
483static 	char* keywords[] = { "value", NULL };
484	PyObject* o;
485
486	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O",
487			keywords, &o)) {
488		return NULL;
489	}
490
491	PyObjCPointer_RaiseException = PyObject_IsTrue(o);
492
493	Py_INCREF(Py_None);
494	return Py_None;
495}
496
497PyDoc_STRVAR(setVerbose_doc,
498	"setVerbose(bool) -> None\n"
499	"\n"
500	"Set verbosity to the new value."
501);
502static PyObject*
503setVerbose(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
504{
505static 	char* keywords[] = { "level", NULL };
506	PyObject* o;
507
508	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:setVerbose",
509			keywords, &o)) {
510		return NULL;
511	}
512
513	PyObjC_VerboseLevel = PyObject_IsTrue(o);
514
515	Py_INCREF(Py_None);
516	return Py_None;
517}
518
519PyDoc_STRVAR(setUseKVOForSetattr_doc,
520	"setUseKVOForSetattr(bool) -> bool\n"
521	"\n"
522	"Specify the default value for __useKVO__ on classes defined "
523	"after this call. Returns the previous value."
524);
525static PyObject*
526setUseKVOForSetattr(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
527{
528static 	char* keywords[] = { "value", NULL };
529	PyObject* o;
530
531	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", keywords, &o)) {
532		return NULL;
533	}
534
535	PyObject* result = PyBool_FromLong(PyObjC_useKVO);
536	PyObjC_useKVO = PyObject_IsTrue(o);
537
538	return result;
539}
540
541PyDoc_STRVAR(getVerbose_doc,
542	"getVerbose() -> bool\n"
543	"\n"
544	"Return the verbosity value."
545);
546static PyObject*
547getVerbose(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
548{
549static 	char* keywords[] = { NULL };
550
551	if (!PyArg_ParseTupleAndKeywords(args, kwds, ":getVerbose",
552			keywords)) {
553		return NULL;
554	}
555
556	return PyBool_FromLong(PyObjC_VerboseLevel);
557}
558
559PyDoc_STRVAR(getObjCPointerIsError_doc,
560	"getObjCPointerIsError() -> bool\n"
561	"\n"
562	"Returns True if PyObjC raises an exception when it tries to wrap a pointer it doesn't know about."
563);
564static PyObject*
565getObjCPointerIsError(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
566{
567static 	char* keywords[] = { NULL };
568
569	if (!PyArg_ParseTupleAndKeywords(args, kwds, "",
570			keywords)) {
571		return NULL;
572	}
573
574	return PyBool_FromLong(PyObjCPointer_RaiseException);
575}
576
577
578PyDoc_STRVAR(allocateBuffer_doc,
579	     "allocateBuffer(size) -> <r/w buffer>\n"
580	     "\n"
581	     "Allocate a buffer of memory of size. Buffer is \n"
582	     "read/write."
583	     );
584static PyObject*
585allocateBuffer(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
586{
587	static	char* keywords[] = { "length", 0 };
588	Py_ssize_t length;
589
590	if (!PyArg_ParseTupleAndKeywords(args, kwds, Py_ARG_SIZE_T,
591				keywords, &length)) {
592		return NULL;
593	}
594
595	if (length <= 0 ) {
596		PyErr_SetString(PyExc_ValueError,
597			"Length must be greater than 0.");
598		return NULL;
599	}
600
601#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 6
602	return PyBuffer_New(length);
603#else
604	return PyByteArray_FromStringAndSize(NULL, length);
605#endif
606}
607
608PyDoc_STRVAR(currentBundle_doc,
609	"currentBundle() -> bundle\n"
610	"\n"
611	"Get the current bundle during module initialization.\n"
612	"Works for plug-ins and applications.\n"
613	"\n"
614	"Note that this is the default bundle used by\n"
615	"NibClassBuilder.extractClasses(...),\n"
616	"so calling it explicitly is rarely useful.\n"
617	"After module initialization, use\n"
618	"NSBundle.bundleForClass_(ClassInYourBundle)."
619);
620static PyObject*
621currentBundle(PyObject* self __attribute__((__unused__)))
622{
623	id rval;
624	char *bundle_address = getenv("PYOBJC_BUNDLE_ADDRESS");
625	if (!(bundle_address && sscanf(bundle_address, "%p", &rval) == 1)) {
626		rval = [NSBundle mainBundle];
627	}
628	return pythonify_c_value(@encode(id), &rval);
629}
630
631
632PyDoc_STRVAR(loadBundle_doc,
633	"loadBundle(module_name, module_globals, bundle_path=None, "
634	"bundle_identifier=None, scan_classes=True) -> bundle\n"
635	"\n"
636	"Load the bundle identified by 'bundle_path' or 'bundle_identifier' \n"
637	"and add the classes in the bundle to the 'module_globals'.\n"
638	"If 'scan_classes' is False the function won't add classes to 'module_globals'"
639	"\n"
640	"If 'bundle_identifier' is specified the right bundle is located\n"
641	"using NSBundle's +bundleWithIdentifier:.\n"
642	"If 'bundle_path' is specified the right bundle is located using\n"
643	"NSBundle's +bundleWithPath:. The path must be an absolute pathname\n"
644);
645static PyObject*
646loadBundle(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
647{
648static  char* keywords[] = { "module_name", "module_globals", "bundle_path", "bundle_identifier", "scan_classes", NULL };
649static	Py_ssize_t	curClassCount = -1;
650	NSBundle* bundle = nil;
651	id bundle_identifier = nil;
652	id bundle_path = nil;
653	PyObject* module_name;
654	PyObject* module_globals;
655	PyObject* class_list;
656	Py_ssize_t len, i;
657	PyObject* scanClasses = NULL;
658
659	if (!PyArg_ParseTupleAndKeywords(args, kwds,
660#if PY_MAJOR_VERSION == 2
661			"SO|O&O&O",
662#else
663			"UO|O&O&O",
664#endif
665			keywords, &module_name, &module_globals,
666			PyObjCObject_Convert, &bundle_path, PyObjCObject_Convert, &bundle_identifier, &scanClasses)) {
667		return NULL;
668	}
669
670	if (!bundle_path && !bundle_identifier) {
671		PyErr_SetString(PyExc_ValueError,
672			"Need to specify either bundle_path or "
673			"bundle_identifier");
674		return NULL;
675	}
676	if (bundle_path && bundle_identifier) {
677		PyErr_SetString(PyExc_ValueError,
678			"Need to specify either bundle_path or "
679			"bundle_identifier");
680		return NULL;
681	}
682
683	if (bundle_path) {
684		if (![bundle_path isKindOfClass:[NSString class]]) {
685			PyErr_SetString(PyExc_TypeError,
686					"bundle_path is not a string");
687			return NULL;
688		}
689		bundle = [NSBundle bundleWithPath:bundle_path];
690	} else {
691		if (![bundle_identifier isKindOfClass:[NSString class]]) {
692			PyErr_SetString(PyExc_TypeError,
693					"bundle_identifier is not a string");
694			return NULL;
695		}
696		bundle = [NSBundle bundleWithIdentifier:bundle_identifier];
697	}
698
699	if (![bundle load]) {
700		PyErr_SetString(PyExc_ImportError,
701			"Bundle could not be loaded");
702		return NULL;
703	}
704
705	/*
706	 * Scanning the class list is expensive and something to be avoided
707	 * when possible.
708	 */
709
710	if (scanClasses != NULL && !PyObject_IsTrue(scanClasses)) {
711		return pythonify_c_value(@encode(NSBundle*), &bundle);
712	}
713
714	class_list = PyObjC_GetClassList();
715	if (class_list == NULL) {
716		return NULL;
717	}
718
719	curClassCount = len = PyTuple_GET_SIZE(class_list);
720	for (i = 0; i < len; i++) {
721		PyObject* item;
722		const char*  nm;
723
724		item = PyTuple_GET_ITEM(class_list, i);
725		if (item == NULL) {
726			continue;
727		}
728
729		nm = ((PyTypeObject*)item)->tp_name;
730
731		if (nm[0] == '%') {
732			/* skip, posed-as type */
733		} else if (PyObjC_HideProtected && nm[0] == '_') {
734			/* Skip private classes */
735		} else if (strcmp(nm, "Object") == 0
736				|| strcmp(nm, "List") == 0
737				|| strcmp(nm, "Protocol") == 0) {
738			/* skip, these have been deprecated since OpenStep! */
739		} else if (PyDict_SetItemString(module_globals,
740				((PyTypeObject*)item)->tp_name, item) == -1) {
741			Py_DECREF(class_list); class_list = NULL;
742			return NULL;
743		}
744	}
745	Py_XDECREF(class_list); class_list = NULL;
746
747	return pythonify_c_value(@encode(NSBundle*), &bundle);
748}
749
750PyDoc_STRVAR(objc_splitSignature_doc,
751	"splitSignature(signature) -> list\n"
752	"\n"
753	"Split a signature string into a list of items."
754);
755static PyObject*
756objc_splitSignature(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
757{
758static  char* keywords[] = { "signature", NULL };
759	const char* signature;
760	const char* end;
761	PyObject* result;
762	PyObject* tuple;
763
764	if (!PyArg_ParseTupleAndKeywords(args, kwds,
765			Py_ARG_BYTES,
766			keywords, &signature)) {
767		return NULL;
768	}
769
770	result = PyList_New(0);
771	if (result == NULL) return NULL;
772
773	while (signature && *signature != 0) {
774		PyObject* str;
775		const char* t;
776
777		end = PyObjCRT_SkipTypeSpec(signature);
778		if (end == NULL) {
779			Py_DECREF(result);
780			return NULL;
781		}
782
783		t = end-1;
784		while (t != signature && isdigit(*t)) {
785			t --;
786		}
787		t ++;
788
789		str = PyBytes_FromStringAndSize(signature, t - signature);
790		if (str == NULL) {
791			Py_DECREF(result);
792			return NULL;
793		}
794
795		if (PyList_Append(result, str) == -1) {
796			Py_DECREF(str);
797			Py_DECREF(result);
798			return NULL;
799		}
800		Py_DECREF(str);
801
802		signature = end;
803	}
804
805	tuple = PyList_AsTuple(result);
806	Py_DECREF(result);
807	return tuple;
808}
809
810
811PyDoc_STRVAR(objc_splitStructSignature_doc,
812	"splitStructSignature(signature) -> structname, fields\n"
813	"\n"
814	"Split a struct signature string into a list of items."
815);
816static PyObject*
817objc_splitStructSignature(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
818{
819static  char* keywords[] = { "signature", NULL };
820	const char* signature;
821	const char* end;
822	PyObject* structname;
823	PyObject* fields;
824
825	if (!PyArg_ParseTupleAndKeywords(args, kwds,
826			Py_ARG_BYTES,
827			keywords, &signature)) {
828		return NULL;
829	}
830
831	if (signature[0] != _C_STRUCT_B) {
832		PyErr_SetString(PyExc_ValueError, "not a struct encoding");
833		return NULL;
834	}
835
836	signature += 1;
837	end = signature;
838	while (*end && *end != _C_STRUCT_E && *end++ != '=');
839	if (end == signature+1) {
840		structname = Py_None;
841		Py_INCREF(structname);
842	} else {
843		structname = PyText_FromStringAndSize(signature, end-signature-1);
844		if (structname == NULL) {
845			return NULL;
846		}
847	}
848	if (*end == '=') {
849		signature = end+1;
850	} else {
851		signature = end;
852	}
853
854	fields = PyList_New(0);
855	if (fields == NULL) return NULL;
856
857	while (signature && *signature != _C_STRUCT_E && *signature != 0) {
858		PyObject* str;
859		PyObject* item;
860		PyObject* name;
861		const char* t;
862
863		if (*signature == '"') {
864			signature ++;
865			end = signature;
866			while (*end && *end != '"') {
867				end ++;
868			}
869			name = PyText_FromStringAndSize(signature, end-signature);
870			if (name == NULL) {
871				Py_DECREF(structname);
872				Py_DECREF(fields);
873				return NULL;
874			}
875			signature = end + 1;
876		} else {
877			name = Py_None;
878			Py_INCREF(name);
879		}
880
881		end = PyObjCRT_SkipTypeSpec(signature);
882		if (end == NULL) {
883			Py_DECREF(structname);
884			Py_DECREF(name);
885			Py_DECREF(fields);
886			return NULL;
887		}
888
889		t = end-1;
890		while (t != signature && isdigit(*t)) {
891			t --;
892		}
893		t ++;
894
895		str = PyBytes_FromStringAndSize(signature, t - signature);
896		if (str == NULL) {
897			Py_DECREF(structname);
898			Py_DECREF(name);
899			Py_DECREF(fields);
900			return NULL;
901		}
902
903		item = Py_BuildValue("NN", name, str);
904		if (item == NULL) {
905			Py_DECREF(fields);
906			return NULL;
907		}
908
909		if (PyList_Append(fields, item) == -1) {
910			Py_DECREF(fields);
911			Py_DECREF(item);
912			Py_DECREF(structname);
913			return NULL;
914		}
915		Py_DECREF(item);
916
917		signature = end;
918	}
919	if (signature && *signature != _C_STRUCT_E) {
920		Py_DECREF(structname);
921		Py_DECREF(fields);
922		PyErr_SetString(PyExc_ValueError, "Value is not a complete struct signature");
923		return NULL;
924	}
925	if (signature && signature[1]) {
926		Py_DECREF(structname);
927		Py_DECREF(fields);
928		PyErr_SetString(PyExc_ValueError, "Additional text at end of signature");
929		return NULL;
930	}
931
932	return Py_BuildValue("NN", structname, fields);
933}
934
935PyDoc_STRVAR(PyObjC_loadBundleVariables_doc,
936	"loadBundleVariables(bundle, module_globals, variableInfo, "
937	"skip_undefined=True)\n"
938	"\n"
939	"Load the specified variables in the bundle. If skip_undefined is \n"
940	"True, variables that are not present in the bundle are skipped, \n"
941	"otherwise this method raises objc.error when a variable cannot be \n"
942	"found.\n"
943	"\n"
944	"variableInfo is a list of (name, type) pairs. The type is the \n"
945	"Objective-C type specifier for the variable type.");
946PyDoc_STRVAR(PyObjC_loadBundleFunctions_doc,
947	"loadBundleFunctions(bundle, module_globals, functionInfo, "
948	"skip_undefined=True)\n"
949	"\n"
950	"Load the specified functions in the bundle. If skip_undefined is \n"
951	"True, variables that are not present in the bundle are skipped, \n"
952	"otherwise this method raises objc.error when a variable cannot be \n"
953	"found.\n"
954	"\n"
955	"functionInfo is a list of (name, signature, doc [, methinfo]) triples. \n"
956	"The signature is the Objective-C type specifier for the function \n"
957	"signature.");
958PyDoc_STRVAR(PyObjC_loadFunctionList_doc,
959	"loadFunctionList(list, module_globals, functionInfo, "
960	"skip_undefined=True)\n"
961	"\n"
962	"Load the specified functions. List should be a capsule object containing\n"
963	"an array of { char*, funcion } structs.");
964
965
966
967#if PY_MAJOR_VERSION == 2
968PyDoc_STRVAR(objc_CFToObject_doc,
969	"CFToObject(cfObject) -> objCObject\n"
970	"\n"
971	"Convert a CoreFoundation object to an Objective-C object. \n"
972	"Raises an exception if the conversion fails"
973);
974static PyObject*
975objc_CFToObject(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
976{
977static  char* keywords[] = { "value", 0 };
978	PyObject* argument;
979	id	  res;
980
981	if (!PyArg_ParseTupleAndKeywords(args, kwds,
982			"O:CFToObject", keywords,
983			&argument)) {
984		return NULL;
985	}
986
987	if (PyErr_WarnEx(PyExc_DeprecationWarning,
988		"MacPython CF support is no longer supported and will be removed in PyObjC 3.0", 0) < 0) {
989		return NULL;
990	}
991
992
993	res = PyObjC_CFTypeToID(argument);
994	if (res == 0) {
995		PyErr_SetString(PyExc_TypeError, "not a CoreFoundation object");
996		return NULL;
997	}
998
999	return pythonify_c_value(@encode(id), &res);
1000}
1001
1002PyDoc_STRVAR(objc_ObjectToCF_doc,
1003	"ObjectToCF(objCObject) -> cfObject\n"
1004	"\n"
1005	"Convert an Objective-C object to a CoreFoundation object. \n"
1006	"Raises an exception if the conversion fails"
1007);
1008static PyObject*
1009objc_ObjectToCF(PyObject* self __attribute__((__unused__)),
1010	PyObject* args, PyObject* kwds)
1011{
1012static char* keywords[] = { "value", 0 };
1013	PyObject* argument;
1014
1015	if (!PyArg_ParseTupleAndKeywords(args, kwds,
1016			"O:ObjectToCF", keywords,
1017			&argument)) {
1018		return NULL;
1019	}
1020
1021	if (PyErr_WarnEx(PyExc_DeprecationWarning,
1022		"MacPython CF support is no longer supported and will be removed in PyObjC 3.0", 0) < 0) {
1023		return NULL;
1024	}
1025
1026	if (!PyObjCObject_Check(argument)) {
1027		PyErr_SetString(PyExc_TypeError, "not an Objective-C object");
1028		return NULL;
1029	}
1030
1031	return PyObjC_IDToCFType(PyObjCObject_GetObject(argument));
1032}
1033#endif /* PY_MAJOR_VERSION == 2 */
1034
1035
1036PyDoc_STRVAR(protocolsForProcess_doc,
1037	"protocolsForProcess() -> [Protocols]\n"
1038	"\n"
1039	"Returns a list of Protocol objects that are present in the process"
1040);
1041static PyObject*
1042protocolsForProcess(PyObject* self __attribute__((__unused__)))
1043{
1044	PyObject *protocols;
1045	Protocol** protlist;
1046	unsigned int protCount;
1047	unsigned int i;
1048
1049	protlist = objc_copyProtocolList(&protCount);
1050	if (protlist == NULL) {
1051		Py_INCREF(Py_None);
1052		return Py_None;
1053	}
1054
1055	protocols = PyList_New(protCount);
1056	if (protocols == NULL) {
1057		return NULL;
1058	}
1059	for (i = 0; i < protCount; i++) {
1060		PyObject *p = PyObjCFormalProtocol_ForProtocol(protlist[i]);
1061		if (p == NULL) {
1062			Py_DECREF(protocols);
1063			free(protlist);
1064			return NULL;
1065		}
1066		PyList_SET_ITEM(protocols, i, p);
1067	}
1068	free(protlist);
1069	return protocols;
1070}
1071
1072PyDoc_STRVAR(protocolNamed_doc,
1073	"_protocolNamed(name) -> Protocol\n");
1074static PyObject*
1075protocolNamed(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
1076{
1077static 	char* keywords[] = { "name", NULL };
1078	char* name;
1079	Protocol* p;
1080
1081	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", keywords, &name)) {
1082		return NULL;
1083	}
1084
1085	p = objc_getProtocol(name);
1086	if (p == NULL) {
1087		PyErr_SetString(PyExc_AttributeError, name);
1088		return NULL;
1089	}
1090	return PyObjCFormalProtocol_ForProtocol(p);
1091}
1092
1093
1094PyDoc_STRVAR(protocolsForClass_doc,
1095	"protocolsForClass(cls) -> [Protocols]\n"
1096	"\n"
1097	"Returns a list of Protocol objects that the class claims\n"
1098	"to implement directly."
1099);
1100static PyObject*
1101protocolsForClass(PyObject* self __attribute__((__unused__)),
1102		PyObject* args,
1103		PyObject* kwds)
1104{
1105	static char* keywords[] = { "cls", NULL };
1106	Protocol** protocol_list;
1107	unsigned int protocol_count, i;
1108	PyObject *protocols;
1109	Class cls;
1110	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:protocolsForClass", keywords,
1111			PyObjCClass_Convert, &cls)) {
1112		return NULL;
1113	}
1114	protocols = PyList_New(0);
1115	if (protocols == NULL) {
1116		return NULL;
1117	}
1118	protocol_list = class_copyProtocolList(cls, &protocol_count);
1119	for (i = 0; i < protocol_count; i++) {
1120		PyObject *protocol = PyObjCFormalProtocol_ForProtocol(protocol_list[i]);
1121		if (protocol == NULL) {
1122			free(protocol_list);
1123			Py_DECREF(protocols);
1124			return NULL;
1125		}
1126		PyList_Append(protocols, protocol);
1127		Py_DECREF(protocol);
1128	}
1129	free(protocol_list);
1130	return protocols;
1131}
1132
1133PyDoc_STRVAR(createOpaquePointerType_doc,
1134	"createOpaquePointerType(name, typestr, doc) -> type\n"
1135	"\n"
1136	"Return a wrapper type for opaque pointers of the given type. The type \n"
1137	"will be registered with PyObjC and will be used to wrap pointers of the \n"
1138	"given type."
1139);
1140static PyObject*
1141createOpaquePointerType(PyObject* self __attribute__((__unused__)),
1142		PyObject* args, PyObject* kwds)
1143{
1144static char* keywords[] = { "name", "typestr", "doc", NULL };
1145	char* name;
1146	char* typestr;
1147	char* docstr = NULL;
1148
1149	if (!PyArg_ParseTupleAndKeywords(args, kwds,
1150				"s"Py_ARG_BYTES"|z",
1151				keywords,
1152				&name, &typestr, &docstr)) {
1153		return NULL;
1154	}
1155
1156	return PyObjCCreateOpaquePointerType(name, typestr, docstr);
1157}
1158
1159PyDoc_STRVAR(registerMetaData_doc,
1160	"registerMetaDataForSelector(class, selector, metadata) -> None\n"
1161	"\n"
1162	"XXX: work out documentation.");
1163static PyObject*
1164registerMetaData(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
1165{
1166static char* keywords[] = { "class_", "selector", "metadata", NULL };
1167
1168	PyObject* class_name;
1169	PyObject* selector;
1170	PyObject* metadata;
1171
1172
1173	if (!PyArg_ParseTupleAndKeywords(args, kwds, "SSO", keywords,
1174			&class_name, &selector, &metadata)) {
1175		return NULL;
1176	}
1177
1178	if (PyObjC_registerMetaData(class_name, selector, metadata) < 0) {
1179		return NULL;
1180
1181	} else {
1182		Py_INCREF(Py_None);
1183		return Py_None;
1184	}
1185}
1186
1187PyDoc_STRVAR(registerStructAlias_doc,
1188	"registerStructAlias(typestr, structType)\n"
1189	"\n"
1190	"Registers 'typestr' as a type that should be mapped onto 'structType'\n"
1191	"'structType' must be created using 'createStructType' (or through \n"
1192	"a metadata file."
1193);
1194static PyObject*
1195registerStructAlias(PyObject* self __attribute__((__unused__)),
1196		                PyObject* args, PyObject* kwds)
1197{
1198	static char* keywords[] = { "typestr", "structType", NULL };
1199	char* typestr;
1200	PyObject* structType;
1201
1202	if (!PyArg_ParseTupleAndKeywords(args, kwds,
1203				Py_ARG_BYTES "O",
1204				keywords, &typestr, &structType)) {
1205		return NULL;
1206	}
1207
1208	if (PyObjC_RegisterStructAlias(typestr, structType) == -1) {
1209		return NULL;
1210	}
1211
1212	Py_INCREF(structType);
1213	return structType;
1214}
1215
1216
1217PyDoc_STRVAR(createStructType_doc,
1218	"createStructType(name, typestr, fieldnames, doc, pack) -> type\n"
1219	"\n"
1220	"Return a wrapper type for structs of the given type. The wrapper will \n"
1221	"registered with PyObjC and will be used to wrap structs of the given type.\n"
1222	"The field names can be ``None`` iff the typestr contains field names."
1223);
1224static PyObject*
1225createStructType(PyObject* self __attribute__((__unused__)),
1226		PyObject* args, PyObject* kwds)
1227{
1228static char* keywords[] = { "name", "typestr", "fieldnames", "doc", "pack", NULL };
1229	char* name;
1230	char* typestr;
1231	PyObject* pyfieldnames;
1232	char* docstr = NULL;
1233	PyObject* retval;
1234	char** fieldnames = NULL;
1235	Py_ssize_t i;
1236	Py_ssize_t field_count;
1237	Py_ssize_t pack = -1;
1238
1239	if (!PyArg_ParseTupleAndKeywords(args, kwds,
1240				"s"Py_ARG_BYTES"O|z" Py_ARG_SIZE_T ,
1241				keywords,
1242				&name, &typestr, &pyfieldnames, &docstr, &pack)) {
1243		return NULL;
1244	}
1245
1246	name = PyObjCUtil_Strdup(name);
1247	typestr = PyObjCUtil_Strdup(typestr);
1248	if (docstr) {
1249		docstr = PyObjCUtil_Strdup(docstr);
1250	}
1251
1252	if (pyfieldnames != Py_None) {
1253		pyfieldnames = PySequence_Fast(pyfieldnames,
1254			"fieldnames must be a sequence of strings");
1255
1256		if (pyfieldnames == NULL) goto error_cleanup;
1257		if (name == NULL || typestr == NULL) {
1258			PyErr_NoMemory();
1259			goto error_cleanup;
1260		}
1261
1262		fieldnames = PyMem_Malloc(sizeof(char*) * PySequence_Fast_GET_SIZE(pyfieldnames));
1263		if (fieldnames == NULL) {
1264			PyErr_NoMemory();
1265			goto error_cleanup;
1266		}
1267		memset(fieldnames, 0,
1268				sizeof(char*) * PySequence_Fast_GET_SIZE(pyfieldnames));
1269		for (i = 0; i < PySequence_Fast_GET_SIZE(pyfieldnames); i++) {
1270			PyObject* v = PySequence_Fast_GET_ITEM(pyfieldnames, i);
1271			if (PyUnicode_Check(v)) {
1272				PyObject* bytes = PyUnicode_AsEncodedString(v, NULL, NULL);
1273				if (bytes == NULL) {
1274					goto error_cleanup;
1275				}
1276				fieldnames[i] = PyObjCUtil_Strdup(PyBytes_AsString(bytes));
1277				Py_DECREF(bytes);
1278#if PY_MAJOR_VERSION == 2
1279			} else if (PyString_Check(v)) {
1280				fieldnames[i] = PyObjCUtil_Strdup(PyString_AS_STRING(v));
1281#endif
1282			} else {
1283				PyErr_SetString(PyExc_TypeError,
1284					"fieldnames must be a sequence of strings");
1285				goto error_cleanup;
1286			}
1287			if (fieldnames[i] == NULL) {
1288				PyErr_NoMemory();
1289				goto error_cleanup;
1290			}
1291		}
1292		field_count = PySequence_Fast_GET_SIZE(pyfieldnames);
1293	} else {
1294		field_count = -1;
1295		fieldnames = NULL;
1296	}
1297
1298
1299	retval = PyObjC_RegisterStructType(typestr, name, docstr, NULL,
1300			field_count, (const char**)fieldnames, pack);
1301	if (retval == NULL) goto error_cleanup;
1302	Py_DECREF(pyfieldnames);
1303
1304	return retval;
1305
1306error_cleanup:
1307	if (name) PyMem_Free(name);
1308	if (typestr) PyMem_Free(typestr);
1309	if (docstr) PyMem_Free(docstr);
1310	if (fieldnames) {
1311		for (i = 0; i < PySequence_Fast_GET_SIZE(pyfieldnames); i++) {
1312			if (fieldnames[i]) PyMem_Free(fieldnames[i]);
1313		}
1314		PyMem_Free(fieldnames);
1315	}
1316	Py_XDECREF(pyfieldnames);
1317
1318	return NULL;
1319}
1320
1321PyDoc_STRVAR(PyObjCIvar_Info_doc,
1322	"listInstanceVariables(classOrInstance) -> [ (name, typestr), ... ]\n"
1323	"\n"
1324	"Return information about all instance variables of an object or class\n"
1325);
1326PyDoc_STRVAR(PyObjCIvar_Get_doc,
1327	"getInstanceVariable(object, name) -> value\n"
1328	"\n"
1329	"Return the value of an instance variable\n"
1330);
1331PyDoc_STRVAR(PyObjCIvar_Set_doc,
1332	"setInstanceVariable(object, name, value [, updateRefCount])\n"
1333	"\n"
1334	"Modify an instance variable. If the instance variable is an object \n"
1335	"reference you must include the ``updateRefCount`` argument, otherwise it \n"
1336	"is ignored. If ``updateRefCount`` is true the reference counts of the \n"
1337	"old and new values are updated, otherwise they are not.\n"
1338	"\n"
1339	"NOTE: updating instance variables is dangerous, instance variables are \n"
1340	"private in Objective-C and classes might not expected that those values \n"
1341	"are changed by other code."
1342);
1343
1344PyDoc_STRVAR(registerCFSignature_doc,
1345	"registerCFSignature(name, encoding, typeId [, tollfreeName]) -> type\n"
1346	"\n"
1347	"Register a CoreFoundation based type with the bridge. If \n"
1348	"tollFreeName is supplied the type is tollfree bridged to that class.");
1349static PyObject*
1350registerCFSignature(PyObject* self __attribute__((__unused__)),
1351		PyObject* args, PyObject* kwds)
1352{
1353	static char* keywords[] = { "name", "encoding", "typeId", "tollfreeName", NULL };
1354	char* name;
1355	char* encoding;
1356	PyObject* pTypeId;
1357	CFTypeID typeId;
1358	char* tollfreeName = NULL;
1359
1360	if (!PyArg_ParseTupleAndKeywords(args, kwds,
1361		"s"Py_ARG_BYTES"O|s",
1362		keywords, &name, &encoding, &pTypeId, &tollfreeName)) {
1363		return NULL;
1364	}
1365
1366	if (pTypeId == Py_None) {
1367		if (tollfreeName == NULL) {
1368			PyErr_SetString(PyExc_ValueError,
1369				"Must specify a typeid when not toll-free");
1370			return NULL;
1371		}
1372		typeId = (CFTypeID)-1;
1373
1374	} else if (depythonify_c_value(@encode(CFTypeID), pTypeId, &typeId) == -1) {
1375		return NULL;
1376
1377	} else {
1378		PyObject* v = PyInt_FromLong(typeId);
1379		int r;
1380
1381		if (v == NULL) {
1382			return NULL;
1383		}
1384
1385		r = PyDict_SetItemString(PyObjC_TypeStr2CFTypeID, encoding, v);
1386		Py_DECREF(v);
1387		if (r == -1) {
1388			return NULL;
1389		}
1390	}
1391
1392	if (tollfreeName) {
1393		Class cls = objc_lookUpClass(tollfreeName);
1394		if (cls == nil) {
1395			PyErr_SetString(PyObjCExc_NoSuchClassError,
1396					tollfreeName);
1397			return NULL;
1398		}
1399		if (PyObjCPointerWrapper_RegisterID(encoding) == -1) {
1400			return NULL;
1401		}
1402
1403		/* Don't have to do anything with the cfTypeId: because
1404		 * the type is toll-free bridged automatic conversion will
1405		 * do the right thing.
1406		 */
1407
1408		return PyObjCClass_New(cls);
1409	} else {
1410		return PyObjCCFType_New(name, encoding, typeId);
1411	}
1412
1413	Py_INCREF(Py_None);
1414	return Py_None;
1415}
1416
1417PyDoc_STRVAR(_updatingMetadata_doc,
1418	"PRIVATE:");
1419static PyObject*
1420_updatingMetadata(PyObject* self __attribute__((__unused__)),
1421		PyObject* args, PyObject* kwds)
1422{
1423	static char* keywords[] = { "flag", NULL };
1424	PyObject* flag;
1425
1426	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", keywords, &flag)) {
1427		return NULL;
1428	}
1429
1430	if (PyObject_IsTrue(flag)) {
1431		PyObjC_UpdatingMetaData = YES;
1432
1433	} else {
1434		PyObjC_MappingCount ++;
1435		PyObjC_UpdatingMetaData = NO;
1436
1437	}
1438
1439	Py_INCREF(Py_None);
1440	return Py_None;
1441}
1442
1443/* Support for locking */
1444static PyObject*
1445PyObjC_objc_sync_enter(PyObject* self __attribute__((__unused__)), PyObject* args)
1446{
1447	NSObject* object;
1448	int rv;
1449
1450	if (!PyArg_ParseTuple(args, "O&",
1451			PyObjCObject_Convert, &object)) {
1452		return NULL;
1453	}
1454
1455	Py_BEGIN_ALLOW_THREADS
1456		rv = objc_sync_enter(object);
1457
1458	Py_END_ALLOW_THREADS
1459
1460	if (rv == OBJC_SYNC_SUCCESS) {
1461		Py_INCREF(Py_None);
1462		return Py_None;
1463	}
1464
1465	PyErr_Format(PyObjCExc_LockError, "objc_sync_enter failed: %d", rv);
1466	return NULL;
1467}
1468
1469static PyObject*
1470PyObjC_objc_sync_exit(PyObject* self __attribute__((__unused__)), PyObject* args)
1471{
1472	NSObject* object;
1473	int rv;
1474
1475	if (!PyArg_ParseTuple(args, "O&",
1476			PyObjCObject_Convert, &object)) {
1477		return NULL;
1478	}
1479
1480	Py_BEGIN_ALLOW_THREADS
1481		rv = objc_sync_exit(object);
1482	Py_END_ALLOW_THREADS
1483	if (rv == OBJC_SYNC_SUCCESS) {
1484		Py_INCREF(Py_None);
1485		return Py_None;
1486	}
1487
1488	PyErr_Format(PyObjCExc_LockError, "objc_sync_exit failed: %d", rv);
1489	return NULL;
1490}
1491
1492
1493PyDoc_STRVAR(_makeClosure_doc,
1494  "_makeClosure(callable, closureFor, [argIndex]) -> closure\n"
1495  "\n"
1496  "Returns a closure object that can be used to call the function from\n"
1497  "C. This object has no useable interface from Python.\n"
1498 );
1499#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7
1500static void _callback_cleanup(void* closure)
1501{
1502	PyObjCFFI_FreeIMP((IMP)closure);
1503}
1504#else
1505static void _callback_cleanup(PyObject* closure)
1506{
1507	PyObjCFFI_FreeIMP((IMP)PyCapsule_GetPointer(closure, "objc.__imp__"));
1508}
1509#endif
1510
1511static PyObject*
1512_makeClosure(
1513	PyObject* self __attribute__((__unused__)),
1514	PyObject* args,
1515	PyObject* kwds)
1516{
1517static  char* keywords[] = { "callable", "closureFor", "argIndex", NULL };
1518	PyObject* callable;
1519	PyObject* closureFor;
1520	PyObjCMethodSignature* methinfo;
1521	Py_ssize_t argIndex;
1522	Py_ssize_t i;
1523
1524	argIndex=-1;
1525	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|" Py_ARG_SIZE_T ,
1526		keywords, &callable, &closureFor, &argIndex)) {
1527		return NULL;
1528	}
1529
1530	if (!PyCallable_Check(callable)) {
1531		PyErr_SetString(PyExc_TypeError, "Callable isn't callable");
1532		return NULL;
1533	}
1534
1535	if (PyObjCFunction_Check(closureFor)) {
1536		methinfo = (PyObjCMethodSignature*)PyObjCFunc_GetMethodSignature(closureFor);
1537		if (methinfo == NULL) {
1538			return NULL;
1539		}
1540
1541	} else if (PyObjCSelector_Check(closureFor)) {
1542		methinfo = ((PyObjCSelector*)closureFor)->sel_methinfo;
1543		if (methinfo == NULL) {
1544			PyErr_SetString(PyExc_ValueError,
1545				"No signature??");
1546			return NULL;
1547		}
1548
1549	} else {
1550		PyErr_Format(PyExc_TypeError, "Don't know how to create closure for instance of %s",
1551				Py_TYPE(closureFor)->tp_name);
1552		return NULL;
1553	}
1554
1555	if (argIndex == -1) {
1556		for (i = 0; i < Py_SIZE(methinfo); i++) {
1557			if (methinfo->argtype[i].callable != NULL) {
1558				argIndex = i;
1559				break;
1560			}
1561		}
1562		if (argIndex == -1) {
1563			PyErr_SetString(PyExc_ValueError,
1564				"No callback argument in the specified object");
1565			return NULL;
1566		}
1567
1568	} else {
1569		if (argIndex < 0 || argIndex >= Py_SIZE(methinfo)) {
1570			PyErr_SetString(PyExc_IndexError,
1571				"No such argument");
1572			return NULL;
1573		}
1574		if (methinfo->argtype[argIndex].callable == NULL) {
1575			PyErr_Format(PyExc_ValueError,
1576				"Argument %" PY_FORMAT_SIZE_T "d is not callable", argIndex);
1577			return NULL;
1578		}
1579	}
1580
1581
1582	PyObjC_callback_function result;
1583
1584	result = PyObjCFFI_MakeFunctionClosure(methinfo->argtype[argIndex].callable, callable);
1585	if (result == NULL) {
1586		return NULL;
1587	}
1588
1589	PyObject* retval = PyCapsule_New(
1590		result, "objc.__imp__", _callback_cleanup);
1591	if (retval == NULL) {
1592		PyObjCFFI_FreeIMP((IMP)result);
1593		return NULL;
1594	}
1595
1596	return retval;
1597}
1598
1599static PyObject*
1600ivar_dict(PyObject* self __attribute__((__unused__)))
1601{
1602	Py_INCREF(PyObjCInstanceVariable_Type.tp_dict);
1603	return PyObjCInstanceVariable_Type.tp_dict;
1604}
1605
1606PyObject*
1607PyObjC_AdjustSelf(PyObject* object)
1608{
1609	if (PyType_Check(object) && PyType_IsSubtype((PyTypeObject*)object, &PyObjCClass_Type)) {
1610		PyObject* temp = PyObjCClass_ClassForMetaClass(object);
1611		Py_INCREF(temp);
1612		Py_DECREF(object);
1613		return temp;
1614	}
1615	return object;
1616}
1617
1618static PyObject*
1619mod_propertiesForClass(PyObject* mod __attribute__((__unused__)), PyObject* object)
1620{
1621	return PyObjCClass_ListProperties(object);
1622}
1623
1624static PyObject*
1625mod_setClassSetupHook(PyObject* mod __attribute__((__unused__)), PyObject* hook)
1626{
1627	PyObject* curval = PyObjC_class_setup_hook;
1628
1629	PyObjC_class_setup_hook = hook;
1630	Py_INCREF(hook);
1631
1632	return curval;
1633}
1634
1635/*
1636 * Helper function for decoding XML metadata:
1637 *
1638 * This fixes an issue with metadata files: metadata files use
1639 * _C_BOOL to represent type 'BOOL', but that the string should
1640 * be used to represent 'bool' which has a different size on
1641 * PPC. Therefore swap usage of _C_BOOL and _C_NSBOOL in data
1642 * from metadata files.
1643 */
1644static void typecode2typecode(char* buf)
1645{
1646	/* Skip pointer declarations and anotations */
1647	for (;;) {
1648		switch(*buf) {
1649		case _C_PTR:
1650		case _C_IN:
1651		case _C_OUT:
1652		case _C_INOUT:
1653		case _C_ONEWAY:
1654		case _C_CONST:
1655			buf++;
1656			break;
1657		default:
1658		      goto exit;
1659		}
1660	}
1661exit:
1662
1663	switch (*buf) {
1664	case _C_BOOL:
1665		*buf = _C_NSBOOL;
1666		break;
1667	case _C_NSBOOL:
1668		*buf = _C_BOOL;
1669		break;
1670        case _C_STRUCT_B:
1671		while (buf && *buf != _C_STRUCT_E && *buf && *buf++ != '=') {
1672		}
1673		while (buf && *buf && *buf != _C_STRUCT_E) {
1674			if (*buf == '"') {
1675				/* embedded field name */
1676				buf = strchr(buf+1, '"');
1677				if (buf == NULL) {
1678					return;
1679				}
1680				buf++;
1681			}
1682			typecode2typecode(buf);
1683			buf = (char*)PyObjCRT_SkipTypeSpec(buf);
1684		}
1685		break;
1686
1687	case _C_UNION_B:
1688		while (buf && *buf != _C_UNION_E && *buf && *buf++ != '=') {
1689		}
1690		while (buf && *buf && *buf != _C_UNION_E) {
1691			if (*buf == '"') {
1692				/* embedded field name */
1693				buf = strchr(buf+1, '"');
1694				if (buf == NULL) {
1695					return;
1696				}
1697				buf++;
1698			}
1699			typecode2typecode(buf);
1700			buf = (char*)PyObjCRT_SkipTypeSpec(buf);
1701		}
1702		break;
1703
1704
1705	case _C_ARY_B:
1706		while (isdigit(*++buf));
1707		typecode2typecode(buf);
1708		break;
1709	}
1710}
1711
1712
1713
1714static PyObject*
1715typestr2typestr(PyObject* args)
1716{
1717	char* s;
1718	char* buf;
1719
1720	if (PyUnicode_Check(args)) {
1721		PyObject* bytes = PyUnicode_AsEncodedString(args, NULL, NULL);
1722		if (bytes == NULL) {
1723			return NULL;
1724		}
1725		buf = PyObjCUtil_Strdup(PyBytes_AsString(args));
1726		Py_DECREF(bytes);
1727
1728	} else if (PyBytes_Check(args)) {
1729		buf = PyObjCUtil_Strdup(PyBytes_AsString(args));
1730	} else {
1731		PyErr_SetString(PyExc_TypeError, "expecing string");
1732		return NULL;
1733	}
1734
1735
1736	if (buf == NULL) {
1737		PyErr_NoMemory();
1738		return NULL;
1739	}
1740
1741	s = buf;
1742	while (s && *s) {
1743		typecode2typecode(s);
1744		if (s && *s == '\"') {
1745			PyErr_Format(PyObjCExc_InternalError,
1746				"typecode2typecode: invalid typecode '%c' "
1747				"at \"%s\"", *s, s);
1748			*s = '\0';
1749			PyMem_Free(buf);
1750			return NULL;
1751
1752		} else {
1753			s = (char*)PyObjCRT_SkipTypeSpec(s);
1754		}
1755	}
1756
1757	PyObject* result = PyBytes_FromString(buf);
1758	PyMem_Free(buf);
1759
1760	return result;
1761}
1762
1763
1764
1765static PyObject*
1766_clear_intern(PyObject* self __attribute__((__unused__)))
1767{
1768	PyObjC_ClearIntern();
1769	Py_INCREF(Py_None);
1770	return Py_None;
1771}
1772
1773
1774#if    PyObjC_BUILD_RELEASE >= 1006
1775    /* Associated Object support. Functionality is available on OSX 10.6 or later. */
1776
1777PyDoc_STRVAR(PyObjC_setAssociatedObject_doc,
1778	"setAssociatedObject(object, key, value, [policy=objc.OBJC_ASSOCIATION_RETAIN])\n"
1779	"\n"
1780	"Set the value for an object assiociation. Use 'None' as the\n"
1781	"value to clear an association.");
1782static PyObject*
1783PyObjC_setAssociatedObject(PyObject* self __attribute__((__unused__)),
1784	PyObject* args, PyObject* kwds)
1785{
1786static char* keywords[] = { "object", "key", "value", "policy", NULL };
1787	id object;
1788	PyObject* key;
1789	id value;
1790	long	  policy = OBJC_ASSOCIATION_RETAIN;
1791
1792	if (objc_setAssociatedObject == NULL) {
1793		PyErr_SetString(PyObjCExc_Error, "setAssociatedObject not available on this platform");
1794		return NULL;
1795	}
1796
1797	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&OO&|i",
1798		keywords,
1799		PyObjCObject_Convert, &object,
1800		&key,
1801		PyObjCObject_Convert, &value,
1802		&policy
1803		)) {
1804
1805		return NULL;
1806	}
1807
1808	PyObjC_DURING
1809		objc_setAssociatedObject(object, (void*)key, value, policy);
1810	PyObjC_HANDLER
1811		PyObjCErr_FromObjC(localException);
1812	PyObjC_ENDHANDLER
1813
1814	if (PyErr_Occurred()) return NULL;
1815
1816	Py_INCREF(Py_None);
1817	return Py_None;
1818}
1819
1820
1821PyDoc_STRVAR(PyObjC_getAssociatedObject_doc,
1822	"getAssociatedObject(object, key) -> value\n"
1823	"\n"
1824	"Get the value for an object assiociation. Returns None \n"
1825	"when they association doesn't exist.");
1826static PyObject*
1827PyObjC_getAssociatedObject(PyObject* self __attribute__((__unused__)),
1828	PyObject* args, PyObject* kwds)
1829{
1830static char* keywords[] = { "object", "key", NULL};
1831	id object;
1832	PyObject* key;
1833	id value;
1834
1835	if (objc_getAssociatedObject == NULL) {
1836		PyErr_SetString(PyObjCExc_Error, "setAssociatedObject not available on this platform");
1837		return NULL;
1838	}
1839
1840	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O",
1841		keywords,
1842		PyObjCObject_Convert, &object,
1843		&key
1844		)) {
1845
1846		return NULL;
1847	}
1848
1849	PyObjC_DURING
1850		value = objc_getAssociatedObject(object, (void*)key);
1851	PyObjC_HANDLER
1852		value = nil;
1853		PyObjCErr_FromObjC(localException);
1854	PyObjC_ENDHANDLER
1855
1856	if (PyErr_Occurred()) return NULL;
1857
1858	return PyObjC_IdToPython(value);
1859}
1860
1861PyDoc_STRVAR(PyObjC_removeAssociatedObjects_doc,
1862	"removeAssociatedObjects(object)\n"
1863	"\n"
1864	"Remove all assocations from an object. This should in general not be used because\n"
1865	"it clear all references, including those made from unrelated code.\n");
1866
1867static PyObject*
1868PyObjC_removeAssociatedObjects(PyObject* self __attribute__((__unused__)),
1869	PyObject* args, PyObject* kwds)
1870{
1871static char* keywords[] = { "object", NULL};
1872	id object;
1873
1874	if (objc_removeAssociatedObjects == NULL) {
1875		PyErr_SetString(PyObjCExc_Error, "setAssociatedObject not available on this platform");
1876		return NULL;
1877	}
1878
1879	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&",
1880		keywords,
1881		PyObjCObject_Convert, &object
1882		)) {
1883
1884		return NULL;
1885	}
1886
1887	PyObjC_DURING
1888		objc_removeAssociatedObjects(object);
1889	PyObjC_HANDLER
1890		PyObjCErr_FromObjC(localException);
1891	PyObjC_ENDHANDLER
1892
1893	if (PyErr_Occurred()) return NULL;
1894
1895	Py_INCREF(Py_None);
1896	return Py_None;
1897}
1898#endif
1899
1900static PyObject*
1901PyObjC_LoadConstant(PyObject* self __attribute__((__unused__)),
1902	PyObject* args, PyObject* kwds)
1903{
1904static char* keywords[] = { "name", "type", "magic", NULL };
1905	char* name;
1906	char* type;
1907	int   magic;
1908#if PY_MAJOR_VERSION >= 3
1909	PyObject *a0, *a1, *a2;
1910#endif
1911
1912#if PY_MAJOR_VERSION >= 3
1913	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", keywords, &a0, &a1, &a2)) return NULL;
1914	if (!PyArg_ParseTupleAndKeywords(args, kwds, PyBytes_Check(a1) ? "syi" : "ssi",
1915#else
1916	if (!PyArg_ParseTupleAndKeywords(args, kwds, "ssi",
1917#endif
1918		keywords, &name, &type, &magic)) {
1919
1920		return NULL;
1921	}
1922
1923	void* buf = dlsym(RTLD_DEFAULT, name);
1924	if (buf == NULL) {
1925		PyErr_SetString(PyExc_AttributeError, name);
1926		return NULL;
1927	}
1928
1929	PyObject* v;
1930
1931	if (magic) {
1932		v = PyObjCCF_NewSpecial(type, buf);
1933	} else {
1934		v = pythonify_c_value(type, buf);
1935	}
1936
1937	if (v == NULL) {
1938		return NULL;
1939	}
1940	return v;
1941}
1942
1943
1944static PyMethodDef mod_methods[] = {
1945	{
1946		"_setClassSetUpHook",
1947		(PyCFunction)mod_setClassSetupHook,
1948		METH_O,
1949		"Private: set hook used during subclass creation",
1950	},
1951
1952	{
1953		"propertiesForClass",
1954	  	(PyCFunction)mod_propertiesForClass,
1955		METH_O,
1956		"Return information about properties from the runtim",
1957	},
1958	{
1959	  "splitSignature",
1960	  (PyCFunction)objc_splitSignature,
1961	  METH_VARARGS|METH_KEYWORDS,
1962	  objc_splitSignature_doc
1963	},
1964	{
1965	  "splitStructSignature",
1966	  (PyCFunction)objc_splitStructSignature,
1967	  METH_VARARGS|METH_KEYWORDS,
1968	  objc_splitStructSignature_doc,
1969	},
1970	{
1971	  "lookUpClass",
1972	  (PyCFunction)lookUpClass,
1973	  METH_VARARGS|METH_KEYWORDS,
1974	  lookUpClass_doc
1975	},
1976	{
1977	  "classAddMethods",
1978	  (PyCFunction)classAddMethods,
1979	  METH_VARARGS|METH_KEYWORDS,
1980	  classAddMethods_doc
1981	},
1982	{ "currentBundle", (PyCFunction)currentBundle, METH_NOARGS, currentBundle_doc },
1983	{ "getClassList", (PyCFunction)getClassList, METH_NOARGS, getClassList_doc },
1984	{ "_setClassExtender", (PyCFunction)set_class_extender, METH_VARARGS|METH_KEYWORDS, set_class_extender_doc  },
1985	{ "setSignatureForSelector", (PyCFunction)set_signature_for_selector, METH_VARARGS|METH_KEYWORDS, set_signature_for_selector_doc },
1986	{ "recycleAutoreleasePool", (PyCFunction)recycle_autorelease_pool, METH_VARARGS|METH_KEYWORDS, recycle_autorelease_pool_doc },
1987	{ "removeAutoreleasePool", (PyCFunction)remove_autorelease_pool, METH_VARARGS|METH_KEYWORDS, remove_autorelease_pool_doc },
1988	{ "_setNSNumberWrapper", (PyCFunction)setNSNumberWrapper, METH_VARARGS|METH_KEYWORDS, setNSNumberWrapper_doc },
1989	{ "_getNSNumberWrapper", (PyCFunction)getNSNumberWrapper, METH_VARARGS|METH_KEYWORDS, getNSNumberWrapper_doc },
1990	{ "setVerbose", (PyCFunction)setVerbose, METH_VARARGS|METH_KEYWORDS, setVerbose_doc },
1991	{ "setObjCPointerIsError", (PyCFunction)setObjCPointerIsError, METH_VARARGS|METH_KEYWORDS, setObjCPointerIsError_doc },
1992	{ "setUseKVOForSetattr", (PyCFunction)setUseKVOForSetattr, METH_VARARGS|METH_KEYWORDS, setUseKVOForSetattr_doc },
1993	{ "setHideProtected", (PyCFunction)setHideProtected, METH_VARARGS|METH_KEYWORDS, setHideProtected_doc },
1994	{ "getVerbose", (PyCFunction)getVerbose, METH_VARARGS|METH_KEYWORDS, getVerbose_doc },
1995	{ "getObjCPointerIsError", (PyCFunction)getObjCPointerIsError, METH_VARARGS|METH_KEYWORDS, getObjCPointerIsError_doc },
1996	{ "pyobjc_id", (PyCFunction)pyobjc_id, METH_VARARGS|METH_KEYWORDS, pyobjc_id_doc },
1997	{ "repythonify", (PyCFunction)repythonify, METH_VARARGS|METH_KEYWORDS, repythonify_doc },
1998#if PY_MAJOR_VERSION == 2
1999	{ "setStrBridgeEnabled", (PyCFunction)setStrBridgeEnabled, METH_VARARGS|METH_KEYWORDS, setStrBridgeEnabled_doc },
2000	{ "getStrBridgeEnabled", (PyCFunction)getStrBridgeEnabled, METH_VARARGS|METH_KEYWORDS, getStrBridgeEnabled_doc },
2001#endif
2002	{ "loadBundle", (PyCFunction)loadBundle, METH_VARARGS|METH_KEYWORDS, loadBundle_doc },
2003	{ "allocateBuffer", (PyCFunction)allocateBuffer, METH_VARARGS|METH_KEYWORDS, allocateBuffer_doc },
2004	{ "protocolsForClass", (PyCFunction)protocolsForClass, METH_VARARGS|METH_KEYWORDS, protocolsForClass_doc },
2005	{ "protocolsForProcess", (PyCFunction)protocolsForProcess, METH_NOARGS, protocolsForProcess_doc },
2006	{ "_protocolNamed", (PyCFunction)protocolNamed, METH_VARARGS|METH_KEYWORDS, protocolNamed_doc },
2007	{ "registerCFSignature", (PyCFunction)registerCFSignature, METH_VARARGS|METH_KEYWORDS, registerCFSignature_doc },
2008#if PY_MAJOR_VERSION == 2
2009	{ "CFToObject", (PyCFunction)objc_CFToObject, METH_VARARGS|METH_KEYWORDS, objc_CFToObject_doc },
2010	{ "ObjectToCF", (PyCFunction)objc_ObjectToCF, METH_VARARGS|METH_KEYWORDS, objc_ObjectToCF_doc },
2011#endif
2012	{ "loadBundleVariables", (PyCFunction)PyObjC_loadBundleVariables,
2013		METH_VARARGS|METH_KEYWORDS, PyObjC_loadBundleVariables_doc },
2014	{ "loadSpecialVar", (PyCFunction)PyObjC_loadSpecialVar,
2015		METH_VARARGS|METH_KEYWORDS, NULL },
2016	{ "loadBundleFunctions", (PyCFunction)PyObjC_loadBundleFunctions,
2017		METH_VARARGS|METH_KEYWORDS, PyObjC_loadBundleFunctions_doc },
2018	{ "loadFunctionList", (PyCFunction)PyObjC_loadFunctionList,
2019		METH_VARARGS|METH_KEYWORDS, PyObjC_loadFunctionList_doc },
2020	{ "listInstanceVariables", (PyCFunction)PyObjCIvar_Info,
2021		METH_O, PyObjCIvar_Info_doc },
2022	{ "getInstanceVariable", (PyCFunction)PyObjCIvar_Get,
2023		METH_VARARGS|METH_KEYWORDS, PyObjCIvar_Get_doc },
2024	{ "setInstanceVariable", (PyCFunction)PyObjCIvar_Set,
2025		METH_VARARGS|METH_KEYWORDS, PyObjCIvar_Set_doc },
2026	{ "createOpaquePointerType", (PyCFunction)createOpaquePointerType,
2027		METH_VARARGS|METH_KEYWORDS, createOpaquePointerType_doc },
2028	{ "createStructType", (PyCFunction)createStructType,
2029		METH_VARARGS|METH_KEYWORDS, createStructType_doc },
2030	{ "registerStructAlias", (PyCFunction)registerStructAlias,
2031		METH_VARARGS|METH_KEYWORDS, registerStructAlias_doc },
2032	{ "registerMetaDataForSelector", (PyCFunction)registerMetaData,
2033		METH_VARARGS|METH_KEYWORDS, registerMetaData_doc },
2034	{ "_updatingMetadata", (PyCFunction)_updatingMetadata,
2035		METH_VARARGS|METH_KEYWORDS, _updatingMetadata_doc },
2036	{ "_makeClosure", (PyCFunction)_makeClosure,
2037		METH_VARARGS|METH_KEYWORDS, _makeClosure_doc },
2038
2039	{ "_sockaddrFromPython", (PyCFunction)PyObjC_SockAddrFromPython,
2040		METH_VARARGS, "private function" },
2041	{ "_sockaddrToPython", (PyCFunction)PyObjC_SockAddrToPython,
2042		METH_VARARGS, "private function" },
2043	{ "_ivar_dict", (PyCFunction)ivar_dict, METH_NOARGS, "private functions" },
2044
2045	{ "_objc_sync_enter", (PyCFunction)PyObjC_objc_sync_enter,
2046		METH_VARARGS, "acquire mutex for an object" },
2047	{ "_objc_sync_exit", (PyCFunction)PyObjC_objc_sync_exit,
2048		METH_VARARGS, "release mutex for an object" },
2049	{ "_block_call", (PyCFunction)PyObjCBlock_Call,
2050		METH_VARARGS,
2051		"_block_call(block, signature, args, kwds) -> retval" },
2052
2053	{ "_typestr2typestr", (PyCFunction)typestr2typestr,
2054		METH_O, "private function" },
2055
2056	{ "_clear_intern", (PyCFunction)_clear_intern, METH_NOARGS,  NULL },
2057
2058#if    PyObjC_BUILD_RELEASE >= 1006
2059
2060	{ "setAssociatedObject", (PyCFunction)PyObjC_setAssociatedObject,
2061		METH_VARARGS|METH_KEYWORDS, PyObjC_setAssociatedObject_doc },
2062	{ "getAssociatedObject", (PyCFunction)PyObjC_getAssociatedObject,
2063		METH_VARARGS|METH_KEYWORDS, PyObjC_getAssociatedObject_doc },
2064	{ "removeAssociatedObjects", (PyCFunction)PyObjC_removeAssociatedObjects,
2065		METH_VARARGS|METH_KEYWORDS, PyObjC_removeAssociatedObjects_doc },
2066
2067#endif /* PyObjC_BUILD_RELEASE >= 1006 */
2068
2069	{ "_loadConstant", (PyCFunction)PyObjC_LoadConstant,
2070		METH_VARARGS|METH_KEYWORDS, "(PRIVATE)" },
2071
2072	{ 0, 0, 0, 0 } /* sentinel */
2073};
2074
2075struct objc_typestr_values {
2076	char*	name;
2077	char    value;
2078} objc_typestr_values [] = {
2079	{ "_C_ID", _C_ID },
2080	{ "_C_CLASS", _C_CLASS },
2081	{ "_C_SEL", _C_SEL },
2082	{ "_C_CHR", _C_CHR },
2083	{ "_C_UCHR", _C_UCHR },
2084	{ "_C_SHT", _C_SHT },
2085	{ "_C_USHT", _C_USHT },
2086#ifdef _C_BOOL
2087	{ "_C_BOOL", _C_BOOL },
2088#endif
2089	{ "_C_INT", _C_INT },
2090	{ "_C_UINT", _C_UINT },
2091	{ "_C_LNG", _C_LNG },
2092	{ "_C_ULNG", _C_ULNG },
2093	{ "_C_LNG_LNG", _C_LNG_LNG },
2094	{ "_C_ULNG_LNG", _C_ULNG_LNG },
2095	{ "_C_FLT", _C_FLT },
2096	{ "_C_DBL", _C_DBL },
2097	{ "_C_BFLD", _C_BFLD },
2098	{ "_C_VOID", _C_VOID },
2099	{ "_C_UNDEF", _C_UNDEF },
2100	{ "_C_PTR", _C_PTR },
2101	{ "_C_CHARPTR", _C_CHARPTR },
2102	{ "_C_ARY_B", _C_ARY_B },
2103	{ "_C_ARY_E", _C_ARY_E },
2104	{ "_C_UNION_B", _C_UNION_B },
2105	{ "_C_UNION_E", _C_UNION_E },
2106	{ "_C_STRUCT_B", _C_STRUCT_B },
2107	{ "_C_STRUCT_E", _C_STRUCT_E },
2108	{ "_C_CONST", _C_CONST },
2109	{ "_C_IN", _C_IN },
2110	{ "_C_INOUT", _C_INOUT },
2111	{ "_C_OUT", _C_OUT },
2112	{ "_C_BYCOPY", _C_BYCOPY },
2113	{ "_C_ONEWAY", _C_ONEWAY },
2114
2115	/* Compatibility: */
2116	{ "_C_LNGLNG", _C_LNG_LNG },
2117	{ "_C_ULNGLNG", _C_ULNG_LNG },
2118
2119	/* PyObjC specific */
2120	{ "_C_NSBOOL",	_C_NSBOOL },
2121	{ "_C_UNICHAR", _C_UNICHAR },
2122	{ "_C_CHAR_AS_TEXT", _C_CHAR_AS_TEXT },
2123	{ "_C_CHAR_AS_INT", _C_CHAR_AS_INT },
2124
2125	{ NULL, 0 }
2126};
2127
2128
2129
2130PyObjC_MODULE_INIT(_objc)
2131{
2132	PyObject *m, *d, *v;
2133
2134	PyObjC_SetupRuntimeCompat();
2135	if (PyErr_Occurred()) {
2136		PyObjC_INITERROR();
2137	}
2138
2139	NSAutoreleasePool *initReleasePool = [[NSAutoreleasePool alloc] init];
2140	[OC_NSBundleHack installBundleHack];
2141
2142	PyObjCClass_DefaultModule = PyText_FromString("objc");
2143
2144	if (PyObjC_InitProxyRegistry() < 0) {
2145		PyObjC_INITERROR();
2146	}
2147
2148	PyObjC_TypeStr2CFTypeID = PyDict_New();
2149	if (PyObjC_TypeStr2CFTypeID == NULL) {
2150		PyObjC_INITERROR();
2151	}
2152
2153	if (PyObjCBlock_Setup() == -1) {
2154		PyObjC_INITERROR();
2155	}
2156
2157
2158	if (PyType_Ready(&PyObjCClass_Type) < 0) {
2159		PyObjC_INITERROR();
2160	}
2161	if (PyType_Ready((PyTypeObject*)&PyObjCObject_Type) < 0) {
2162		PyObjC_INITERROR();
2163	}
2164	if (PyType_Ready(&PyObjCSelector_Type) < 0) {
2165		PyObjC_INITERROR();
2166	}
2167	if (PyType_Ready(&PyObjCNativeSelector_Type) < 0) {
2168		PyObjC_INITERROR();
2169	}
2170	if (PyType_Ready(&PyObjCPythonSelector_Type) < 0) {
2171		PyObjC_INITERROR();
2172	}
2173	if (PyType_Ready(&PyObjCInstanceVariable_Type) < 0) {
2174		PyObjC_INITERROR();
2175	}
2176	if (PyType_Ready(&PyObjCInformalProtocol_Type) < 0) {
2177		PyObjC_INITERROR();
2178	}
2179	if (PyType_Ready(&PyObjCFormalProtocol_Type) < 0) {
2180		PyObjC_INITERROR();
2181	}
2182	if (PyType_Ready(&PyObjCUnicode_Type) < 0) {
2183		PyObjC_INITERROR();
2184	}
2185	if (PyType_Ready(&PyObjCIMP_Type) < 0) {
2186		PyObjC_INITERROR();
2187	}
2188	if (PyType_Ready(&PyObjCMethodAccessor_Type) < 0) {
2189		PyObjC_INITERROR();
2190	}
2191	if (PyType_Ready(&PyObjCMethodSignature_Type) < 0) {
2192		PyObjC_INITERROR();
2193	}
2194	if (PyType_Ready(&PyObjC_VarList_Type) < 0) {
2195		PyObjC_INITERROR();
2196	}
2197	if (PyType_Ready(&PyObjC_FSRefType) < 0) {
2198		PyObjC_INITERROR();
2199	}
2200	if (PyType_Ready(&PyObjC_FSSpecType) < 0) {
2201		PyObjC_INITERROR();
2202	}
2203
2204	PyObjCSuper_Type.tp_doc = PySuper_Type.tp_doc;
2205	PyObjCSuper_Type.tp_init = PySuper_Type.tp_init;
2206	PyObjCSuper_Type.tp_alloc = PySuper_Type.tp_alloc;
2207	PyObjCSuper_Type.tp_new = PySuper_Type.tp_new;
2208	PyObjCSuper_Type.tp_dealloc = PySuper_Type.tp_dealloc;
2209	PyObjCSuper_Type.tp_free = PySuper_Type.tp_free;
2210	PyObjCSuper_Type.tp_traverse = PySuper_Type.tp_traverse;
2211	if (PyType_Ready(&PyObjCSuper_Type) < 0) {
2212		PyObjC_INITERROR();
2213	}
2214
2215	if (PyObjCCFType_Setup() == -1) {
2216		PyObjC_INITERROR();
2217	}
2218
2219	m = PyObjC_MODULE_CREATE(_objc);
2220	if (m == 0) {
2221		PyObjC_INITERROR();
2222	}
2223
2224
2225	d = PyModule_GetDict(m);
2226	if (d == 0) {
2227		PyObjC_INITERROR();
2228	}
2229	/* use PyDict_SetItemString for the retain, non-heap types can't be dealloc'ed */
2230
2231	if (PyDict_SetItemString(d, "objc_class", (PyObject*)&PyObjCClass_Type) < 0) {
2232		PyObjC_INITERROR();
2233	}
2234	if (PyDict_SetItemString(d, "objc_object", (PyObject*)&PyObjCObject_Type) < 0) {
2235		PyObjC_INITERROR();
2236	}
2237	if (PyDict_SetItemString(d, "pyobjc_unicode", (PyObject*)&PyObjCUnicode_Type) < 0) {
2238		PyObjC_INITERROR();
2239	}
2240	if (PyDict_SetItemString(d, "selector", (PyObject*)&PyObjCSelector_Type) < 0) {
2241		PyObjC_INITERROR();
2242	}
2243	if (PyDict_SetItemString(d, "FSRef", (PyObject*)&PyObjC_FSRefType) < 0) {
2244		PyObjC_INITERROR();
2245	}
2246	if (PyDict_SetItemString(d, "FSSpec", (PyObject*)&PyObjC_FSSpecType) < 0) {
2247		PyObjC_INITERROR();
2248	}
2249	if (PyDict_SetItemString(d, "ivar", (PyObject*)&PyObjCInstanceVariable_Type) < 0) {
2250		PyObjC_INITERROR();
2251	}
2252	if (PyDict_SetItemString(d, "informal_protocol", (PyObject*)&PyObjCInformalProtocol_Type) < 0) {
2253		PyObjC_INITERROR();
2254	}
2255	if (PyDict_SetItemString(d, "formal_protocol", (PyObject*)&PyObjCFormalProtocol_Type) < 0) {
2256		PyObjC_INITERROR();
2257	}
2258	if (PyDict_SetItemString(d, "varlist", (PyObject*)&PyObjC_VarList_Type) < 0) {
2259		PyObjC_INITERROR();
2260	}
2261	if (PyDict_SetItemString(d, "function", (PyObject*)&PyObjCFunc_Type) < 0) {
2262		PyObjC_INITERROR();
2263	}
2264	if (PyDict_SetItemString(d, "IMP", (PyObject*)&PyObjCIMP_Type) < 0) {
2265		PyObjC_INITERROR();
2266	}
2267	if (PyDict_SetItemString(d, "super", (PyObject*)&PyObjCSuper_Type) < 0) {
2268		PyObjC_INITERROR();
2269	}
2270
2271	v = PyObjCInitNULL();
2272	if (v == NULL) {
2273		PyObjC_INITERROR();
2274	}
2275
2276	if (PyDict_SetItemString(d, "NULL", v) < 0) {
2277		Py_DECREF(v);
2278		PyObjC_INITERROR();
2279	}
2280	Py_DECREF(v);
2281
2282	if (PyObjCUtil_Init(m) < 0) {
2283		PyObjC_INITERROR();
2284	}
2285	if (PyObjCAPI_Register(m) < 0) {
2286		PyObjC_INITERROR();
2287	}
2288	if (PyObjCIMP_SetUpMethodWrappers() < 0) {
2289		PyObjC_INITERROR();
2290	}
2291
2292#if PY_MAJOR_VERSION == 2
2293	PyObjCStrBridgeWarning = PyErr_NewException("objc.PyObjCStrBridgeWarning", PyExc_DeprecationWarning, NULL);
2294	PyModule_AddObject(m, "PyObjCStrBridgeWarning", PyObjCStrBridgeWarning);
2295#endif
2296
2297	{
2298		struct objc_typestr_values* cur = objc_typestr_values;
2299
2300		for (; cur->name != NULL; cur ++)  {
2301			PyObject* t = PyBytes_FromStringAndSize(&cur->value, 1);
2302			if (t == NULL) {
2303				PyObjC_INITERROR();
2304			}
2305			if (PyModule_AddObject(m, cur->name, t)) {
2306				PyObjC_INITERROR();
2307			}
2308		}
2309	}
2310
2311	/* Add _C_CFTYPEID to avoid hardcoding this in our python code */
2312	if (PyModule_AddObject(m, "_C_CFTYPEID", PyBytes_FromString(@encode(CFTypeID))) < 0) {
2313		PyObjC_INITERROR();
2314	}
2315
2316	/* Likewise for _C_NSInteger and _C_NSUInteger */
2317	if (PyModule_AddObject(m, "_C_NSInteger", PyBytes_FromString(@encode(NSInteger))) < 0) {
2318		PyObjC_INITERROR();
2319	}
2320	if (PyModule_AddObject(m, "_C_NSUInteger", PyBytes_FromString(@encode(NSUInteger))) < 0) {
2321		PyObjC_INITERROR();
2322	}
2323	if (PyModule_AddObject(m, "_C_CFIndex", PyBytes_FromString(@encode(CFIndex))) < 0) {
2324		PyObjC_INITERROR();
2325	}
2326	if (PyModule_AddObject(m, "_C_CGFloat", PyBytes_FromString(@encode(CGFloat))) < 0) {
2327		PyObjC_INITERROR();
2328	}
2329
2330
2331	if (PyModule_AddIntConstant(m, "_size_sockaddr_ip4", sizeof(struct sockaddr_in)) < 0) {
2332		PyObjC_INITERROR();
2333	}
2334	if (PyModule_AddIntConstant(m, "_size_sockaddr_ip6", sizeof(struct sockaddr_in6)) < 0) {
2335		PyObjC_INITERROR();
2336	}
2337
2338
2339	if (PyModule_AddStringConstant(m, "__version__", OBJC_VERSION) < 0) {
2340		PyObjC_INITERROR();
2341	}
2342
2343	if (PyModule_AddObject(m, "_sockaddr_type", PyBytes_FromString(@encode(struct sockaddr))) < 0) {
2344		PyObjC_INITERROR();
2345	}
2346
2347	PyObjCPointerWrapper_Init();
2348	PyObjC_InstallAllocHack();
2349
2350#if    PyObjC_BUILD_RELEASE >= 1006
2351	if (objc_setAssociatedObject != NULL) {
2352		if (PyModule_AddIntConstant(m, "OBJC_ASSOCIATION_ASSIGN", OBJC_ASSOCIATION_ASSIGN) < 0) {
2353			PyObjC_INITERROR();
2354		}
2355		if (PyModule_AddIntConstant(m, "OBJC_ASSOCIATION_RETAIN_NONATOMIC", OBJC_ASSOCIATION_RETAIN_NONATOMIC) < 0) {
2356			PyObjC_INITERROR();
2357		}
2358		if (PyModule_AddIntConstant(m, "OBJC_ASSOCIATION_COPY_NONATOMIC", OBJC_ASSOCIATION_COPY_NONATOMIC) < 0) {
2359			PyObjC_INITERROR();
2360		}
2361		if (PyModule_AddIntConstant(m, "OBJC_ASSOCIATION_RETAIN", OBJC_ASSOCIATION_RETAIN) < 0) {
2362			PyObjC_INITERROR();
2363		}
2364		if (PyModule_AddIntConstant(m, "OBJC_ASSOCIATION_COPY", OBJC_ASSOCIATION_COPY) < 0) {
2365			PyObjC_INITERROR();
2366		}
2367	} else {
2368		/* Build on a system where object associations are available, running on a platform where they aren't.
2369		 * Disable the wrappers.
2370		 */
2371		if (PyDict_DelItemString(d, "setAssociatedObject") < 0) {
2372			PyErr_Clear();
2373		}
2374		if (PyDict_DelItemString(d, "getAssociatedObject") < 0) {
2375			PyErr_Clear();
2376		}
2377		if (PyDict_DelItemString(d, "removeAssociatedObjects") < 0) {
2378			PyErr_Clear();
2379		}
2380
2381	}
2382#endif /* PyObjC_BUILD_RELEASE >= 1006 */
2383
2384
2385
2386#ifdef MAC_OS_X_VERSION_MAX_ALLOWED
2387	/* An easy way to check for the MacOS X version we did build for */
2388	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_MAX_ALLOWED", MAC_OS_X_VERSION_MAX_ALLOWED) < 0) {
2389		PyObjC_INITERROR();
2390	}
2391#endif /* MAC_OS_X_VERSION_MAX_ALLOWED */
2392
2393#ifdef MAC_OS_X_VERSION_MIN_REQUIRED
2394	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_MIN_REQUIRED", MAC_OS_X_VERSION_MAX_ALLOWED) < 0) {
2395		PyObjC_INITERROR();
2396	}
2397#endif /* MAC_OS_X_VERSION_MIN_REQUIRED */
2398
2399#ifdef MAC_OS_X_VERSION_10_1
2400	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_1", MAC_OS_X_VERSION_10_1) < 0) {
2401		PyObjC_INITERROR();
2402	}
2403#endif /* MAC_OS_X_VERSION_10_1 */
2404
2405#ifdef MAC_OS_X_VERSION_10_2
2406	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_2", MAC_OS_X_VERSION_10_2) < 0) {
2407		PyObjC_INITERROR();
2408	}
2409#endif /* MAC_OS_X_VERSION_10_2 */
2410
2411#ifdef MAC_OS_X_VERSION_10_3
2412	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_3", MAC_OS_X_VERSION_10_3) < 0) {
2413		PyObjC_INITERROR();
2414	}
2415#endif /* MAC_OS_X_VERSION_10_3 */
2416
2417#ifdef MAC_OS_X_VERSION_10_4
2418	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_4", MAC_OS_X_VERSION_10_4) < 0) {
2419		PyObjC_INITERROR();
2420	}
2421#endif /* MAC_OS_X_VERSION_10_4 */
2422
2423#ifdef MAC_OS_X_VERSION_10_5
2424	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_5", MAC_OS_X_VERSION_10_5) < 0) {
2425		PyObjC_INITERROR();
2426	}
2427#endif /* MAC_OS_X_VERSION_10_5 */
2428
2429#ifdef MAC_OS_X_VERSION_10_6
2430	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_6", MAC_OS_X_VERSION_10_6) < 0) {
2431		PyObjC_INITERROR();
2432	}
2433#endif /* MAC_OS_X_VERSION_10_6 */
2434
2435#ifdef MAC_OS_X_VERSION_10_7
2436	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_7", MAC_OS_X_VERSION_10_7) < 0) {
2437		PyObjC_INITERROR();
2438	}
2439#endif /* MAC_OS_X_VERSION_10_7 */
2440
2441#ifdef MAC_OS_X_VERSION_10_8
2442	if (PyModule_AddIntConstant(m, "MAC_OS_X_VERSION_10_8", MAC_OS_X_VERSION_10_8) < 0) {
2443		PyObjC_INITERROR();
2444	}
2445#endif /* MAC_OS_X_VERSION_10_8 */
2446
2447	if (PyModule_AddStringConstant(m, "platform", "MACOSX") < 0) {
2448		PyObjC_INITERROR();
2449	}
2450
2451	PyEval_InitThreads();
2452	if (![NSThread isMultiThreaded]) {
2453		[NSThread detachNewThreadSelector:@selector(targetForBecomingMultiThreaded:) toTarget:[OC_NSAutoreleasePoolCollector class] withObject:nil];
2454	}
2455	[initReleasePool release];
2456	/* Allocate an auto-release pool for our own use, this avoids numerous
2457	 * warnings during startup of a python script.
2458	 */
2459	global_release_pool = [[NSAutoreleasePool alloc] init];
2460	[OC_NSAutoreleasePoolCollector newAutoreleasePool];
2461
2462#ifndef Py_ARG_BYTES
2463#error "No Py_ARG_BYTES"
2464#endif
2465
2466#ifndef Py_ARG_NSInteger
2467#error "No Py_ARG_NSInteger"
2468#endif
2469
2470#ifndef Py_ARG_NSUInteger
2471#error "No Py_ARG_NSUInteger"
2472#endif
2473
2474	PyObjC_INITDONE();
2475}
2476