test_whitespace_eater_unicode(): Make this test Python 2.1 compatible.
[python/dscho.git] / Mac / Modules / qdoffs / qdoffssupport.py
blob0c4dd2ae4f82045184a49bafa621079cf98e5ad8
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 = 'QDOffscreen.h' # The Apple header file
10 MODNAME = '_Qdoffs' # The name of the module
11 OBJECTNAME = 'GWorld' # The basic name of the objects used here
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX = 'Qdoffs' # The prefix for module-wide routines
15 OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them
16 OBJECTPREFIX = OBJECTNAME + '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 GWorldPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
26 GWorldFlags = Type("GWorldFlags", "l")
27 GDHandle = OpaqueByValueType("GDHandle", "ResObj")
28 OptGDHandle = OpaqueByValueType("GDHandle", "OptResObj")
29 CTabHandle = OpaqueByValueType("CTabHandle", "OptResObj")
30 PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
31 PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
32 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
33 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
34 QDErr = OSErrType("QDErr", 'h')
36 includestuff = includestuff + """
37 #ifdef WITHOUT_FRAMEWORKS
38 #include <QDOffscreen.h>
39 #else
40 #include <Carbon/Carbon.h>
41 #endif
43 #ifdef USE_TOOLBOX_OBJECT_GLUE
44 extern PyObject *_GWorldObj_New(GWorldPtr);
45 extern int _GWorldObj_Convert(PyObject *, GWorldPtr *);
47 #define GWorldObj_New _GWorldObj_New
48 #define GWorldObj_Convert _GWorldObj_Convert
49 #endif
51 #define as_GrafPtr(gworld) ((GrafPtr)(gworld))
53 """
55 initstuff = initstuff + """
56 PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New);
57 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert);
58 """
60 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
61 # XXXX Should inherit from GrafPtr?
62 def outputCheckNewArg(self):
63 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
64 ## def outputInitStructMembers(self):
65 ## GlobalObjectDefinition.outputInitStructMembers(self)
66 ## Output("SetWRefCon(itself, (long)it);")
67 ## def outputCheckConvertArg(self):
68 ## OutLbrace("if (DlgObj_Check(v))")
69 ## Output("*p_itself = ((WindowObject *)v)->ob_itself;")
70 ## Output("return 1;")
71 ## OutRbrace()
72 ## Out("""
73 ## if (v == Py_None) { *p_itself = NULL; return 1; }
74 ## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
75 ## """)
76 def outputFreeIt(self, itselfname):
77 Output("DisposeGWorld(%s);", itselfname)
78 # From here on it's basically all boiler plate...
80 # Create the generator groups and link them
81 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
82 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
83 module.addobject(object)
86 # Create the generator classes used to populate the lists
87 Function = OSErrWeakLinkFunctionGenerator
88 Method = OSErrWeakLinkMethodGenerator
90 # Create and populate the lists
91 functions = []
92 methods = []
93 execfile(INPUTFILE)
95 # A method to convert a GWorldPtr to a GrafPtr
96 f = Method(GrafPtr, 'as_GrafPtr', (GWorldPtr, 'p', InMode))
97 methods.append(f)
100 # Manual generator: get data out of a pixmap
101 pixmapgetbytes_body = """
102 PixMapHandle pm;
103 int from, length;
104 char *cp;
106 if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) )
107 return NULL;
108 cp = GetPixBaseAddr(pm)+from;
109 _res = PyString_FromStringAndSize(cp, length);
110 return _res;
112 f = ManualGenerator("GetPixMapBytes", pixmapgetbytes_body)
113 f.docstring = lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap"""
114 functions.append(f)
116 # Manual generator: store data in a pixmap
117 pixmapputbytes_body = """
118 PixMapHandle pm;
119 int from, length;
120 char *cp, *icp;
122 if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) )
123 return NULL;
124 cp = GetPixBaseAddr(pm)+from;
125 memcpy(cp, icp, length);
126 Py_INCREF(Py_None);
127 _res = Py_None;
128 return _res;
130 f = ManualGenerator("PutPixMapBytes", pixmapputbytes_body)
131 f.docstring = lambda: """(pixmap, int start, string data). Store bytes into the pixmap"""
132 functions.append(f)
134 # add the populated lists to the generator groups
135 # (in a different wordl the scan program would generate this)
136 for f in functions: module.add(f)
137 for f in methods: object.add(f)
141 # generate output (open the output file as late as possible)
142 SetOutputFileName(OUTPUTFILE)
143 module.generate()