Fix three PyChecker-detected gotchas.
[python/dscho.git] / Mac / Modules / htmlrender / htmlsupport.py
blobe769018b0147ed844ec787a50fe8a5b6fa3b7829
1 # This script generates the Dialogs interface for Python.
2 # It uses the "bgen" package to generate C code.
3 # It execs the file dlggen.py which contain the function definitions
4 # (dlggen.py was generated by dlgscan.py, scanning the <Dialogs.h> header file).
6 from macsupport import *
8 # Create the type objects
10 HRReference = OpaqueByValueType("HRReference", "HtmlObj")
12 HRScrollbarState = Type("HRScrollbarState", "h")
13 URLSourceType = Type("URLSourceType", "H")
14 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
15 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
16 char_ptr = Type("char *", "s")
17 StringPtr = Type("StringPtr", "s")
19 ### An OptHandle is either a handle or None (in case NULL is passed in).
20 ### This is needed for GetDialogItem().
21 ##OptHandle = OpaqueByValueType("Handle", "OptResObj")
23 ##ModalFilterProcPtr = InputOnlyType("PyObject*", "O")
24 ##ModalFilterProcPtr.passInput = lambda name: "NewModalFilterProc(Dlg_PassFilterProc(%s))" % name
25 ##ModalFilterUPP = ModalFilterProcPtr
28 ##DITLMethod = Type("DITLMethod", "h")
29 ##DialogItemIndex = Type("DialogItemIndex", "h")
30 ##DialogItemType = Type("DialogItemType", "h")
31 ##DialogItemIndexZeroBased = Type("DialogItemIndexZeroBased", "h")
32 ##AlertType = Type("AlertType", "h")
33 ##StringPtr = Str255
34 ##EventMask = Type("EventMask", "h")
36 includestuff = includestuff + """
37 #include <HTMLRendering.h>
38 """
40 ###ifndef HAVE_UNIVERSAL_HEADERS
41 ###define NewModalFilterProc(x) (x)
42 ###endif
44 ###define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
46 ##/* XXX Shouldn't this be a stack? */
47 ##static PyObject *Dlg_FilterProc_callback = NULL;
49 ##static PyObject *DlgObj_New(DialogPtr); /* Forward */
51 ##static pascal Boolean Dlg_UnivFilterProc(DialogPtr dialog,
52 ## EventRecord *event,
53 ## short *itemHit)
54 ##{
55 ## Boolean rv;
56 ## PyObject *args, *res;
57 ## PyObject *callback = Dlg_FilterProc_callback;
58 ## if (callback == NULL)
59 ## return 0; /* Default behavior */
60 ## Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
61 ## args = Py_BuildValue("O&O&", WinObj_WhichWindow, dialog, PyMac_BuildEventRecord, event);
62 ## if (args == NULL)
63 ## res = NULL;
64 ## else {
65 ## res = PyEval_CallObject(callback, args);
66 ## Py_DECREF(args);
67 ## }
68 ## if (res == NULL) {
69 ## PySys_WriteStderr("Exception in Dialog Filter\\n");
70 ## PyErr_Print();
71 ## *itemHit = -1; /* Fake return item */
72 ## return 1; /* We handled it */
73 ## }
74 ## else {
75 ## Dlg_FilterProc_callback = callback;
76 ## if (PyInt_Check(res)) {
77 ## *itemHit = PyInt_AsLong(res);
78 ## rv = 1;
79 ## }
80 ## else
81 ## rv = PyObject_IsTrue(res);
82 ## }
83 ## Py_DECREF(res);
84 ## return rv;
85 ##}
87 ##static ModalFilterProcPtr
88 ##Dlg_PassFilterProc(PyObject *callback)
89 ##{
90 ## PyObject *tmp = Dlg_FilterProc_callback;
91 ## Dlg_FilterProc_callback = NULL;
92 ## if (callback == Py_None) {
93 ## Py_XDECREF(tmp);
94 ## return NULL;
95 ## }
96 ## Py_INCREF(callback);
97 ## Dlg_FilterProc_callback = callback;
98 ## Py_XDECREF(tmp);
99 ## return &Dlg_UnivFilterProc;
102 ##static PyObject *Dlg_UserItemProc_callback = NULL;
104 ##static pascal void Dlg_UnivUserItemProc(DialogPtr dialog,
105 ## short item)
107 ## PyObject *args, *res;
109 ## if (Dlg_UserItemProc_callback == NULL)
110 ## return; /* Default behavior */
111 ## Dlg_FilterProc_callback = NULL; /* We'll restore it when call successful */
112 ## args = Py_BuildValue("O&h", WinObj_WhichWindow, dialog, item);
113 ## if (args == NULL)
114 ## res = NULL;
115 ## else {
116 ## res = PyEval_CallObject(Dlg_UserItemProc_callback, args);
117 ## Py_DECREF(args);
118 ## }
119 ## if (res == NULL) {
120 ## PySys_WriteStderr("Exception in Dialog UserItem proc\\n");
121 ## PyErr_Print();
122 ## }
123 ## Py_XDECREF(res);
124 ## return;
127 ##extern PyMethodChain WinObj_chain;
128 ##"""
131 # Define a class which specializes our object definition
132 class MyObjectDefinition(GlobalObjectDefinition):
133 pass
134 ## def __init__(self, name, prefix = None, itselftype = None):
135 ## GlobalObjectDefinition.__init__(self, name, prefix, itselftype)
136 ## self.basechain = "&WinObj_chain"
137 ## def outputInitStructMembers(self):
138 ## GlobalObjectDefinition.outputInitStructMembers(self)
139 ## Output("SetWRefCon(itself, (long)it);")
140 ## def outputCheckNewArg(self):
141 ## Output("if (itself == NULL) return Py_None;")
142 ## def outputCheckConvertArg(self):
143 ## Output("if (v == Py_None) { *p_itself = NULL; return 1; }")
144 ## Output("if (PyInt_Check(v)) { *p_itself = (DialogPtr)PyInt_AsLong(v);")
145 ## Output(" return 1; }")
146 def outputFreeIt(self, itselfname):
147 Output("HRDisposeReference(%s);", itselfname)
149 # Create the generator groups and link them
150 module = MacModule('HtmlRender', 'Html', includestuff, finalstuff, initstuff)
151 object = MyObjectDefinition('HtmlObject', 'HtmlObj', 'HRReference')
152 module.addobject(object)
154 # Create the generator classes used to populate the lists
155 Function = OSErrFunctionGenerator
156 Method = OSErrMethodGenerator
158 # Create and populate the lists
159 functions = []
160 methods = []
161 execfile("htmlgen.py")
163 f = Function(int, 'HRHTMLRenderingLibAvailable')
164 functions.append(f)
166 # add the populated lists to the generator groups
167 for f in functions: module.add(f)
168 for f in methods: object.add(f)
170 ### Some methods that are currently macro's in C, but will be real routines
171 ### in MacOS 8.
173 ##f = Method(ExistingDialogPtr, 'GetDialogWindow', (DialogRef, 'dialog', InMode))
174 ##object.add(f)
175 ##f = Method(SInt16, 'GetDialogDefaultItem', (DialogRef, 'dialog', InMode))
176 ##object.add(f)
177 ##f = Method(SInt16, 'GetDialogCancelItem', (DialogRef, 'dialog', InMode))
178 ##object.add(f)
179 ##f = Method(SInt16, 'GetDialogKeyboardFocusItem', (DialogRef, 'dialog', InMode))
180 ##object.add(f)
181 ##f = Method(void, 'SetGrafPortOfDialog', (DialogRef, 'dialog', InMode))
182 ##object.add(f)
184 ##setuseritembody = """
185 ## PyObject *new = NULL;
188 ## if (!PyArg_ParseTuple(_args, "|O", &new))
189 ## return NULL;
191 ## if (Dlg_UserItemProc_callback && new && new != Py_None) {
192 ## PyErr_SetString(Dlg_Error, "Another UserItemProc is already installed");
193 ## return NULL;
194 ## }
196 ## if (new == Py_None) {
197 ## new = NULL;
198 ## _res = Py_None;
199 ## Py_INCREF(Py_None);
200 ## } else {
201 ## Py_INCREF(new);
202 ## _res = Py_BuildValue("O&", ResObj_New, (Handle)NewUserItemProc(Dlg_UnivUserItemProc));
203 ## }
205 ## Dlg_UserItemProc_callback = new;
206 ## return _res;
207 ##"""
208 ##f = ManualGenerator("SetUserItemHandler", setuseritembody)
209 ##module.add(f)
211 # generate output
212 SetOutputFileName('HtmlRendermodule.c')
213 module.generate()