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