/Mac/Modules/cf/cfsupport.py

http://unladen-swallow.googlecode.com/ · Python · 666 lines · 564 code · 69 blank · 33 comment · 16 complexity · 194ac2e387429cb01165680c38400ed3 MD5 · raw file

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