py-cvs-2001_07_13 (Rev 1.3) merge
[python/dscho.git] / Tools / bgen / bgen / macsupport.py
blobd8677b00410ff2ff5af2ca84430c1f2e2776f468
1 """\
2 Augment the "bgen" package with definitions that are useful on the Apple Macintosh.
4 Intended usage is "from macsupport import *" -- this implies all bgen's goodies.
5 """
8 # Import everything from bgen (for ourselves as well as for re-export)
9 from bgen import *
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")
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")
38 wide = OpaqueByValueType("wide", "PyMac_Buildwide", "PyMac_Getwide")
39 wide_ptr = OpaqueType("wide", "PyMac_Buildwide", "PyMac_Getwide")
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
47 # File System Specifications
48 FSSpec_ptr = OpaqueType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")
49 FSSpec = OpaqueByValueType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")
51 # OSType and ResType: 4-byte character strings
52 def OSTypeType(typename):
53 return OpaqueByValueType(typename, "PyMac_BuildOSType", "PyMac_GetOSType")
54 OSType = OSTypeType("OSType")
55 ResType = OSTypeType("ResType")
56 FourCharCode = OSTypeType("FourCharCode")
58 # Version numbers
59 NumVersion = OpaqueByValueType("NumVersion", "PyMac_BuildNumVersion", "BUG")
61 # Handles (always resources in our case)
62 Handle = OpaqueByValueType("Handle", "ResObj")
63 MenuHandle = OpaqueByValueType("MenuHandle", "MenuObj")
64 MenuRef = MenuHandle
65 ControlHandle = OpaqueByValueType("ControlHandle", "CtlObj")
66 ControlRef = ControlHandle
68 # Windows and Dialogs
69 WindowPtr = OpaqueByValueType("WindowPtr", "WinObj")
70 WindowRef = WindowPtr
71 DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")
72 DialogRef = DialogPtr
73 ExistingWindowPtr = OpaqueByValueType("WindowPtr", "WinObj_WhichWindow", "BUG")
74 ExistingDialogPtr = OpaqueByValueType("DialogPtr", "DlgObj_WhichDialog", "BUG")
76 # NULL pointer passed in as optional storage -- not present in Python version
77 NullStorage = FakeType("(void *)0")
79 # More standard datatypes
80 Fixed = OpaqueByValueType("Fixed", "PyMac_BuildFixed", "PyMac_GetFixed")
82 # Quickdraw data types
83 Rect = Rect_ptr = OpaqueType("Rect", "PyMac_BuildRect", "PyMac_GetRect")
84 Point = OpaqueByValueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")
85 Point_ptr = OpaqueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")
87 # Event records
88 EventRecord = OpaqueType("EventRecord", "PyMac_BuildEventRecord", "PyMac_GetEventRecord")
89 EventRecord_ptr = EventRecord
91 # OSErr is special because it is turned into an exception
92 # (Could do this with less code using a variant of mkvalue("O&")?)
93 class OSErrType(Type):
94 def errorCheck(self, name):
95 Output("if (%s != noErr) return PyMac_Error(%s);", name, name)
96 self.used = 1
97 OSErr = OSErrType("OSErr", 'h')
98 OSStatus = OSErrType("OSStatus", 'l')
101 # Various buffer types
103 InBuffer = VarInputBufferType('char', 'long', 'l') # (buf, len)
104 UcharInBuffer = VarInputBufferType('unsigned char', 'long', 'l') # (buf, len)
105 OptionalInBuffer = OptionalVarInputBufferType('char', 'long', 'l') # (buf, len)
107 InOutBuffer = HeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, len)
108 VarInOutBuffer = VarHeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, &len)
110 OutBuffer = HeapOutputBufferType('char', 'long', 'l') # (buf, len)
111 VarOutBuffer = VarHeapOutputBufferType('char', 'long', 'l') # (buf, &len)
112 VarVarOutBuffer = VarVarHeapOutputBufferType('char', 'long', 'l') # (buf, len, &len)
115 # Predefine various pieces of program text to be passed to Module() later:
117 # Stuff added immediately after the system include files
118 includestuff = """
119 #include "macglue.h"
120 #include "pymactoolbox.h"
122 /* Macro to test whether a weak-loaded CFM function exists */
123 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\\
124 PyErr_SetString(PyExc_NotImplementedError, \\
125 "Not available in this shared library/OS version"); \\
126 return NULL; \\
127 }} while(0)
131 # Stuff added just before the module's init function
132 finalstuff = """
135 # Stuff added inside the module's init function
136 initstuff = """
140 # Generator classes with a twist -- if the function returns OSErr,
141 # its mode is manipulated so that it turns into an exception or disappears
142 # (and its name is changed to _err, for documentation purposes).
143 # This requires that the OSErr type (defined above) has a non-trivial
144 # errorCheck method.
145 class OSErrMixIn:
146 "Mix-in class to treat OSErr/OSStatus return values special"
147 def makereturnvar(self):
148 if self.returntype.__class__ == OSErrType:
149 return Variable(self.returntype, "_err", ErrorMode)
150 else:
151 return Variable(self.returntype, "_rv", OutMode)
153 class OSErrFunctionGenerator(OSErrMixIn, FunctionGenerator): pass
154 class OSErrMethodGenerator(OSErrMixIn, MethodGenerator): pass
156 class WeakLinkMixIn:
157 "Mix-in to test the function actually exists (!= NULL) before calling"
159 def precheck(self):
160 Output('PyMac_PRECHECK(%s);', self.name)
162 class WeakLinkFunctionGenerator(WeakLinkMixIn, FunctionGenerator): pass
163 class WeakLinkMethodGenerator(WeakLinkMixIn, MethodGenerator): pass
164 class OSErrWeakLinkFunctionGenerator(OSErrMixIn, WeakLinkMixIn, FunctionGenerator): pass
165 class OSErrWeakLinkMethodGenerator(OSErrMixIn, WeakLinkMixIn, MethodGenerator): pass
167 class MacModule(Module):
168 "Subclass which gets the exception initializer from macglue.c"
169 def exceptionInitializer(self):
170 return "PyMac_GetOSErrException()"
172 _SetOutputFileName = SetOutputFileName # Save original
173 def SetOutputFileName(file = None):
174 "Set the output file name and set its creator&type to CWIE&TEXT"
175 _SetOutputFileName(file)
176 if file:
177 import MacOS
178 MacOS.SetCreatorAndType(file, 'CWIE', 'TEXT')