/Tools/bgen/bgen/macsupport.py

http://unladen-swallow.googlecode.com/ · Python · 197 lines · 131 code · 39 blank · 27 comment · 2 complexity · a2895c9b35516be8899308036a0ed407 MD5 · raw file

  1. """\
  2. Augment the "bgen" package with definitions that are useful on the Apple Macintosh.
  3. Intended usage is "from macsupport import *" -- this implies all bgen's goodies.
  4. """
  5. # Import everything from bgen (for ourselves as well as for re-export)
  6. from bgen import *
  7. # Simple types
  8. Boolean = Type("Boolean", "b")
  9. SignedByte = Type("SignedByte", "b")
  10. Size = Type("Size", "l")
  11. Style = Type("Style", "b")
  12. StyleParameter = Type("StyleParameter", "h")
  13. CharParameter = Type("CharParameter", "h")
  14. TextEncoding = Type("TextEncoding", "l")
  15. ByteCount = Type("ByteCount", "l")
  16. Duration = Type("Duration", "l")
  17. ByteOffset = Type("ByteOffset", "l")
  18. OptionBits = Type("OptionBits", "l")
  19. ItemCount = Type("ItemCount", "l")
  20. PBVersion = Type("PBVersion", "l")
  21. ScriptCode = Type("ScriptCode", "h")
  22. LangCode = Type("LangCode", "h")
  23. RegionCode = Type("RegionCode", "h")
  24. UInt8 = Type("UInt8", "b")
  25. SInt8 = Type("SInt8", "b")
  26. UInt16 = Type("UInt16", "H")
  27. SInt16 = Type("SInt16", "h")
  28. UInt32 = Type("UInt32", "l")
  29. SInt32 = Type("SInt32", "l")
  30. Float32 = Type("Float32", "f")
  31. wide = OpaqueByValueType("wide", "PyMac_Buildwide", "PyMac_Getwide")
  32. wide_ptr = OpaqueType("wide", "PyMac_Buildwide", "PyMac_Getwide")
  33. # Pascal strings
  34. ConstStr255Param = OpaqueArrayType("Str255", "PyMac_BuildStr255", "PyMac_GetStr255")
  35. Str255 = OpaqueArrayType("Str255", "PyMac_BuildStr255", "PyMac_GetStr255")
  36. StringPtr = OpaqueByValueType("StringPtr", "PyMac_BuildStr255", "PyMac_GetStr255")
  37. ConstStringPtr = StringPtr
  38. # File System Specifications
  39. FSSpec_ptr = OpaqueType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")
  40. FSSpec = OpaqueByValueStructType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")
  41. FSRef_ptr = OpaqueType("FSRef", "PyMac_BuildFSRef", "PyMac_GetFSRef")
  42. FSRef = OpaqueByValueStructType("FSRef", "PyMac_BuildFSRef", "PyMac_GetFSRef")
  43. # OSType and ResType: 4-byte character strings
  44. def OSTypeType(typename):
  45. return OpaqueByValueType(typename, "PyMac_BuildOSType", "PyMac_GetOSType")
  46. OSType = OSTypeType("OSType")
  47. ResType = OSTypeType("ResType")
  48. FourCharCode = OSTypeType("FourCharCode")
  49. # Version numbers
  50. NumVersion = OpaqueByValueType("NumVersion", "PyMac_BuildNumVersion", "BUG")
  51. # Handles (always resources in our case)
  52. Handle = OpaqueByValueType("Handle", "ResObj")
  53. MenuHandle = OpaqueByValueType("MenuHandle", "MenuObj")
  54. MenuRef = MenuHandle
  55. ControlHandle = OpaqueByValueType("ControlHandle", "CtlObj")
  56. ControlRef = ControlHandle
  57. # Windows and Dialogs
  58. WindowPtr = OpaqueByValueType("WindowPtr", "WinObj")
  59. WindowRef = WindowPtr
  60. DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
  61. DialogRef = DialogPtr
  62. ExistingWindowPtr = OpaqueByValueType("WindowPtr", "WinObj_WhichWindow", "BUG")
  63. ExistingDialogPtr = OpaqueByValueType("DialogPtr", "DlgObj_WhichDialog", "BUG")
  64. # NULL pointer passed in as optional storage -- not present in Python version
  65. NullStorage = FakeType("(void *)0")
  66. # More standard datatypes
  67. Fixed = OpaqueByValueType("Fixed", "PyMac_BuildFixed", "PyMac_GetFixed")
  68. # Quickdraw data types
  69. Rect = Rect_ptr = OpaqueType("Rect", "PyMac_BuildRect", "PyMac_GetRect")
  70. Point = OpaqueByValueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")
  71. Point_ptr = OpaqueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")
  72. # Event records
  73. EventRecord = OpaqueType("EventRecord", "PyMac_BuildEventRecord", "PyMac_GetEventRecord")
  74. EventRecord_ptr = EventRecord
  75. # CoreFoundation datatypes
  76. CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
  77. CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
  78. CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
  79. CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
  80. CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
  81. CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
  82. CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
  83. CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
  84. OptionalCFURLRef = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
  85. # OSErr is special because it is turned into an exception
  86. # (Could do this with less code using a variant of mkvalue("O&")?)
  87. class OSErrType(Type):
  88. def errorCheck(self, name):
  89. Output("if (%s != noErr) return PyMac_Error(%s);", name, name)
  90. self.used = 1
  91. OSErr = OSErrType("OSErr", 'h')
  92. OSStatus = OSErrType("OSStatus", 'l')
  93. # Various buffer types
  94. InBuffer = VarInputBufferType('char', 'long', 'l') # (buf, len)
  95. UcharInBuffer = VarInputBufferType('unsigned char', 'long', 'l') # (buf, len)
  96. OptionalInBuffer = OptionalVarInputBufferType('char', 'long', 'l') # (buf, len)
  97. InOutBuffer = HeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, len)
  98. VarInOutBuffer = VarHeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, &len)
  99. OutBuffer = HeapOutputBufferType('char', 'long', 'l') # (buf, len)
  100. VarOutBuffer = VarHeapOutputBufferType('char', 'long', 'l') # (buf, &len)
  101. VarVarOutBuffer = VarVarHeapOutputBufferType('char', 'long', 'l') # (buf, len, &len)
  102. # Unicode arguments sometimes have reversed len, buffer (don't understand why Apple did this...)
  103. class VarUnicodeInputBufferType(VarInputBufferType):
  104. def getargsFormat(self):
  105. return "u#"
  106. class VarUnicodeReverseInputBufferType(ReverseInputBufferMixin, VarUnicodeInputBufferType):
  107. pass
  108. UnicodeInBuffer = VarUnicodeInputBufferType('UniChar', 'UniCharCount', 'l')
  109. UnicodeReverseInBuffer = VarUnicodeReverseInputBufferType('UniChar', 'UniCharCount', 'l')
  110. UniChar_ptr = InputOnlyType("UniCharPtr", "u")
  111. # Predefine various pieces of program text to be passed to Module() later:
  112. # Stuff added immediately after the system include files
  113. includestuff = """
  114. #include "pymactoolbox.h"
  115. /* Macro to test whether a weak-loaded CFM function exists */
  116. #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\\
  117. PyErr_SetString(PyExc_NotImplementedError, \\
  118. "Not available in this shared library/OS version"); \\
  119. return NULL; \\
  120. }} while(0)
  121. """
  122. # Stuff added just before the module's init function
  123. finalstuff = """
  124. """
  125. # Stuff added inside the module's init function
  126. initstuff = """
  127. """
  128. # Generator classes with a twist -- if the function returns OSErr,
  129. # its mode is manipulated so that it turns into an exception or disappears
  130. # (and its name is changed to _err, for documentation purposes).
  131. # This requires that the OSErr type (defined above) has a non-trivial
  132. # errorCheck method.
  133. class OSErrMixIn:
  134. "Mix-in class to treat OSErr/OSStatus return values special"
  135. def makereturnvar(self):
  136. if self.returntype.__class__ == OSErrType:
  137. return Variable(self.returntype, "_err", ErrorMode)
  138. else:
  139. return Variable(self.returntype, "_rv", OutMode)
  140. class OSErrFunctionGenerator(OSErrMixIn, FunctionGenerator): pass
  141. class OSErrMethodGenerator(OSErrMixIn, MethodGenerator): pass
  142. class WeakLinkMixIn:
  143. "Mix-in to test the function actually exists (!= NULL) before calling"
  144. def precheck(self):
  145. Output('#ifndef %s', self.name)
  146. Output('PyMac_PRECHECK(%s);', self.name)
  147. Output('#endif')
  148. class WeakLinkFunctionGenerator(WeakLinkMixIn, FunctionGenerator): pass
  149. class WeakLinkMethodGenerator(WeakLinkMixIn, MethodGenerator): pass
  150. class OSErrWeakLinkFunctionGenerator(OSErrMixIn, WeakLinkMixIn, FunctionGenerator): pass
  151. class OSErrWeakLinkMethodGenerator(OSErrMixIn, WeakLinkMixIn, MethodGenerator): pass
  152. class MacModule(Module):
  153. "Subclass which gets the exception initializer from macglue.c"
  154. def exceptionInitializer(self):
  155. return "PyMac_GetOSErrException()"