fix to work on python <= 2.1
[python/dscho.git] / Mac / Modules / res / ressupport.py
blob37cb6600bc9e3519895051e1d14fae81775986a7
1 # This script will generate the Resources interface for Python.
2 # It uses the "bgen" package to generate C code.
3 # It execs the file resgen.py which contain the function definitions
4 # (resgen.py was generated by resscan.py, scanning the <Resources.h> header file).
6 from macsupport import *
8 class ResMixIn:
10 def checkit(self):
11 if self.returntype.__class__ != OSErrType:
12 OutLbrace()
13 Output("OSErr _err = ResError();")
14 Output("if (_err != noErr) return PyMac_Error(_err);")
15 OutRbrace()
16 FunctionGenerator.checkit(self) # XXX
18 class ResFunction(ResMixIn, OSErrWeakLinkFunctionGenerator): pass
19 class ResMethod(ResMixIn, OSErrWeakLinkMethodGenerator): pass
21 RsrcChainLocation = Type("RsrcChainLocation", "h")
22 FSCatalogInfoBitmap = FakeType("0") # Type("FSCatalogInfoBitmap", "l")
23 FSCatalogInfo_ptr = FakeType("(FSCatalogInfo *)0")
25 # includestuff etc. are imported from macsupport
27 includestuff = includestuff + """
28 #ifndef PyDoc_STR
29 #define PyDoc_STR(x) (x)
30 #endif
31 #ifdef WITHOUT_FRAMEWORKS
32 #include <Resources.h>
33 #include <string.h>
34 #else
35 #include <Carbon/Carbon.h>
36 #endif
38 #ifdef USE_TOOLBOX_OBJECT_GLUE
39 extern PyObject *_ResObj_New(Handle);
40 extern int _ResObj_Convert(PyObject *, Handle *);
41 extern PyObject *_OptResObj_New(Handle);
42 extern int _OptResObj_Convert(PyObject *, Handle *);
43 #define ResObj_New _ResObj_New
44 #define ResObj_Convert _ResObj_Convert
45 #define OptResObj_New _OptResObj_New
46 #define OptResObj_Convert _OptResObj_Convert
47 #endif
49 /* Function to dispose a resource, with a "normal" calling sequence */
50 static void
51 PyMac_AutoDisposeHandle(Handle h)
53 DisposeHandle(h);
55 """
57 finalstuff = finalstuff + """
59 /* Alternative version of ResObj_New, which returns None for null argument */
60 PyObject *OptResObj_New(Handle itself)
62 if (itself == NULL) {
63 Py_INCREF(Py_None);
64 return Py_None;
66 return ResObj_New(itself);
69 int OptResObj_Convert(PyObject *v, Handle *p_itself)
71 PyObject *tmp;
73 if ( v == Py_None ) {
74 *p_itself = NULL;
75 return 1;
77 if (ResObj_Check(v))
79 *p_itself = ((ResourceObject *)v)->ob_itself;
80 return 1;
82 /* If it isn't a resource yet see whether it is convertible */
83 if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
84 *p_itself = ((ResourceObject *)tmp)->ob_itself;
85 Py_DECREF(tmp);
86 return 1;
88 PyErr_Clear();
89 PyErr_SetString(PyExc_TypeError, "Resource required");
90 return 0;
92 """
94 initstuff = initstuff + """
95 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New);
96 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert);
97 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New);
98 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert);
99 """
101 module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff)
103 class ResDefinition(PEP253Mixin, GlobalObjectDefinition):
104 getsetlist = [
105 ('data',
107 PyObject *res;
108 char state;
110 state = HGetState(self->ob_itself);
111 HLock(self->ob_itself);
112 res = PyString_FromStringAndSize(
113 *self->ob_itself,
114 GetHandleSize(self->ob_itself));
115 HUnlock(self->ob_itself);
116 HSetState(self->ob_itself, state);
117 return res;
118 """,
120 char *data;
121 long size;
123 if ( v == NULL )
124 return -1;
125 if ( !PyString_Check(v) )
126 return -1;
127 size = PyString_Size(v);
128 data = PyString_AsString(v);
129 /* XXXX Do I need the GetState/SetState calls? */
130 SetHandleSize(self->ob_itself, size);
131 if ( MemError())
132 return -1;
133 HLock(self->ob_itself);
134 memcpy((char *)*self->ob_itself, data, size);
135 HUnlock(self->ob_itself);
136 /* XXXX Should I do the Changed call immedeately? */
137 return 0;
138 """,
139 'The resource data'
140 ), (
141 'size',
142 'return PyInt_FromLong(GetHandleSize(self->ob_itself));',
143 None,
144 'The length of the resource data'
147 def outputCheckNewArg(self):
148 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
150 def outputCheckConvertArg(self):
151 # if it isn't a resource we may be able to coerce it
152 Output("if (!%s_Check(v))", self.prefix)
153 OutLbrace()
154 Output("PyObject *tmp;")
155 Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
156 OutLbrace()
157 Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
158 Output("Py_DECREF(tmp);")
159 Output("return 1;")
160 OutRbrace()
161 Output("PyErr_Clear();")
162 OutRbrace()
164 def outputStructMembers(self):
165 GlobalObjectDefinition.outputStructMembers(self)
166 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
168 def outputInitStructMembers(self):
169 GlobalObjectDefinition.outputInitStructMembers(self)
170 Output("it->ob_freeit = NULL;")
172 def outputCleanupStructMembers(self):
173 Output("if (self->ob_freeit && self->ob_itself)")
174 OutLbrace()
175 Output("self->ob_freeit(self->ob_itself);")
176 OutRbrace()
177 Output("self->ob_itself = NULL;")
179 def output_tp_newBody(self):
180 Output("PyObject *self;")
181 Output
182 Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
183 Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
184 Output("((%s *)self)->ob_freeit = NULL;", self.objecttype)
185 Output("return self;")
187 def output_tp_initBody(self):
188 Output("char *srcdata = NULL;")
189 Output("int srclen = 0;")
190 Output("%s itself;", self.itselftype);
191 Output("char *kw[] = {\"itself\", 0};")
192 Output()
193 Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself))",
194 self.prefix);
195 OutLbrace()
196 Output("((%s *)self)->ob_itself = itself;", self.objecttype)
197 Output("return 0;")
198 OutRbrace()
199 Output("PyErr_Clear();")
200 Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;")
201 Output("if ((itself = NewHandle(srclen)) == NULL)")
202 OutLbrace()
203 Output("PyErr_NoMemory();")
204 Output("return 0;")
205 OutRbrace()
206 Output("((%s *)self)->ob_itself = itself;", self.objecttype)
207 # XXXX Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;")
208 Output("if (srclen && srcdata)")
209 OutLbrace()
210 Output("HLock(itself);")
211 Output("memcpy(*itself, srcdata, srclen);")
212 Output("HUnlock(itself);")
213 OutRbrace()
214 Output("return 0;")
216 resobject = ResDefinition('Resource', 'ResObj', 'Handle')
217 module.addobject(resobject)
219 functions = []
220 resmethods = []
222 execfile('resgen.py')
223 execfile('resedit.py')
225 for f in functions: module.add(f)
226 for f in resmethods: resobject.add(f)
228 SetOutputFileName('_Resmodule.c')
229 module.generate()