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 *
13 Output("OSErr _err = ResError();")
14 Output("if (_err != noErr) return PyMac_Error(_err);")
16 FunctionGenerator
.checkit(self
) # XXX
18 class ResFunction(ResMixIn
, FunctionGenerator
): pass
19 class ResMethod(ResMixIn
, MethodGenerator
): pass
21 # includestuff etc. are imported from macsupport
23 includestuff
= includestuff
+ """
24 #include <Resources.h>
27 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
29 /* Function to dispose a resource, with a "normal" calling sequence */
31 PyMac_AutoDisposeHandle(Handle h)
37 finalstuff
= finalstuff
+ """
39 /* Alternative version of ResObj_New, which returns None for null argument */
40 PyObject *OptResObj_New(itself)
47 return ResObj_New(itself);
50 OptResObj_Convert(v, p_itself)
62 *p_itself = ((ResourceObject *)v)->ob_itself;
65 /* If it isn't a resource yet see whether it is convertible */
66 if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
67 *p_itself = ((ResourceObject *)tmp)->ob_itself;
72 PyErr_SetString(PyExc_TypeError, "Resource required");
77 initstuff
= initstuff
+ """
80 module
= MacModule('Res', 'Res', includestuff
, finalstuff
, initstuff
)
83 if (strcmp(name, "size") == 0)
84 return PyInt_FromLong(GetHandleSize(self->ob_itself));
85 if (strcmp(name, "data") == 0) {
88 state = HGetState(self->ob_itself);
89 HLock(self->ob_itself);
90 res = PyString_FromStringAndSize(
92 GetHandleSize(self->ob_itself));
93 HUnlock(self->ob_itself);
94 HSetState(self->ob_itself, state);
97 if (strcmp(name, "__members__") == 0)
98 return Py_BuildValue("[ss]", "data", "size");
103 ResObj_setattr(self, name, value)
104 ResourceObject *self;
111 if (strcmp(name, "data") != 0 || value == NULL )
113 if ( !PyString_Check(value) )
115 size = PyString_Size(value);
116 data = PyString_AsString(value);
117 /* XXXX Do I need the GetState/SetState calls? */
118 SetHandleSize(self->ob_itself, size);
121 HLock(self->ob_itself);
122 memcpy((char *)*self->ob_itself, data, size);
123 HUnlock(self->ob_itself);
124 /* XXXX Should I do the Changed call immedeately? */
129 class ResDefinition(GlobalObjectDefinition
):
131 def outputCheckNewArg(self
):
132 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
134 def outputCheckConvertArg(self
):
135 # if it isn't a resource we may be able to coerce it
136 Output("if (!%s_Check(v))", self
.prefix
)
138 Output("PyObject *tmp;")
139 Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
141 Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
142 Output("Py_DECREF(tmp);")
145 Output("PyErr_Clear();")
148 def outputGetattrHook(self
):
149 Output(getattrHookCode
)
151 def outputSetattr(self
):
154 def outputStructMembers(self
):
155 GlobalObjectDefinition
.outputStructMembers(self
)
156 Output("void (*ob_freeit)(%s ptr);", self
.itselftype
)
158 def outputInitStructMembers(self
):
159 GlobalObjectDefinition
.outputInitStructMembers(self
)
160 Output("it->ob_freeit = NULL;")
162 def outputCleanupStructMembers(self
):
163 Output("if (self->ob_freeit && self->ob_itself)")
165 Output("self->ob_freeit(self->ob_itself);")
167 Output("self->ob_itself = NULL;")
170 resobject
= ResDefinition('Resource', 'ResObj', 'Handle')
171 module
.addobject(resobject
)
176 execfile('resgen.py')
177 execfile('resedit.py')
179 for f
in functions
: module
.add(f
)
180 for f
in resmethods
: resobject
.add(f
)
182 SetOutputFileName('Resmodule.c')