Fix three PyChecker-detected gotchas.
[python/dscho.git] / Mac / Modules / evt / evtsupport.py
blob7725ded54baa92e451ee24ab190a3514c6406761
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 = 'Events.h' # The Apple header file
10 MODNAME = 'Evt' # The name of the module
11 OBJECTNAME = 'Event' # The basic name of the objects used here
12 KIND = 'Record' # Usually 'Ptr' or 'Handle'
14 # The following is *usually* unchanged but may still require tuning
15 MODPREFIX = MODNAME # The prefix for module-wide routines
16 OBJECTTYPE = OBJECTNAME + KIND # The C type used to represent them
17 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
18 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
19 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
21 from macsupport import *
23 # Create the type objects
25 #WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
27 RgnHandle = FakeType("(RgnHandle)0")
28 # XXXX Should be next, but this will break a lot of code...
29 # RgnHandle = OpaqueByValueType("RgnHandle", "OptResObj")
31 KeyMap = ArrayOutputBufferType("KeyMap")
32 ##MacOSEventKind = Type("MacOSEventKind", "h") # Old-style
33 ##MacOSEventMask = Type("MacOSEventMask", "h") # Old-style
34 EventMask = Type("EventMask", "H")
35 EventKind = Type("EventKind", "H")
37 includestuff = includestuff + """
38 #include <%s>""" % MACHEADERFILE + """
39 """
41 class MyObjectDefinition(GlobalObjectDefinition):
42 def outputCheckNewArg(self):
43 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
44 def outputCheckConvertArg(self):
45 OutLbrace("if (DlgObj_Check(v))")
46 Output("*p_itself = ((WindowObject *)v)->ob_itself;")
47 Output("return 1;")
48 OutRbrace()
49 Out("""
50 if (v == Py_None) { *p_itself = NULL; return 1; }
51 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
52 """)
54 # From here on it's basically all boiler plate...
56 # Create the generator groups and link them
57 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
58 ##object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
59 ##module.addobject(object)
61 # Create the generator classes used to populate the lists
62 Function = OSErrFunctionGenerator
63 ##Method = OSErrMethodGenerator
65 # Create and populate the lists
66 functions = []
67 ##methods = []
68 execfile(INPUTFILE)
70 # add the populated lists to the generator groups
71 # (in a different wordl the scan program would generate this)
72 for f in functions: module.add(f)
73 ##for f in methods: object.add(f)
75 WaitNextEvent_body = """
76 Boolean _rv;
77 EventMask eventMask;
78 EventRecord theEvent;
79 UInt32 sleep;
80 Handle mouseregion = (Handle)0;
82 if (!PyArg_ParseTuple(_args, "Hl|O&",
83 &eventMask,
84 &sleep,
85 OptResObj_Convert, &mouseregion))
86 return NULL;
87 _rv = WaitNextEvent(eventMask,
88 &theEvent,
89 sleep,
90 (RgnHandle)mouseregion);
91 _res = Py_BuildValue("bO&",
92 _rv,
93 PyMac_BuildEventRecord, &theEvent);
94 return _res;
95 """
96 f = ManualGenerator("WaitNextEvent", WaitNextEvent_body);
97 f.docstring = lambda: "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)"
98 module.add(f)
100 # generate output (open the output file as late as possible)
101 SetOutputFileName(OUTPUTFILE)
102 module.generate()