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
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
):
27 raise ValueError, "MethodSkipArg1 expects at least 2 args"
28 a0
, a1
, args
= args
[0], args
[1], args
[2:]
30 if t0
!= "CFAllocatorRef" and m0
!= InMode
:
31 raise ValueError, "MethodSkipArg1 should have dummy AllocatorRef first arg"
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
49 #include <CFDictionary.h>
53 #include <CoreServices/CoreServices.h>
56 #ifdef USE_TOOLBOX_OBJECT_GLUE
57 extern PyObject *_CFTypeRefObj_New(CFTypeRef);
58 extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *);
59 #define CFTypeRefObj_New _CFTypeRefObj_New
60 #define CFTypeRefObj_Convert _CFTypeRefObj_Convert
62 extern PyObject *_CFStringRefObj_New(CFStringRef);
63 extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
64 #define CFStringRefObj_New _CFStringRefObj_New
65 #define CFStringRefObj_Convert _CFStringRefObj_Convert
67 extern PyObject *_CFMutableStringRefObj_New(CFMutableStringRef);
68 extern int _CFMutableStringRefObj_Convert(PyObject *, CFMutableStringRef *);
69 #define CFMutableStringRefObj_New _CFMutableStringRefObj_New
70 #define CFMutableStringRefObj_Convert _CFMutableStringRefObj_Convert
72 extern PyObject *_CFArrayRefObj_New(CFArrayRef);
73 extern int _CFArrayRefObj_Convert(PyObject *, CFArrayRef *);
74 #define CFArrayRefObj_New _CFArrayRefObj_New
75 #define CFArrayRefObj_Convert _CFArrayRefObj_Convert
77 extern PyObject *_CFMutableArrayRefObj_New(CFMutableArrayRef);
78 extern int _CFMutableArrayRefObj_Convert(PyObject *, CFMutableArrayRef *);
79 #define CFMutableArrayRefObj_New _CFMutableArrayRefObj_New
80 #define CFMutableArrayRefObj_Convert _CFMutableArrayRefObj_Convert
82 extern PyObject *_CFDataRefObj_New(CFDataRef);
83 extern int _CFDataRefObj_Convert(PyObject *, CFDataRef *);
84 #define CFDataRefObj_New _CFDataRefObj_New
85 #define CFDataRefObj_Convert _CFDataRefObj_Convert
87 extern PyObject *_CFMutableDataRefObj_New(CFMutableDataRef);
88 extern int _CFMutableDataRefObj_Convert(PyObject *, CFMutableDataRef *);
89 #define CFMutableDataRefObj_New _CFMutableDataRefObj_New
90 #define CFMutableDataRefObj_Convert _CFMutableDataRefObj_Convert
92 extern PyObject *_CFDictionaryRefObj_New(CFDictionaryRef);
93 extern int _CFDictionaryRefObj_Convert(PyObject *, CFDictionaryRef *);
94 #define CFDictionaryRefObj_New _CFDictionaryRefObj_New
95 #define CFDictionaryRefObj_Convert _CFDictionaryRefObj_Convert
97 extern PyObject *_CFMutableDictionaryRefObj_New(CFMutableDictionaryRef);
98 extern int _CFMutableDictionaryRefObj_Convert(PyObject *, CFMutableDictionaryRef *);
99 #define CFMutableDictionaryRefObj_New _CFMutableDictionaryRefObj_New
100 #define CFMutableDictionaryRefObj_Convert _CFMutableDictionaryRefObj_Convert
102 extern PyObject *_CFURLRefObj_New(CFURLRef);
103 extern int _CFURLRefObj_Convert(PyObject *, CFURLRef *);
104 extern int _OptionalCFURLRefObj_Convert(PyObject *, CFURLRef *);
105 #define CFURLRefObj_New _CFURLRefObj_New
106 #define CFURLRefObj_Convert _CFURLRefObj_Convert
107 #define OptionalCFURLRefObj_Convert _OptionalCFURLRefObj_Convert
111 ** Parse/generate CFRange records
113 PyObject *CFRange_New(CFRange *itself)
116 return Py_BuildValue("ll", (long)itself->location, (long)itself->length);
120 CFRange_Convert(PyObject *v, CFRange *p_itself)
122 long location, length;
124 if( !PyArg_ParseTuple(v, "ll", &location, &length) )
126 p_itself->location = (CFIndex)location;
127 p_itself->length = (CFIndex)length;
131 /* Optional CFURL argument or None (passed as NULL) */
133 OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself)
135 if ( v == Py_None ) {
139 return CFURLRefObj_Convert(v, p_itself);
144 initstuff
= initstuff
+ """
145 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFTypeRefObj_New);
146 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFTypeRefObj_Convert);
147 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFStringRef, CFStringRefObj_New);
148 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFStringRef, CFStringRefObj_Convert);
149 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableStringRef, CFMutableStringRefObj_New);
150 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert);
152 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFArrayRef, CFArrayRefObj_New);
153 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFArrayRef, CFArrayRefObj_Convert);
154 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New);
155 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert);
156 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFDictionaryRef, CFDictionaryRefObj_New);
157 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert);
158 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New);
159 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert);
160 PyMac_INIT_TOOLBOX_OBJECT_NEW(CFURLRef, CFURLRefObj_New);
161 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
162 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
165 Boolean
= Type("Boolean", "l")
166 CFTypeID
= Type("CFTypeID", "l") # XXXX a guess, seems better than OSTypeType.
167 CFHashCode
= Type("CFHashCode", "l")
168 CFIndex
= Type("CFIndex", "l")
169 CFRange
= OpaqueByValueType('CFRange', 'CFRange')
170 CFOptionFlags
= Type("CFOptionFlags", "l")
171 CFStringEncoding
= Type("CFStringEncoding", "l")
172 CFComparisonResult
= Type("CFComparisonResult", "l") # a bit dangerous, it's an enum
173 CFURLPathStyle
= Type("CFURLPathStyle", "l") # a bit dangerous, it's an enum
176 return_stringptr
= Type("char *", "s") # ONLY FOR RETURN VALUES!!
178 CFAllocatorRef
= FakeType("(CFAllocatorRef)NULL")
179 CFArrayCallBacks_ptr
= FakeType("&kCFTypeArrayCallBacks")
180 CFDictionaryKeyCallBacks_ptr
= FakeType("&kCFTypeDictionaryKeyCallBacks")
181 CFDictionaryValueCallBacks_ptr
= FakeType("&kCFTypeDictionaryValueCallBacks")
183 CFTypeRef
= OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
184 CFArrayRef
= OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
185 CFMutableArrayRef
= OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
186 CFArrayRef
= OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
187 CFMutableArrayRef
= OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
188 CFDataRef
= OpaqueByValueType("CFDataRef", "CFDataRefObj")
189 CFMutableDataRef
= OpaqueByValueType("CFMutableDataRef", "CFMutableDataRefObj")
190 CFDictionaryRef
= OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
191 CFMutableDictionaryRef
= OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
192 CFStringRef
= OpaqueByValueType("CFStringRef", "CFStringRefObj")
193 CFMutableStringRef
= OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
194 CFURLRef
= OpaqueByValueType("CFURLRef", "CFURLRefObj")
195 OptionalCFURLRef
= OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
196 # ADD object type here
198 # Our (opaque) objects
200 class MyGlobalObjectDefinition(GlobalObjectDefinition
):
201 def outputCheckNewArg(self
):
202 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
203 def outputStructMembers(self
):
204 GlobalObjectDefinition
.outputStructMembers(self
)
205 Output("void (*ob_freeit)(CFTypeRef ptr);")
206 def outputInitStructMembers(self
):
207 GlobalObjectDefinition
.outputInitStructMembers(self
)
208 ## Output("it->ob_freeit = NULL;")
209 Output("it->ob_freeit = CFRelease;")
210 def outputCheckConvertArg(self
):
212 if (v == Py_None) { *p_itself = NULL; return 1; }
213 /* Check for other CF objects here */
215 def outputCleanupStructMembers(self
):
216 Output("if (self->ob_freeit && self->ob_itself)")
218 Output("self->ob_freeit((CFTypeRef)self->ob_itself);")
221 def outputCompare(self
):
223 Output("static int %s_compare(%s *self, %s *other)", self
.prefix
, self
.objecttype
, self
.objecttype
)
225 Output("/* XXXX Or should we use CFEqual?? */")
226 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
227 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
231 def outputHash(self
):
233 Output("static int %s_hash(%s *self)", self
.prefix
, self
.objecttype
)
235 Output("/* XXXX Or should we use CFHash?? */")
236 Output("return (int)self->ob_itself;")
239 def outputRepr(self
):
241 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
243 Output("char buf[100];")
244 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);""")
245 Output("return PyString_FromString(buf);")
248 class CFTypeRefObjectDefinition(MyGlobalObjectDefinition
):
251 class CFArrayRefObjectDefinition(MyGlobalObjectDefinition
):
252 basechain
= "&CFTypeRefObj_chain"
254 def outputRepr(self
):
256 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
258 Output("char buf[100];")
259 Output("""sprintf(buf, "<CFArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
260 Output("return PyString_FromString(buf);")
263 class CFMutableArrayRefObjectDefinition(MyGlobalObjectDefinition
):
264 basechain
= "&CFArrayRefObj_chain"
266 def outputRepr(self
):
268 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
270 Output("char buf[100];")
271 Output("""sprintf(buf, "<CFMutableArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
272 Output("return PyString_FromString(buf);")
275 class CFDictionaryRefObjectDefinition(MyGlobalObjectDefinition
):
276 basechain
= "&CFTypeRefObj_chain"
278 def outputRepr(self
):
280 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
282 Output("char buf[100];")
283 Output("""sprintf(buf, "<CFDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
284 Output("return PyString_FromString(buf);")
287 class CFMutableDictionaryRefObjectDefinition(MyGlobalObjectDefinition
):
288 basechain
= "&CFDictionaryRefObj_chain"
290 def outputRepr(self
):
292 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
294 Output("char buf[100];")
295 Output("""sprintf(buf, "<CFMutableDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
296 Output("return PyString_FromString(buf);")
299 class CFDataRefObjectDefinition(MyGlobalObjectDefinition
):
300 basechain
= "&CFTypeRefObj_chain"
302 def outputRepr(self
):
304 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
306 Output("char buf[100];")
307 Output("""sprintf(buf, "<CFDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
308 Output("return PyString_FromString(buf);")
311 class CFMutableDataRefObjectDefinition(MyGlobalObjectDefinition
):
312 basechain
= "&CFDataRefObj_chain"
314 def outputRepr(self
):
316 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
318 Output("char buf[100];")
319 Output("""sprintf(buf, "<CFMutableDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
320 Output("return PyString_FromString(buf);")
323 class CFStringRefObjectDefinition(MyGlobalObjectDefinition
):
324 basechain
= "&CFTypeRefObj_chain"
326 def outputCheckConvertArg(self
):
328 if (v == Py_None) { *p_itself = NULL; return 1; }
329 if (PyString_Check(v)) {
330 char *cStr = PyString_AsString(v);
331 *p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, 0);
334 if (PyUnicode_Check(v)) {
335 /* We use the CF types here, if Python was configured differently that will give an error */
336 CFIndex size = PyUnicode_GetSize(v);
337 UniChar *unichars = PyUnicode_AsUnicode(v);
338 if (!unichars) return 0;
339 *p_itself = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
345 def outputRepr(self
):
347 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
349 Output("char buf[100];")
350 Output("""sprintf(buf, "<CFStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
351 Output("return PyString_FromString(buf);")
354 class CFMutableStringRefObjectDefinition(CFStringRefObjectDefinition
):
355 basechain
= "&CFStringRefObj_chain"
357 def outputCheckConvertArg(self
):
358 # Mutable, don't allow Python strings
359 return MyGlobalObjectDefinition
.outputCheckConvertArg(self
)
361 def outputRepr(self
):
363 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
365 Output("char buf[100];")
366 Output("""sprintf(buf, "<CFMutableStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
367 Output("return PyString_FromString(buf);")
370 class CFURLRefObjectDefinition(MyGlobalObjectDefinition
):
371 basechain
= "&CFTypeRefObj_chain"
373 def outputRepr(self
):
375 Output("static PyObject * %s_repr(%s *self)", self
.prefix
, self
.objecttype
)
377 Output("char buf[100];")
378 Output("""sprintf(buf, "<CFURL object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
379 Output("return PyString_FromString(buf);")
383 # ADD object class here
385 # From here on it's basically all boiler plate...
387 # Create the generator groups and link them
388 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
389 CFTypeRef_object
= CFTypeRefObjectDefinition('CFTypeRef', 'CFTypeRefObj', 'CFTypeRef')
390 CFArrayRef_object
= CFArrayRefObjectDefinition('CFArrayRef', 'CFArrayRefObj', 'CFArrayRef')
391 CFMutableArrayRef_object
= CFMutableArrayRefObjectDefinition('CFMutableArrayRef', 'CFMutableArrayRefObj', 'CFMutableArrayRef')
392 CFDictionaryRef_object
= CFDictionaryRefObjectDefinition('CFDictionaryRef', 'CFDictionaryRefObj', 'CFDictionaryRef')
393 CFMutableDictionaryRef_object
= CFMutableDictionaryRefObjectDefinition('CFMutableDictionaryRef', 'CFMutableDictionaryRefObj', 'CFMutableDictionaryRef')
394 CFDataRef_object
= CFDataRefObjectDefinition('CFDataRef', 'CFDataRefObj', 'CFDataRef')
395 CFMutableDataRef_object
= CFMutableDataRefObjectDefinition('CFMutableDataRef', 'CFMutableDataRefObj', 'CFMutableDataRef')
396 CFStringRef_object
= CFStringRefObjectDefinition('CFStringRef', 'CFStringRefObj', 'CFStringRef')
397 CFMutableStringRef_object
= CFMutableStringRefObjectDefinition('CFMutableStringRef', 'CFMutableStringRefObj', 'CFMutableStringRef')
398 CFURLRef_object
= CFURLRefObjectDefinition('CFURLRef', 'CFURLRefObj', 'CFURLRef')
402 module
.addobject(CFTypeRef_object
)
403 module
.addobject(CFArrayRef_object
)
404 module
.addobject(CFMutableArrayRef_object
)
405 module
.addobject(CFDictionaryRef_object
)
406 module
.addobject(CFMutableDictionaryRef_object
)
407 module
.addobject(CFDataRef_object
)
408 module
.addobject(CFMutableDataRef_object
)
409 module
.addobject(CFStringRef_object
)
410 module
.addobject(CFMutableStringRef_object
)
411 module
.addobject(CFURLRef_object
)
412 # ADD addobject call here
414 # Create the generator classes used to populate the lists
415 Function
= OSErrWeakLinkFunctionGenerator
416 Method
= OSErrWeakLinkMethodGenerator
418 # Create and populate the lists
420 CFTypeRef_methods
= []
421 CFArrayRef_methods
= []
422 CFMutableArrayRef_methods
= []
423 CFDictionaryRef_methods
= []
424 CFMutableDictionaryRef_methods
= []
425 CFDataRef_methods
= []
426 CFMutableDataRef_methods
= []
427 CFStringRef_methods
= []
428 CFMutableStringRef_methods
= []
429 CFURLRef_methods
= []
431 # ADD _methods initializer here
435 # add the populated lists to the generator groups
436 # (in a different wordl the scan program would generate this)
437 for f
in functions
: module
.add(f
)
438 for f
in CFTypeRef_methods
: CFTypeRef_object
.add(f
)
439 for f
in CFArrayRef_methods
: CFArrayRef_object
.add(f
)
440 for f
in CFMutableArrayRef_methods
: CFMutableArrayRef_object
.add(f
)
441 for f
in CFDictionaryRef_methods
: CFDictionaryRef_object
.add(f
)
442 for f
in CFMutableDictionaryRef_methods
: CFMutableDictionaryRef_object
.add(f
)
443 for f
in CFDataRef_methods
: CFDataRef_object
.add(f
)
444 for f
in CFMutableDataRef_methods
: CFMutableDataRef_object
.add(f
)
445 for f
in CFStringRef_methods
: CFStringRef_object
.add(f
)
446 for f
in CFMutableStringRef_methods
: CFMutableStringRef_object
.add(f
)
447 for f
in CFURLRef_methods
: CFURLRef_object
.add(f
)
449 # Manual generators for getting data out of strings
451 getasstring_body
= """
452 int size = CFStringGetLength(_self->ob_itself)+1;
453 char *data = malloc(size);
455 if( data == NULL ) return PyErr_NoMemory();
456 if ( CFStringGetCString(_self->ob_itself, data, size, 0) ) {
457 _res = (PyObject *)PyString_FromString(data);
459 PyErr_SetString(PyExc_RuntimeError, "CFStringGetCString could not fit the string");
466 f
= ManualGenerator("CFStringGetString", getasstring_body
);
467 f
.docstring
= lambda: "() -> (string _rv)"
468 CFStringRef_object
.add(f
)
470 getasunicode_body
= """
471 int size = CFStringGetLength(_self->ob_itself)+1;
472 Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
477 if( data == NULL ) return PyErr_NoMemory();
478 CFStringGetCharacters(_self->ob_itself, range, data);
479 _res = (PyObject *)PyUnicode_FromUnicode(data, size);
484 f
= ManualGenerator("CFStringGetUnicode", getasunicode_body
);
485 f
.docstring
= lambda: "() -> (unicode _rv)"
486 CFStringRef_object
.add(f
)
488 # ADD add forloop here
490 # generate output (open the output file as late as possible)
491 SetOutputFileName(OUTPUTFILE
)