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
+ """
29 #define PyDoc_STR(x) (x)
31 #ifdef WITHOUT_FRAMEWORKS
32 #include <Resources.h>
35 #include <Carbon/Carbon.h>
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
49 /* Function to dispose a resource, with a "normal" calling sequence */
51 PyMac_AutoDisposeHandle(Handle h)
57 finalstuff
= finalstuff
+ """
59 /* Alternative version of ResObj_New, which returns None for null argument */
60 PyObject *OptResObj_New(Handle itself)
66 return ResObj_New(itself);
69 int OptResObj_Convert(PyObject *v, Handle *p_itself)
79 *p_itself = ((ResourceObject *)v)->ob_itself;
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;
89 PyErr_SetString(PyExc_TypeError, "Resource required");
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);
101 module
= MacModule('_Res', 'Res', includestuff
, finalstuff
, initstuff
)
103 class ResDefinition(PEP253Mixin
, GlobalObjectDefinition
):
110 state = HGetState(self->ob_itself);
111 HLock(self->ob_itself);
112 res = PyString_FromStringAndSize(
114 GetHandleSize(self->ob_itself));
115 HUnlock(self->ob_itself);
116 HSetState(self->ob_itself, state);
125 if ( !PyString_Check(v) )
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);
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? */
142 'return PyInt_FromLong(GetHandleSize(self->ob_itself));',
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
)
154 Output("PyObject *tmp;")
155 Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
157 Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
158 Output("Py_DECREF(tmp);")
161 Output("PyErr_Clear();")
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)")
175 Output("self->ob_freeit(self->ob_itself);")
177 Output("self->ob_itself = NULL;")
179 def output_tp_newBody(self
):
180 Output("PyObject *self;")
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};")
193 Output("if (PyArg_ParseTupleAndKeywords(args, kwds, \"O&\", kw, %s_Convert, &itself))",
196 Output("((%s *)self)->ob_itself = itself;", self
.objecttype
)
199 Output("PyErr_Clear();")
200 Output("if (!PyArg_ParseTupleAndKeywords(args, kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;")
201 Output("if ((itself = NewHandle(srclen)) == NULL)")
203 Output("PyErr_NoMemory();")
206 Output("((%s *)self)->ob_itself = itself;", self
.objecttype
)
207 # XXXX Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;")
208 Output("if (srclen && srcdata)")
210 Output("HLock(itself);")
211 Output("memcpy(*itself, srcdata, srclen);")
212 Output("HUnlock(itself);")
216 resobject
= ResDefinition('Resource', 'ResObj', 'Handle')
217 module
.addobject(resobject
)
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')