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 *
11 if self.returntype.__class__ != OSErrType:
13 Output("OSErr _err = ResError();")
14 Output("if (_err != noErr) return PyMac_Error(_err);")
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 #include <Carbon/Carbon.h>
30 #ifdef USE_TOOLBOX_OBJECT_GLUE
31 extern PyObject *_ResObj_New(Handle);
32 extern int _ResObj_Convert(PyObject *, Handle *);
33 extern PyObject *_OptResObj_New(Handle);
34 extern int _OptResObj_Convert(PyObject *, Handle *);
35 #define ResObj_New _ResObj_New
36 #define ResObj_Convert _ResObj_Convert
37 #define OptResObj_New _OptResObj_New
38 #define OptResObj_Convert _OptResObj_Convert
41 /* Function to dispose a resource, with a "normal" calling sequence */
43 PyMac_AutoDisposeHandle(Handle h)
49 finalstuff = finalstuff + """
51 /* Alternative version of ResObj_New, which returns None for null argument */
52 PyObject *OptResObj_New(Handle itself)
58 return ResObj_New(itself);
61 int OptResObj_Convert(PyObject *v, Handle *p_itself)
71 *p_itself = ((ResourceObject *)v)->ob_itself;
74 /* If it isn't a resource yet see whether it is convertible */
75 if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
76 *p_itself = ((ResourceObject *)tmp)->ob_itself;
81 PyErr_SetString(PyExc_TypeError, "Resource required");
86 initstuff = initstuff + """
87 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New);
88 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert);
89 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New);
90 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert);
93 module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff)
95 class ResDefinition(PEP253Mixin, GlobalObjectDefinition):
102 state = HGetState(self->ob_itself);
103 HLock(self->ob_itself);
104 res = PyString_FromStringAndSize(
106 GetHandleSize(self->ob_itself));
107 HUnlock(self->ob_itself);
108 HSetState(self->ob_itself, state);
117 if ( !PyString_Check(v) )
119 size = PyString_Size(v);
120 data = PyString_AsString(v);
121 /* XXXX Do I need the GetState/SetState calls? */
122 SetHandleSize(self->ob_itself, size);
125 HLock(self->ob_itself);
126 memcpy((char *)*self->ob_itself, data, size);
127 HUnlock(self->ob_itself);
128 /* XXXX Should I do the Changed call immedeately? */
134 'return PyInt_FromLong(GetHandleSize(self->ob_itself));',
136 'The length of the resource data'
139 def outputCheckNewArg(self):
140 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
142 def outputCheckConvertArg(self):
143 # if it isn't a resource we may be able to coerce it
144 Output("if (!%s_Check(v))", self.prefix)
146 Output("PyObject *tmp;")
147 Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
149 Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
150 Output("Py_DECREF(tmp);")
153 Output("PyErr_Clear();")
156 def outputStructMembers(self):
157 GlobalObjectDefinition.outputStructMembers(self)
158 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
160 def outputInitStructMembers(self):
161 GlobalObjectDefinition.outputInitStructMembers(self)
162 Output("it->ob_freeit = NULL;")
164 def outputCleanupStructMembers(self):
165 Output("if (self->ob_freeit && self->ob_itself)")
167 Output("self->ob_freeit(self->ob_itself);")
169 Output("self->ob_itself = NULL;")
171 def output_tp_newBody(self):
172 Output("PyObject *self;")
174 Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
175 Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
176 Output("((%s *)self)->ob_freeit = NULL;", self.objecttype)
177 Output("return self;")
179 def output_tp_initBody(self):
180 Output("char *srcdata = NULL;")
181 Output("int srclen = 0;")
182 Output("%s itself;", self.itselftype);
183 Output("char *kw[] = {\"itself\", 0};")
185 Output("if (PyArg_ParseTupleAndKeywords(_args, _kwds, \"O&\", kw, %s_Convert, &itself))",
188 Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
191 Output("PyErr_Clear();")
192 Output("if (!PyArg_ParseTupleAndKeywords(_args, _kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;")
193 Output("if ((itself = NewHandle(srclen)) == NULL)")
195 Output("PyErr_NoMemory();")
198 Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
199 # XXXX Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;")
200 Output("if (srclen && srcdata)")
202 Output("HLock(itself);")
203 Output("memcpy(*itself, srcdata, srclen);")
204 Output("HUnlock(itself);")
208 resobject = ResDefinition('Resource', 'ResObj', 'Handle')
209 module.addobject(resobject)
214 execfile('resgen.py')
215 execfile('resedit.py')
217 for f in functions: module.add(f)
218 for f in resmethods: resobject.add(f)
220 SetOutputFileName('_Resmodule.c')