Fix three PyChecker-detected gotchas.
[python/dscho.git] / Mac / Modules / win / winsupport.py
blob6054e263d39b3c73db19d75dffa0da69ebe8fa29
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 import string
8 # Declarations that change for each manager
9 MACHEADERFILE = 'Windows.h' # The Apple header file
10 MODNAME = 'Win' # The name of the module
11 OBJECTNAME = 'Window' # The basic name of the objects used here
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX = MODNAME # The prefix for module-wide routines
15 OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them
16 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
17 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
18 EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
19 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
21 from macsupport import *
23 # Create the type objects
25 WindowPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
26 WindowRef = WindowPtr
27 WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
28 WindowPeek.passInput = lambda name: "(WindowPeek)(%s)" % name
29 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
30 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
32 DragReference = OpaqueByValueType("DragReference", "DragObj")
34 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
35 PicHandle = OpaqueByValueType("PicHandle", "ResObj")
36 WCTabHandle = OpaqueByValueType("WCTabHandle", "ResObj")
37 AuxWinHandle = OpaqueByValueType("AuxWinHandle", "ResObj")
38 PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
39 AliasHandle = OpaqueByValueType("AliasHandle", "ResObj")
40 IconRef = OpaqueByValueType("IconRef", "ResObj")
42 WindowRegionCode = Type("WindowRegionCode", "H")
43 WindowClass = Type("WindowClass", "l")
44 WindowAttributes = Type("WindowAttributes", "l")
45 WindowPositionMethod = Type("WindowPositionMethod", "l")
46 WindowTransitionEffect = Type("WindowTransitionEffect", "l")
47 WindowTransitionAction = Type("WindowTransitionAction", "l")
48 RGBColor = OpaqueType("RGBColor", "QdRGB")
49 RGBColor_ptr = RGBColor
50 ScrollWindowOptions = Type("ScrollWindowOptions", "l")
51 WindowPartCode = Type("WindowPartCode", "h")
53 PropertyCreator = OSTypeType("PropertyCreator")
54 PropertyTag = OSTypeType("PropertyTag")
56 includestuff = includestuff + """
57 #include <%s>""" % MACHEADERFILE + """
59 #if !ACCESSOR_CALLS_ARE_FUNCTIONS
60 /* Carbon calls that we emulate in classic mode */
61 #define GetWindowSpareFlag(win) (((CWindowPeek)(win))->spareFlag)
62 #define GetWindowFromPort(port) ((WindowRef)(port))
63 #define GetWindowPortBounds(win, rectp) (*(rectp) = ((CWindowPeek)(win))->port.portRect)
64 #endif
65 #if ACCESSOR_CALLS_ARE_FUNCTIONS
66 /* Classic calls that we emulate in carbon mode */
67 #define GetWindowUpdateRgn(win, rgn) GetWindowRegion((win), kWindowUpdateRgn, (rgn))
68 #define GetWindowStructureRgn(win, rgn) GetWindowRegion((win), kWindowStructureRgn, (rgn))
69 #define GetWindowContentRgn(win, rgn) GetWindowRegion((win), kWindowContentRgn, (rgn))
70 #endif
72 /* Function to dispose a window, with a "normal" calling sequence */
73 static void
74 PyMac_AutoDisposeWindow(WindowPtr w)
76 DisposeWindow(w);
78 """
80 finalstuff = finalstuff + """
81 /* Return the object corresponding to the window, or NULL */
83 PyObject *
84 WinObj_WhichWindow(w)
85 WindowPtr w;
87 PyObject *it;
89 if (w == NULL) {
90 it = Py_None;
91 Py_INCREF(it);
92 } else {
93 it = (PyObject *) GetWRefCon(w);
94 if (it == NULL || ((WindowObject *)it)->ob_itself != w || !WinObj_Check(it)) {
95 it = WinObj_New(w);
96 ((WindowObject *)it)->ob_freeit = NULL;
97 } else {
98 Py_INCREF(it);
101 return it;
105 class MyObjectDefinition(GlobalObjectDefinition):
106 def outputCheckNewArg(self):
107 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
108 def outputStructMembers(self):
109 GlobalObjectDefinition.outputStructMembers(self)
110 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
111 def outputInitStructMembers(self):
112 GlobalObjectDefinition.outputInitStructMembers(self)
113 Output("it->ob_freeit = NULL;")
114 Output("if (GetWRefCon(itself) == 0)")
115 OutLbrace()
116 Output("SetWRefCon(itself, (long)it);")
117 Output("it->ob_freeit = PyMac_AutoDisposeWindow;")
118 OutRbrace()
119 def outputCheckConvertArg(self):
120 OutLbrace("if (DlgObj_Check(v))")
121 Output("*p_itself = DlgObj_ConvertToWindow(v);")
122 Output("return 1;")
123 OutRbrace()
124 Out("""
125 if (v == Py_None) { *p_itself = NULL; return 1; }
126 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
127 """)
128 def outputCleanupStructMembers(self):
129 Output("if (self->ob_freeit && self->ob_itself)")
130 OutLbrace()
131 Output("SetWRefCon(self->ob_itself, 0);")
132 Output("self->ob_freeit(self->ob_itself);")
133 OutRbrace()
134 Output("self->ob_itself = NULL;")
135 Output("self->ob_freeit = NULL;")
137 def outputCompare(self):
138 Output()
139 Output("static int %s_compare(self, other)", self.prefix)
140 IndentLevel()
141 Output("%s *self, *other;", self.objecttype)
142 DedentLevel()
143 OutLbrace()
144 Output("if ( self->ob_itself > other->ob_itself ) return 1;")
145 Output("if ( self->ob_itself < other->ob_itself ) return -1;")
146 Output("return 0;")
147 OutRbrace()
149 def outputHash(self):
150 Output()
151 Output("static int %s_hash(self)", self.prefix)
152 IndentLevel()
153 Output("%s *self;", self.objecttype)
154 DedentLevel()
155 OutLbrace()
156 Output("return (int)self->ob_itself;")
157 OutRbrace()
159 def outputRepr(self):
160 Output()
161 Output("static PyObject * %s_repr(self)", self.prefix)
162 IndentLevel()
163 Output("%s *self;", self.objecttype)
164 DedentLevel()
165 OutLbrace()
166 Output("char buf[100];")
167 Output("""sprintf(buf, "<Window object at 0x%%08.8x for 0x%%08.8x>", self, self->ob_itself);""")
168 Output("return PyString_FromString(buf);")
169 OutRbrace()
171 ## def outputFreeIt(self, itselfname):
172 ## Output("DisposeWindow(%s);", itselfname)
173 # From here on it's basically all boiler plate...
175 # Create the generator groups and link them
176 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
177 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
178 module.addobject(object)
180 # Create the generator classes used to populate the lists
181 Function = OSErrFunctionGenerator
182 Method = OSErrMethodGenerator
184 # Create and populate the lists
185 functions = []
186 methods = []
187 execfile(INPUTFILE)
189 # Add manual routines for converting integer WindowPtr's (as returned by
190 # various event routines) and Dialog objects to a WindowObject.
191 whichwin_body = """
192 long ptr;
194 if ( !PyArg_ParseTuple(_args, "i", &ptr) )
195 return NULL;
196 return WinObj_WhichWindow((WindowPtr)ptr);
199 f = ManualGenerator("WhichWindow", whichwin_body)
200 f.docstring = lambda : "Resolve an integer WindowPtr address to a Window object"
202 functions.append(f)
204 # And add the routines that access the internal bits of a window struct. They
205 # are currently #defined in Windows.h, they will be real routines in Copland
206 # (at which time this execfile can go)
207 execfile(EDITFILE)
209 # add the populated lists to the generator groups
210 # (in a different wordl the scan program would generate this)
211 for f in functions: module.add(f)
212 for f in methods: object.add(f)
216 # generate output (open the output file as late as possible)
217 SetOutputFileName(OUTPUTFILE)
218 module.generate()