Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Mac / Modules / cf / cfsupport.py
blob8aed6c2ce028e43ec85e48e8e27902622e634998
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).
6 #error missing SetActionFilter
8 import string
10 # Declarations that change for each manager
11 MODNAME = '_CF' # The name of the module
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX = 'CF' # The prefix for module-wide routines
15 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
16 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
18 from macsupport import *
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.
22 class MethodSkipArg1(MethodGenerator):
23 """Similar to MethodGenerator, but has self as last argument"""
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)
42 # Create the type objects
44 includestuff = includestuff + """
45 #ifdef WITHOUT_FRAMEWORKS
46 #include <CFBase.h>
47 #include <CFArray.h>
48 #include <CFData.h>
49 #include <CFDictionary.h>
50 #include <CFString.h>
51 #include <CFURL.h>
52 #include <CFPropertyList.h>
53 #include <CFPreferences.h>
54 #else
55 #include <CoreServices/CoreServices.h>
56 #endif
58 #include "pycfbridge.h"
60 #ifdef USE_TOOLBOX_OBJECT_GLUE
61 extern PyObject *_CFTypeRefObj_New(CFTypeRef);
62 extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *);
63 #define CFTypeRefObj_New _CFTypeRefObj_New
64 #define CFTypeRefObj_Convert _CFTypeRefObj_Convert
66 extern PyObject *_CFStringRefObj_New(CFStringRef);
67 extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
68 #define CFStringRefObj_New _CFStringRefObj_New
69 #define CFStringRefObj_Convert _CFStringRefObj_Convert
71 extern PyObject *_CFMutableStringRefObj_New(CFMutableStringRef);
72 extern int _CFMutableStringRefObj_Convert(PyObject *, CFMutableStringRef *);
73 #define CFMutableStringRefObj_New _CFMutableStringRefObj_New
74 #define CFMutableStringRefObj_Convert _CFMutableStringRefObj_Convert
76 extern PyObject *_CFArrayRefObj_New(CFArrayRef);
77 extern int _CFArrayRefObj_Convert(PyObject *, CFArrayRef *);
78 #define CFArrayRefObj_New _CFArrayRefObj_New
79 #define CFArrayRefObj_Convert _CFArrayRefObj_Convert
81 extern PyObject *_CFMutableArrayRefObj_New(CFMutableArrayRef);
82 extern int _CFMutableArrayRefObj_Convert(PyObject *, CFMutableArrayRef *);
83 #define CFMutableArrayRefObj_New _CFMutableArrayRefObj_New
84 #define CFMutableArrayRefObj_Convert _CFMutableArrayRefObj_Convert
86 extern PyObject *_CFDataRefObj_New(CFDataRef);
87 extern int _CFDataRefObj_Convert(PyObject *, CFDataRef *);
88 #define CFDataRefObj_New _CFDataRefObj_New
89 #define CFDataRefObj_Convert _CFDataRefObj_Convert
91 extern PyObject *_CFMutableDataRefObj_New(CFMutableDataRef);
92 extern int _CFMutableDataRefObj_Convert(PyObject *, CFMutableDataRef *);
93 #define CFMutableDataRefObj_New _CFMutableDataRefObj_New
94 #define CFMutableDataRefObj_Convert _CFMutableDataRefObj_Convert
96 extern PyObject *_CFDictionaryRefObj_New(CFDictionaryRef);
97 extern int _CFDictionaryRefObj_Convert(PyObject *, CFDictionaryRef *);
98 #define CFDictionaryRefObj_New _CFDictionaryRefObj_New
99 #define CFDictionaryRefObj_Convert _CFDictionaryRefObj_Convert
101 extern PyObject *_CFMutableDictionaryRefObj_New(CFMutableDictionaryRef);
102 extern int _CFMutableDictionaryRefObj_Convert(PyObject *, CFMutableDictionaryRef *);
103 #define CFMutableDictionaryRefObj_New _CFMutableDictionaryRefObj_New
104 #define CFMutableDictionaryRefObj_Convert _CFMutableDictionaryRefObj_Convert
106 extern PyObject *_CFURLRefObj_New(CFURLRef);
107 extern int _CFURLRefObj_Convert(PyObject *, CFURLRef *);
108 extern int _OptionalCFURLRefObj_Convert(PyObject *, CFURLRef *);
109 #define CFURLRefObj_New _CFURLRefObj_New
110 #define CFURLRefObj_Convert _CFURLRefObj_Convert
111 #define OptionalCFURLRefObj_Convert _OptionalCFURLRefObj_Convert
112 #endif
115 ** Parse/generate CFRange records
117 PyObject *CFRange_New(CFRange *itself)
120 return Py_BuildValue("ll", (long)itself->location, (long)itself->length);
124 CFRange_Convert(PyObject *v, CFRange *p_itself)
126 long location, length;
128 if( !PyArg_ParseTuple(v, "ll", &location, &length) )
129 return 0;
130 p_itself->location = (CFIndex)location;
131 p_itself->length = (CFIndex)length;
132 return 1;
135 /* Optional CFURL argument or None (passed as NULL) */
137 OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself)
139 if ( v == Py_None ) {
140 p_itself = NULL;
141 return 1;
143 return CFURLRefObj_Convert(v, p_itself);
148 initstuff = initstuff + """
149 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFTypeRefObj_New);
150 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFTypeRefObj_Convert);
151 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFStringRef, CFStringRefObj_New);
152 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFStringRef, CFStringRefObj_Convert);
153 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableStringRef, CFMutableStringRefObj_New);
154 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert);
156 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFArrayRef, CFArrayRefObj_New);
157 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFArrayRef, CFArrayRefObj_Convert);
158 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New);
159 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert);
160 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFDictionaryRef, CFDictionaryRefObj_New);
161 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert);
162 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New);
163 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert);
164 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFURLRef, CFURLRefObj_New);
165 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
166 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
169 variablestuff="""
170 #define _STRINGCONST(name) PyModule_AddObject(m, #name, CFStringRefObj_New(name))
171 _STRINGCONST(kCFPreferencesAnyApplication);
172 _STRINGCONST(kCFPreferencesCurrentApplication);
173 _STRINGCONST(kCFPreferencesAnyHost);
174 _STRINGCONST(kCFPreferencesCurrentHost);
175 _STRINGCONST(kCFPreferencesAnyUser);
176 _STRINGCONST(kCFPreferencesCurrentUser);
180 Boolean = Type("Boolean", "l")
181 CFTypeID = Type("CFTypeID", "l") # XXXX a guess, seems better than OSTypeType.
182 CFHashCode = Type("CFHashCode", "l")
183 CFIndex = Type("CFIndex", "l")
184 CFRange = OpaqueByValueType('CFRange', 'CFRange')
185 CFOptionFlags = Type("CFOptionFlags", "l")
186 CFStringEncoding = Type("CFStringEncoding", "l")
187 CFComparisonResult = Type("CFComparisonResult", "l") # a bit dangerous, it's an enum
188 CFURLPathStyle = Type("CFURLPathStyle", "l") # a bit dangerous, it's an enum
190 char_ptr = stringptr
191 return_stringptr = Type("char *", "s") # ONLY FOR RETURN VALUES!!
193 CFAllocatorRef = FakeType("(CFAllocatorRef)NULL")
194 CFArrayCallBacks_ptr = FakeType("&kCFTypeArrayCallBacks")
195 CFDictionaryKeyCallBacks_ptr = FakeType("&kCFTypeDictionaryKeyCallBacks")
196 CFDictionaryValueCallBacks_ptr = FakeType("&kCFTypeDictionaryValueCallBacks")
197 # The real objects
198 CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
199 CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
200 CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
201 CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
202 CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
203 CFDataRef = OpaqueByValueType("CFDataRef", "CFDataRefObj")
204 CFMutableDataRef = OpaqueByValueType("CFMutableDataRef", "CFMutableDataRefObj")
205 CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
206 CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
207 CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
208 CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
209 CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
210 OptionalCFURLRef = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
211 ##CFPropertyListRef = OpaqueByValueType("CFPropertyListRef", "CFTypeRefObj")
212 # ADD object type here
214 # Our (opaque) objects
216 class MyGlobalObjectDefinition(GlobalObjectDefinition):
217 def outputCheckNewArg(self):
218 Output('if (itself == NULL)')
219 OutLbrace()
220 Output('PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");')
221 Output('return NULL;')
222 OutRbrace()
223 def outputStructMembers(self):
224 GlobalObjectDefinition.outputStructMembers(self)
225 Output("void (*ob_freeit)(CFTypeRef ptr);")
226 def outputInitStructMembers(self):
227 GlobalObjectDefinition.outputInitStructMembers(self)
228 ## Output("it->ob_freeit = NULL;")
229 Output("it->ob_freeit = CFRelease;")
230 def outputCheckConvertArg(self):
231 Out("""
232 if (v == Py_None) { *p_itself = NULL; return 1; }
233 /* Check for other CF objects here */
234 """)
235 def outputCleanupStructMembers(self):
236 Output("if (self->ob_freeit && self->ob_itself)")
237 OutLbrace()
238 Output("self->ob_freeit((CFTypeRef)self->ob_itself);")
239 OutRbrace()
241 def outputCompare(self):
242 Output()
243 Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
244 OutLbrace()
245 Output("/* XXXX Or should we use CFEqual?? */")
246 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
247 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
248 Output("return 0;")
249 OutRbrace()
251 def outputHash(self):
252 Output()
253 Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
254 OutLbrace()
255 Output("/* XXXX Or should we use CFHash?? */")
256 Output("return (int)self->ob_itself;")
257 OutRbrace()
259 def outputRepr(self):
260 Output()
261 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
262 OutLbrace()
263 Output("char buf[100];")
264 Output("""sprintf(buf, "<CFTypeRef type-%%d object at 0x%%8.8x for 0x%%8.8x>", CFGetTypeID(self->ob_itself), (unsigned)self, (unsigned)self->ob_itself);""")
265 Output("return PyString_FromString(buf);")
266 OutRbrace()
268 class CFTypeRefObjectDefinition(MyGlobalObjectDefinition):
269 pass
271 class CFArrayRefObjectDefinition(MyGlobalObjectDefinition):
272 basechain = "&CFTypeRefObj_chain"
274 def outputRepr(self):
275 Output()
276 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
277 OutLbrace()
278 Output("char buf[100];")
279 Output("""sprintf(buf, "<CFArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
280 Output("return PyString_FromString(buf);")
281 OutRbrace()
283 class CFMutableArrayRefObjectDefinition(MyGlobalObjectDefinition):
284 basechain = "&CFArrayRefObj_chain"
286 def outputRepr(self):
287 Output()
288 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
289 OutLbrace()
290 Output("char buf[100];")
291 Output("""sprintf(buf, "<CFMutableArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
292 Output("return PyString_FromString(buf);")
293 OutRbrace()
295 class CFDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
296 basechain = "&CFTypeRefObj_chain"
298 def outputRepr(self):
299 Output()
300 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
301 OutLbrace()
302 Output("char buf[100];")
303 Output("""sprintf(buf, "<CFDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
304 Output("return PyString_FromString(buf);")
305 OutRbrace()
307 class CFMutableDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
308 basechain = "&CFDictionaryRefObj_chain"
310 def outputRepr(self):
311 Output()
312 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
313 OutLbrace()
314 Output("char buf[100];")
315 Output("""sprintf(buf, "<CFMutableDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
316 Output("return PyString_FromString(buf);")
317 OutRbrace()
319 class CFDataRefObjectDefinition(MyGlobalObjectDefinition):
320 basechain = "&CFTypeRefObj_chain"
322 def outputCheckConvertArg(self):
323 Out("""
324 if (v == Py_None) { *p_itself = NULL; return 1; }
325 if (PyString_Check(v)) {
326 char *cStr;
327 int cLen;
328 if( PyString_AsStringAndSize(v, &cStr, &cLen) < 0 ) return 0;
329 *p_itself = CFDataCreate((CFAllocatorRef)NULL, (unsigned char *)cStr, cLen);
330 return 1;
332 """)
334 def outputRepr(self):
335 Output()
336 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
337 OutLbrace()
338 Output("char buf[100];")
339 Output("""sprintf(buf, "<CFDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
340 Output("return PyString_FromString(buf);")
341 OutRbrace()
343 class CFMutableDataRefObjectDefinition(MyGlobalObjectDefinition):
344 basechain = "&CFDataRefObj_chain"
346 def outputRepr(self):
347 Output()
348 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
349 OutLbrace()
350 Output("char buf[100];")
351 Output("""sprintf(buf, "<CFMutableDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
352 Output("return PyString_FromString(buf);")
353 OutRbrace()
355 class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
356 basechain = "&CFTypeRefObj_chain"
358 def outputCheckConvertArg(self):
359 Out("""
360 if (v == Py_None) { *p_itself = NULL; return 1; }
361 if (PyString_Check(v)) {
362 char *cStr = PyString_AsString(v);
363 *p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, 0);
364 return 1;
366 if (PyUnicode_Check(v)) {
367 /* We use the CF types here, if Python was configured differently that will give an error */
368 CFIndex size = PyUnicode_GetSize(v);
369 UniChar *unichars = PyUnicode_AsUnicode(v);
370 if (!unichars) return 0;
371 *p_itself = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
372 return 1;
375 """)
377 def outputRepr(self):
378 Output()
379 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
380 OutLbrace()
381 Output("char buf[100];")
382 Output("""sprintf(buf, "<CFStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
383 Output("return PyString_FromString(buf);")
384 OutRbrace()
386 class CFMutableStringRefObjectDefinition(CFStringRefObjectDefinition):
387 basechain = "&CFStringRefObj_chain"
389 def outputCheckConvertArg(self):
390 # Mutable, don't allow Python strings
391 return MyGlobalObjectDefinition.outputCheckConvertArg(self)
393 def outputRepr(self):
394 Output()
395 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
396 OutLbrace()
397 Output("char buf[100];")
398 Output("""sprintf(buf, "<CFMutableStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
399 Output("return PyString_FromString(buf);")
400 OutRbrace()
402 class CFURLRefObjectDefinition(MyGlobalObjectDefinition):
403 basechain = "&CFTypeRefObj_chain"
405 def outputRepr(self):
406 Output()
407 Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
408 OutLbrace()
409 Output("char buf[100];")
410 Output("""sprintf(buf, "<CFURL object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
411 Output("return PyString_FromString(buf);")
412 OutRbrace()
415 # ADD object class here
417 # From here on it's basically all boiler plate...
419 # Create the generator groups and link them
420 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
421 CFTypeRef_object = CFTypeRefObjectDefinition('CFTypeRef', 'CFTypeRefObj', 'CFTypeRef')
422 CFArrayRef_object = CFArrayRefObjectDefinition('CFArrayRef', 'CFArrayRefObj', 'CFArrayRef')
423 CFMutableArrayRef_object = CFMutableArrayRefObjectDefinition('CFMutableArrayRef', 'CFMutableArrayRefObj', 'CFMutableArrayRef')
424 CFDictionaryRef_object = CFDictionaryRefObjectDefinition('CFDictionaryRef', 'CFDictionaryRefObj', 'CFDictionaryRef')
425 CFMutableDictionaryRef_object = CFMutableDictionaryRefObjectDefinition('CFMutableDictionaryRef', 'CFMutableDictionaryRefObj', 'CFMutableDictionaryRef')
426 CFDataRef_object = CFDataRefObjectDefinition('CFDataRef', 'CFDataRefObj', 'CFDataRef')
427 CFMutableDataRef_object = CFMutableDataRefObjectDefinition('CFMutableDataRef', 'CFMutableDataRefObj', 'CFMutableDataRef')
428 CFStringRef_object = CFStringRefObjectDefinition('CFStringRef', 'CFStringRefObj', 'CFStringRef')
429 CFMutableStringRef_object = CFMutableStringRefObjectDefinition('CFMutableStringRef', 'CFMutableStringRefObj', 'CFMutableStringRef')
430 CFURLRef_object = CFURLRefObjectDefinition('CFURLRef', 'CFURLRefObj', 'CFURLRef')
432 # ADD object here
434 module.addobject(CFTypeRef_object)
435 module.addobject(CFArrayRef_object)
436 module.addobject(CFMutableArrayRef_object)
437 module.addobject(CFDictionaryRef_object)
438 module.addobject(CFMutableDictionaryRef_object)
439 module.addobject(CFDataRef_object)
440 module.addobject(CFMutableDataRef_object)
441 module.addobject(CFStringRef_object)
442 module.addobject(CFMutableStringRef_object)
443 module.addobject(CFURLRef_object)
444 # ADD addobject call here
446 # Create the generator classes used to populate the lists
447 Function = OSErrWeakLinkFunctionGenerator
448 Method = OSErrWeakLinkMethodGenerator
450 # Create and populate the lists
451 functions = []
452 CFTypeRef_methods = []
453 CFArrayRef_methods = []
454 CFMutableArrayRef_methods = []
455 CFDictionaryRef_methods = []
456 CFMutableDictionaryRef_methods = []
457 CFDataRef_methods = []
458 CFMutableDataRef_methods = []
459 CFStringRef_methods = []
460 CFMutableStringRef_methods = []
461 CFURLRef_methods = []
463 # ADD _methods initializer here
464 execfile(INPUTFILE)
467 # add the populated lists to the generator groups
468 # (in a different wordl the scan program would generate this)
469 for f in functions: module.add(f)
470 for f in CFTypeRef_methods: CFTypeRef_object.add(f)
471 for f in CFArrayRef_methods: CFArrayRef_object.add(f)
472 for f in CFMutableArrayRef_methods: CFMutableArrayRef_object.add(f)
473 for f in CFDictionaryRef_methods: CFDictionaryRef_object.add(f)
474 for f in CFMutableDictionaryRef_methods: CFMutableDictionaryRef_object.add(f)
475 for f in CFDataRef_methods: CFDataRef_object.add(f)
476 for f in CFMutableDataRef_methods: CFMutableDataRef_object.add(f)
477 for f in CFStringRef_methods: CFStringRef_object.add(f)
478 for f in CFMutableStringRef_methods: CFMutableStringRef_object.add(f)
479 for f in CFURLRef_methods: CFURLRef_object.add(f)
481 # Manual generators for getting data out of strings
483 getasstring_body = """
484 int size = CFStringGetLength(_self->ob_itself)+1;
485 char *data = malloc(size);
487 if( data == NULL ) return PyErr_NoMemory();
488 if ( CFStringGetCString(_self->ob_itself, data, size, 0) ) {
489 _res = (PyObject *)PyString_FromString(data);
490 } else {
491 PyErr_SetString(PyExc_RuntimeError, "CFStringGetCString could not fit the string");
492 _res = NULL;
494 free(data);
495 return _res;
498 f = ManualGenerator("CFStringGetString", getasstring_body);
499 f.docstring = lambda: "() -> (string _rv)"
500 CFStringRef_object.add(f)
502 getasunicode_body = """
503 int size = CFStringGetLength(_self->ob_itself)+1;
504 Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
505 CFRange range;
507 range.location = 0;
508 range.length = size;
509 if( data == NULL ) return PyErr_NoMemory();
510 CFStringGetCharacters(_self->ob_itself, range, data);
511 _res = (PyObject *)PyUnicode_FromUnicode(data, size);
512 free(data);
513 return _res;
516 f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
517 f.docstring = lambda: "() -> (unicode _rv)"
518 CFStringRef_object.add(f)
520 # Get data from CFDataRef
521 getasdata_body = """
522 int size = CFDataGetLength(_self->ob_itself);
523 char *data = (char *)CFDataGetBytePtr(_self->ob_itself);
525 _res = (PyObject *)PyString_FromStringAndSize(data, size);
526 return _res;
529 f = ManualGenerator("CFDataGetData", getasdata_body);
530 f.docstring = lambda: "() -> (string _rv)"
531 CFDataRef_object.add(f)
533 # Manual generator for CFPropertyListCreateFromXMLData because of funny error return
534 fromxml_body = """
535 CFTypeRef _rv;
536 CFOptionFlags mutabilityOption;
537 CFStringRef errorString;
538 if (!PyArg_ParseTuple(_args, "l",
539 &mutabilityOption))
540 return NULL;
541 _rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
542 _self->ob_itself,
543 mutabilityOption,
544 &errorString);
545 if (errorString)
546 CFRelease(errorString);
547 if (_rv == NULL) {
548 PyErr_SetString(PyExc_RuntimeError, "Parse error in XML data");
549 return NULL;
551 _res = Py_BuildValue("O&",
552 CFTypeRefObj_New, _rv);
553 return _res;
555 f = ManualGenerator("CFPropertyListCreateFromXMLData", fromxml_body)
556 f.docstring = lambda: "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"
557 CFTypeRef_object.add(f)
559 # Convert CF objects to Python objects
560 toPython_body = """
561 return PyCF_CF2Python(_self->ob_itself);
564 f = ManualGenerator("toPython", toPython_body);
565 f.docstring = lambda: "() -> (python_object)"
566 CFTypeRef_object.add(f)
568 toCF_body = """
569 CFTypeRef rv;
570 CFTypeID typeid;
572 if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv))
573 return NULL;
574 typeid = CFGetTypeID(rv);
576 if (typeid == CFStringGetTypeID())
577 return Py_BuildValue("O&", CFStringRefObj_New, rv);
578 if (typeid == CFArrayGetTypeID())
579 return Py_BuildValue("O&", CFArrayRefObj_New, rv);
580 if (typeid == CFDictionaryGetTypeID())
581 return Py_BuildValue("O&", CFDictionaryRefObj_New, rv);
582 if (typeid == CFURLGetTypeID())
583 return Py_BuildValue("O&", CFURLRefObj_New, rv);
585 return Py_BuildValue("O&", CFTypeRefObj_New, rv);
587 f = ManualGenerator("toCF", toCF_body);
588 f.docstring = lambda: "(python_object) -> (CF_object)"
589 module.add(f)
591 # ADD add forloop here
593 # generate output (open the output file as late as possible)
594 SetOutputFileName(OUTPUTFILE)
595 module.generate()