cfsupport.py revision ce17cf6da7544c90acffaa21682b527d1996506d
1# This script generates a Python interface for an Apple Macintosh Manager.
2# It uses the "bgen" package to generate C code.
3# The function specifications are generated by scanning the mamager's header file,
4# using the "scantools" package (customized for this particular manager).
5
6#error missing SetActionFilter
7
8import string
9
10# Declarations that change for each manager
11MODNAME = '_CF'				# The name of the module
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = 'CF'			# The prefix for module-wide routines
15INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
16OUTPUTFILE = MODNAME + "module.c"	# The file generated by this program
17
18from macsupport import *
19
20# Special case generator for the functions that have an AllocatorRef first argument,
21# which we skip anyway, and the object as the second arg.
22class MethodSkipArg1(MethodGenerator):
23	"""Similar to MethodGenerator, but has self as last argument"""
24
25	def parseArgumentList(self, args):
26		if len(args) < 2:
27			raise ValueError, "MethodSkipArg1 expects at least 2 args"
28		a0, a1, args = args[0], args[1], args[2:]
29		t0, n0, m0 = a0
30		if t0 != "CFAllocatorRef" and m0 != InMode:
31			raise ValueError, "MethodSkipArg1 should have dummy AllocatorRef first arg"
32		t1, n1, m1 = a1
33		if m1 != InMode:
34			raise ValueError, "method's 'self' must be 'InMode'"
35		dummy = Variable(t0, n0, m0)
36		self.argumentList.append(dummy)
37		self.itself = Variable(t1, "_self->ob_itself", SelfMode)
38		self.argumentList.append(self.itself)
39		FunctionGenerator.parseArgumentList(self, args)
40
41
42# Create the type objects
43
44includestuff = includestuff + """
45#include <CoreServices/CoreServices.h>
46
47#include "pycfbridge.h"
48
49#ifdef USE_TOOLBOX_OBJECT_GLUE
50extern PyObject *_CFObj_New(CFTypeRef);
51extern int _CFObj_Convert(PyObject *, CFTypeRef *);
52#define CFObj_New _CFObj_New
53#define CFObj_Convert _CFObj_Convert
54
55extern PyObject *_CFTypeRefObj_New(CFTypeRef);
56extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *);
57#define CFTypeRefObj_New _CFTypeRefObj_New
58#define CFTypeRefObj_Convert _CFTypeRefObj_Convert
59
60extern PyObject *_CFStringRefObj_New(CFStringRef);
61extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
62#define CFStringRefObj_New _CFStringRefObj_New
63#define CFStringRefObj_Convert _CFStringRefObj_Convert
64
65extern PyObject *_CFMutableStringRefObj_New(CFMutableStringRef);
66extern int _CFMutableStringRefObj_Convert(PyObject *, CFMutableStringRef *);
67#define CFMutableStringRefObj_New _CFMutableStringRefObj_New
68#define CFMutableStringRefObj_Convert _CFMutableStringRefObj_Convert
69
70extern PyObject *_CFArrayRefObj_New(CFArrayRef);
71extern int _CFArrayRefObj_Convert(PyObject *, CFArrayRef *);
72#define CFArrayRefObj_New _CFArrayRefObj_New
73#define CFArrayRefObj_Convert _CFArrayRefObj_Convert
74
75extern PyObject *_CFMutableArrayRefObj_New(CFMutableArrayRef);
76extern int _CFMutableArrayRefObj_Convert(PyObject *, CFMutableArrayRef *);
77#define CFMutableArrayRefObj_New _CFMutableArrayRefObj_New
78#define CFMutableArrayRefObj_Convert _CFMutableArrayRefObj_Convert
79
80extern PyObject *_CFDataRefObj_New(CFDataRef);
81extern int _CFDataRefObj_Convert(PyObject *, CFDataRef *);
82#define CFDataRefObj_New _CFDataRefObj_New
83#define CFDataRefObj_Convert _CFDataRefObj_Convert
84
85extern PyObject *_CFMutableDataRefObj_New(CFMutableDataRef);
86extern int _CFMutableDataRefObj_Convert(PyObject *, CFMutableDataRef *);
87#define CFMutableDataRefObj_New _CFMutableDataRefObj_New
88#define CFMutableDataRefObj_Convert _CFMutableDataRefObj_Convert
89
90extern PyObject *_CFDictionaryRefObj_New(CFDictionaryRef);
91extern int _CFDictionaryRefObj_Convert(PyObject *, CFDictionaryRef *);
92#define CFDictionaryRefObj_New _CFDictionaryRefObj_New
93#define CFDictionaryRefObj_Convert _CFDictionaryRefObj_Convert
94
95extern PyObject *_CFMutableDictionaryRefObj_New(CFMutableDictionaryRef);
96extern int _CFMutableDictionaryRefObj_Convert(PyObject *, CFMutableDictionaryRef *);
97#define CFMutableDictionaryRefObj_New _CFMutableDictionaryRefObj_New
98#define CFMutableDictionaryRefObj_Convert _CFMutableDictionaryRefObj_Convert
99
100extern PyObject *_CFURLRefObj_New(CFURLRef);
101extern int _CFURLRefObj_Convert(PyObject *, CFURLRef *);
102extern int _OptionalCFURLRefObj_Convert(PyObject *, CFURLRef *);
103#define CFURLRefObj_New _CFURLRefObj_New
104#define CFURLRefObj_Convert _CFURLRefObj_Convert
105#define OptionalCFURLRefObj_Convert _OptionalCFURLRefObj_Convert
106#endif
107
108/*
109** Parse/generate CFRange records
110*/
111PyObject *CFRange_New(CFRange *itself)
112{
113
114	return Py_BuildValue("ll", (long)itself->location, (long)itself->length);
115}
116
117int
118CFRange_Convert(PyObject *v, CFRange *p_itself)
119{
120	long location, length;
121
122	if( !PyArg_ParseTuple(v, "ll", &location, &length) )
123		return 0;
124	p_itself->location = (CFIndex)location;
125	p_itself->length = (CFIndex)length;
126	return 1;
127}
128
129/* Optional CFURL argument or None (passed as NULL) */
130int
131OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself)
132{
133    if ( v == Py_None ) {
134    	p_itself = NULL;
135    	return 1;
136    }
137    return CFURLRefObj_Convert(v, p_itself);
138}
139"""
140
141finalstuff = finalstuff + """
142
143/* Routines to convert any CF type to/from the corresponding CFxxxObj */
144PyObject *CFObj_New(CFTypeRef itself)
145{
146	if (itself == NULL)
147	{
148		PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
149		return NULL;
150	}
151	if (CFGetTypeID(itself) == CFArrayGetTypeID()) return CFArrayRefObj_New((CFArrayRef)itself);
152	if (CFGetTypeID(itself) == CFDictionaryGetTypeID()) return CFDictionaryRefObj_New((CFDictionaryRef)itself);
153	if (CFGetTypeID(itself) == CFDataGetTypeID()) return CFDataRefObj_New((CFDataRef)itself);
154	if (CFGetTypeID(itself) == CFStringGetTypeID()) return CFStringRefObj_New((CFStringRef)itself);
155	if (CFGetTypeID(itself) == CFURLGetTypeID()) return CFURLRefObj_New((CFURLRef)itself);
156	/* XXXX Or should we use PyCF_CF2Python here?? */
157	return CFTypeRefObj_New(itself);
158}
159int CFObj_Convert(PyObject *v, CFTypeRef *p_itself)
160{
161
162	if (v == Py_None) { *p_itself = NULL; return 1; }
163	/* Check for other CF objects here */
164
165	if (!CFTypeRefObj_Check(v) &&
166		!CFArrayRefObj_Check(v) &&
167		!CFMutableArrayRefObj_Check(v) &&
168		!CFDictionaryRefObj_Check(v) &&
169		!CFMutableDictionaryRefObj_Check(v) &&
170		!CFDataRefObj_Check(v) &&
171		!CFMutableDataRefObj_Check(v) &&
172		!CFStringRefObj_Check(v) &&
173		!CFMutableStringRefObj_Check(v) &&
174		!CFURLRefObj_Check(v) )
175	{
176		/* XXXX Or should we use PyCF_Python2CF here?? */
177		PyErr_SetString(PyExc_TypeError, "CF object required");
178		return 0;
179	}
180	*p_itself = ((CFTypeRefObject *)v)->ob_itself;
181	return 1;
182}
183"""
184
185initstuff = initstuff + """
186PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFObj_New);
187PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFObj_Convert);
188PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFTypeRefObj_New);
189PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFTypeRefObj_Convert);
190PyMac_INIT_TOOLBOX_OBJECT_NEW(CFStringRef, CFStringRefObj_New);
191PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFStringRef, CFStringRefObj_Convert);
192PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableStringRef, CFMutableStringRefObj_New);
193PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert);
194PyMac_INIT_TOOLBOX_OBJECT_NEW(CFArrayRef, CFArrayRefObj_New);
195PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFArrayRef, CFArrayRefObj_Convert);
196PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New);
197PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert);
198PyMac_INIT_TOOLBOX_OBJECT_NEW(CFDictionaryRef, CFDictionaryRefObj_New);
199PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert);
200PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New);
201PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert);
202PyMac_INIT_TOOLBOX_OBJECT_NEW(CFURLRef, CFURLRefObj_New);
203PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
204"""
205
206variablestuff="""
207#define _STRINGCONST(name) PyModule_AddObject(m, #name, CFStringRefObj_New(name))
208_STRINGCONST(kCFPreferencesAnyApplication);
209_STRINGCONST(kCFPreferencesCurrentApplication);
210_STRINGCONST(kCFPreferencesAnyHost);
211_STRINGCONST(kCFPreferencesCurrentHost);
212_STRINGCONST(kCFPreferencesAnyUser);
213_STRINGCONST(kCFPreferencesCurrentUser);
214
215"""
216
217Boolean = Type("Boolean", "l")
218CFTypeID = Type("CFTypeID", "l") # XXXX a guess, seems better than OSTypeType.
219CFHashCode = Type("CFHashCode", "l")
220CFIndex = Type("CFIndex", "l")
221CFRange = OpaqueByValueType('CFRange', 'CFRange')
222CFOptionFlags = Type("CFOptionFlags", "l")
223CFStringEncoding = Type("CFStringEncoding", "l")
224CFComparisonResult = Type("CFComparisonResult", "l")  # a bit dangerous, it's an enum
225CFURLPathStyle = Type("CFURLPathStyle", "l") #  a bit dangerous, it's an enum
226
227char_ptr = stringptr
228return_stringptr = Type("char *", "s")	# ONLY FOR RETURN VALUES!!
229
230CFAllocatorRef = FakeType("(CFAllocatorRef)NULL")
231CFArrayCallBacks_ptr = FakeType("&kCFTypeArrayCallBacks")
232CFDictionaryKeyCallBacks_ptr = FakeType("&kCFTypeDictionaryKeyCallBacks")
233CFDictionaryValueCallBacks_ptr = FakeType("&kCFTypeDictionaryValueCallBacks")
234# The real objects
235CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
236CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
237CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
238CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
239CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
240CFDataRef = OpaqueByValueType("CFDataRef", "CFDataRefObj")
241CFMutableDataRef = OpaqueByValueType("CFMutableDataRef", "CFMutableDataRefObj")
242CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
243CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
244CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
245CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
246CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
247OptionalCFURLRef  = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
248##CFPropertyListRef = OpaqueByValueType("CFPropertyListRef", "CFTypeRefObj")
249# ADD object type here
250
251# Our (opaque) objects
252
253class MyGlobalObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
254	def outputCheckNewArg(self):
255		Output('if (itself == NULL)')
256		OutLbrace()
257		Output('PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");')
258		Output('return NULL;')
259		OutRbrace()
260	def outputStructMembers(self):
261		GlobalObjectDefinition.outputStructMembers(self)
262		Output("void (*ob_freeit)(CFTypeRef ptr);")
263	def outputInitStructMembers(self):
264		GlobalObjectDefinition.outputInitStructMembers(self)
265##		Output("it->ob_freeit = NULL;")
266		Output("it->ob_freeit = CFRelease;")
267	def outputCheckConvertArg(self):
268		Out("""
269		if (v == Py_None) { *p_itself = NULL; return 1; }
270		/* Check for other CF objects here */
271		""")
272	def outputCleanupStructMembers(self):
273		Output("if (self->ob_freeit && self->ob_itself)")
274		OutLbrace()
275		Output("self->ob_freeit((CFTypeRef)self->ob_itself);")
276		Output("self->ob_itself = NULL;")
277		OutRbrace()
278
279	def outputCompare(self):
280		Output()
281		Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
282		OutLbrace()
283		Output("/* XXXX Or should we use CFEqual?? */")
284		Output("if ( self->ob_itself > other->ob_itself ) return 1;")
285		Output("if ( self->ob_itself < other->ob_itself ) return -1;")
286		Output("return 0;")
287		OutRbrace()
288
289	def outputHash(self):
290		Output()
291		Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
292		OutLbrace()
293		Output("/* XXXX Or should we use CFHash?? */")
294		Output("return (int)self->ob_itself;")
295		OutRbrace()
296
297	def outputRepr(self):
298		Output()
299		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
300		OutLbrace()
301		Output("char buf[100];")
302		Output("""sprintf(buf, "<CFTypeRef type-%%d object at 0x%%8.8x for 0x%%8.8x>", (int)CFGetTypeID(self->ob_itself), (unsigned)self, (unsigned)self->ob_itself);""")
303		Output("return PyString_FromString(buf);")
304		OutRbrace()
305
306	def output_tp_newBody(self):
307		Output("PyObject *self;")
308		Output
309		Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
310		Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
311		Output("((%s *)self)->ob_freeit = CFRelease;", self.objecttype)
312		Output("return self;")
313
314	def output_tp_initBody(self):
315		Output("%s itself;", self.itselftype)
316		Output("char *kw[] = {\"itself\", 0};")
317		Output()
318		Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself))",
319			self.prefix)
320		OutLbrace()
321		Output("((%s *)self)->ob_itself = itself;", self.objecttype)
322		Output("return 0;")
323		OutRbrace()
324		if self.prefix != 'CFTypeRefObj':
325			Output()
326			Output("/* Any CFTypeRef descendent is allowed as initializer too */")
327			Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, CFTypeRefObj_Convert, &itself))")
328			OutLbrace()
329			Output("((%s *)self)->ob_itself = itself;", self.objecttype)
330			Output("return 0;")
331			OutRbrace()
332		Output("return -1;")
333
334class CFTypeRefObjectDefinition(MyGlobalObjectDefinition):
335	pass
336
337class CFArrayRefObjectDefinition(MyGlobalObjectDefinition):
338	basetype = "CFTypeRef_Type"
339
340	def outputRepr(self):
341		Output()
342		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
343		OutLbrace()
344		Output("char buf[100];")
345		Output("""sprintf(buf, "<CFArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
346		Output("return PyString_FromString(buf);")
347		OutRbrace()
348
349class CFMutableArrayRefObjectDefinition(MyGlobalObjectDefinition):
350	basetype = "CFArrayRef_Type"
351
352	def outputRepr(self):
353		Output()
354		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
355		OutLbrace()
356		Output("char buf[100];")
357		Output("""sprintf(buf, "<CFMutableArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
358		Output("return PyString_FromString(buf);")
359		OutRbrace()
360
361class CFDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
362	basetype = "CFTypeRef_Type"
363
364	def outputRepr(self):
365		Output()
366		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
367		OutLbrace()
368		Output("char buf[100];")
369		Output("""sprintf(buf, "<CFDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
370		Output("return PyString_FromString(buf);")
371		OutRbrace()
372
373class CFMutableDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
374	basetype = "CFDictionaryRef_Type"
375
376	def outputRepr(self):
377		Output()
378		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
379		OutLbrace()
380		Output("char buf[100];")
381		Output("""sprintf(buf, "<CFMutableDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
382		Output("return PyString_FromString(buf);")
383		OutRbrace()
384
385class CFDataRefObjectDefinition(MyGlobalObjectDefinition):
386	basetype = "CFTypeRef_Type"
387
388	def outputCheckConvertArg(self):
389		Out("""
390		if (v == Py_None) { *p_itself = NULL; return 1; }
391		if (PyString_Check(v)) {
392		    char *cStr;
393		    int cLen;
394		    if( PyString_AsStringAndSize(v, &cStr, &cLen) < 0 ) return 0;
395		    *p_itself = CFDataCreate((CFAllocatorRef)NULL, (unsigned char *)cStr, cLen);
396		    return 1;
397		}
398		""")
399
400	def outputRepr(self):
401		Output()
402		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
403		OutLbrace()
404		Output("char buf[100];")
405		Output("""sprintf(buf, "<CFDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
406		Output("return PyString_FromString(buf);")
407		OutRbrace()
408
409class CFMutableDataRefObjectDefinition(MyGlobalObjectDefinition):
410	basetype = "CFDataRef_Type"
411
412	def outputRepr(self):
413		Output()
414		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
415		OutLbrace()
416		Output("char buf[100];")
417		Output("""sprintf(buf, "<CFMutableDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
418		Output("return PyString_FromString(buf);")
419		OutRbrace()
420
421class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
422	basetype = "CFTypeRef_Type"
423
424	def outputCheckConvertArg(self):
425		Out("""
426		if (v == Py_None) { *p_itself = NULL; return 1; }
427		if (PyString_Check(v)) {
428		    char *cStr;
429		    if (!PyArg_Parse(v, "es", "ascii", &cStr))
430		    	return NULL;
431			*p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, kCFStringEncodingASCII);
432			return 1;
433		}
434		if (PyUnicode_Check(v)) {
435			/* We use the CF types here, if Python was configured differently that will give an error */
436			CFIndex size = PyUnicode_GetSize(v);
437			UniChar *unichars = PyUnicode_AsUnicode(v);
438			if (!unichars) return 0;
439			*p_itself = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
440			return 1;
441		}
442
443		""")
444
445	def outputRepr(self):
446		Output()
447		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
448		OutLbrace()
449		Output("char buf[100];")
450		Output("""sprintf(buf, "<CFStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
451		Output("return PyString_FromString(buf);")
452		OutRbrace()
453
454class CFMutableStringRefObjectDefinition(CFStringRefObjectDefinition):
455	basetype = "CFStringRef_Type"
456
457	def outputCheckConvertArg(self):
458		# Mutable, don't allow Python strings
459		return MyGlobalObjectDefinition.outputCheckConvertArg(self)
460
461	def outputRepr(self):
462		Output()
463		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
464		OutLbrace()
465		Output("char buf[100];")
466		Output("""sprintf(buf, "<CFMutableStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
467		Output("return PyString_FromString(buf);")
468		OutRbrace()
469
470class CFURLRefObjectDefinition(MyGlobalObjectDefinition):
471	basetype = "CFTypeRef_Type"
472
473	def outputRepr(self):
474		Output()
475		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
476		OutLbrace()
477		Output("char buf[100];")
478		Output("""sprintf(buf, "<CFURL object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
479		Output("return PyString_FromString(buf);")
480		OutRbrace()
481
482
483# ADD object class here
484
485# From here on it's basically all boiler plate...
486
487# Create the generator groups and link them
488module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
489CFTypeRef_object = CFTypeRefObjectDefinition('CFTypeRef', 'CFTypeRefObj', 'CFTypeRef')
490CFArrayRef_object = CFArrayRefObjectDefinition('CFArrayRef', 'CFArrayRefObj', 'CFArrayRef')
491CFMutableArrayRef_object = CFMutableArrayRefObjectDefinition('CFMutableArrayRef', 'CFMutableArrayRefObj', 'CFMutableArrayRef')
492CFDictionaryRef_object = CFDictionaryRefObjectDefinition('CFDictionaryRef', 'CFDictionaryRefObj', 'CFDictionaryRef')
493CFMutableDictionaryRef_object = CFMutableDictionaryRefObjectDefinition('CFMutableDictionaryRef', 'CFMutableDictionaryRefObj', 'CFMutableDictionaryRef')
494CFDataRef_object = CFDataRefObjectDefinition('CFDataRef', 'CFDataRefObj', 'CFDataRef')
495CFMutableDataRef_object = CFMutableDataRefObjectDefinition('CFMutableDataRef', 'CFMutableDataRefObj', 'CFMutableDataRef')
496CFStringRef_object = CFStringRefObjectDefinition('CFStringRef', 'CFStringRefObj', 'CFStringRef')
497CFMutableStringRef_object = CFMutableStringRefObjectDefinition('CFMutableStringRef', 'CFMutableStringRefObj', 'CFMutableStringRef')
498CFURLRef_object = CFURLRefObjectDefinition('CFURLRef', 'CFURLRefObj', 'CFURLRef')
499
500# ADD object here
501
502module.addobject(CFTypeRef_object)
503module.addobject(CFArrayRef_object)
504module.addobject(CFMutableArrayRef_object)
505module.addobject(CFDictionaryRef_object)
506module.addobject(CFMutableDictionaryRef_object)
507module.addobject(CFDataRef_object)
508module.addobject(CFMutableDataRef_object)
509module.addobject(CFStringRef_object)
510module.addobject(CFMutableStringRef_object)
511module.addobject(CFURLRef_object)
512# ADD addobject call here
513
514# Create the generator classes used to populate the lists
515Function = OSErrWeakLinkFunctionGenerator
516Method = OSErrWeakLinkMethodGenerator
517
518# Create and populate the lists
519functions = []
520CFTypeRef_methods = []
521CFArrayRef_methods = []
522CFMutableArrayRef_methods = []
523CFDictionaryRef_methods = []
524CFMutableDictionaryRef_methods = []
525CFDataRef_methods = []
526CFMutableDataRef_methods = []
527CFStringRef_methods = []
528CFMutableStringRef_methods = []
529CFURLRef_methods = []
530
531# ADD _methods initializer here
532execfile(INPUTFILE)
533
534
535# add the populated lists to the generator groups
536# (in a different wordl the scan program would generate this)
537for f in functions: module.add(f)
538for f in CFTypeRef_methods: CFTypeRef_object.add(f)
539for f in CFArrayRef_methods: CFArrayRef_object.add(f)
540for f in CFMutableArrayRef_methods: CFMutableArrayRef_object.add(f)
541for f in CFDictionaryRef_methods: CFDictionaryRef_object.add(f)
542for f in CFMutableDictionaryRef_methods: CFMutableDictionaryRef_object.add(f)
543for f in CFDataRef_methods: CFDataRef_object.add(f)
544for f in CFMutableDataRef_methods: CFMutableDataRef_object.add(f)
545for f in CFStringRef_methods: CFStringRef_object.add(f)
546for f in CFMutableStringRef_methods: CFMutableStringRef_object.add(f)
547for f in CFURLRef_methods: CFURLRef_object.add(f)
548
549# Manual generators for getting data out of strings
550
551getasstring_body = """
552int size = CFStringGetLength(_self->ob_itself)+1;
553char *data = malloc(size);
554
555if( data == NULL ) return PyErr_NoMemory();
556if ( CFStringGetCString(_self->ob_itself, data, size, 0) ) {
557	_res = (PyObject *)PyString_FromString(data);
558} else {
559	PyErr_SetString(PyExc_RuntimeError, "CFStringGetCString could not fit the string");
560	_res = NULL;
561}
562free(data);
563return _res;
564"""
565
566f = ManualGenerator("CFStringGetString", getasstring_body);
567f.docstring = lambda: "() -> (string _rv)"
568CFStringRef_object.add(f)
569
570getasunicode_body = """
571int size = CFStringGetLength(_self->ob_itself)+1;
572Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
573CFRange range;
574
575range.location = 0;
576range.length = size;
577if( data == NULL ) return PyErr_NoMemory();
578CFStringGetCharacters(_self->ob_itself, range, data);
579_res = (PyObject *)PyUnicode_FromUnicode(data, size-1);
580free(data);
581return _res;
582"""
583
584f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
585f.docstring = lambda: "() -> (unicode _rv)"
586CFStringRef_object.add(f)
587
588# Get data from CFDataRef
589getasdata_body = """
590int size = CFDataGetLength(_self->ob_itself);
591char *data = (char *)CFDataGetBytePtr(_self->ob_itself);
592
593_res = (PyObject *)PyString_FromStringAndSize(data, size);
594return _res;
595"""
596
597f = ManualGenerator("CFDataGetData", getasdata_body);
598f.docstring = lambda: "() -> (string _rv)"
599CFDataRef_object.add(f)
600
601# Manual generator for CFPropertyListCreateFromXMLData because of funny error return
602fromxml_body = """
603CFTypeRef _rv;
604CFOptionFlags mutabilityOption;
605CFStringRef errorString;
606if (!PyArg_ParseTuple(_args, "l",
607                      &mutabilityOption))
608	return NULL;
609_rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
610                                      _self->ob_itself,
611                                      mutabilityOption,
612                                      &errorString);
613if (errorString)
614	CFRelease(errorString);
615if (_rv == NULL) {
616	PyErr_SetString(PyExc_RuntimeError, "Parse error in XML data");
617	return NULL;
618}
619_res = Py_BuildValue("O&",
620                     CFTypeRefObj_New, _rv);
621return _res;
622"""
623f = ManualGenerator("CFPropertyListCreateFromXMLData", fromxml_body)
624f.docstring = lambda: "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"
625CFTypeRef_object.add(f)
626
627# Convert CF objects to Python objects
628toPython_body = """
629_res = PyCF_CF2Python(_self->ob_itself);
630return _res;
631"""
632
633f = ManualGenerator("toPython", toPython_body);
634f.docstring = lambda: "() -> (python_object)"
635CFTypeRef_object.add(f)
636
637toCF_body = """
638CFTypeRef rv;
639CFTypeID typeid;
640
641if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv))
642	return NULL;
643typeid = CFGetTypeID(rv);
644
645if (typeid == CFStringGetTypeID())
646	return Py_BuildValue("O&", CFStringRefObj_New, rv);
647if (typeid == CFArrayGetTypeID())
648	return Py_BuildValue("O&", CFArrayRefObj_New, rv);
649if (typeid == CFDictionaryGetTypeID())
650	return Py_BuildValue("O&", CFDictionaryRefObj_New, rv);
651if (typeid == CFURLGetTypeID())
652	return Py_BuildValue("O&", CFURLRefObj_New, rv);
653
654_res = Py_BuildValue("O&", CFTypeRefObj_New, rv);
655return _res;
656"""
657f = ManualGenerator("toCF", toCF_body);
658f.docstring = lambda: "(python_object) -> (CF_object)"
659module.add(f)
660
661# ADD add forloop here
662
663# generate output (open the output file as late as possible)
664SetOutputFileName(OUTPUTFILE)
665module.generate()
666
667