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" */
30 finalstuff
= finalstuff
+ """
32 /* Alternative version of ResObj_New, which returns None for null argument */
33 PyObject *OptResObj_New(itself)
40 return ResObj_New(itself);
43 OptResObj_Convert(v, p_itself)
53 PyErr_SetString(PyExc_TypeError, "Resource required");
56 *p_itself = ((ResourceObject *)v)->ob_itself;
62 initstuff
= initstuff
+ """
65 module
= MacModule('Res', 'Res', includestuff
, finalstuff
, initstuff
)
68 if (strcmp(name, "size") == 0)
69 return PyInt_FromLong(GetHandleSize(self->ob_itself));
70 if (strcmp(name, "data") == 0) {
73 state = HGetState(self->ob_itself);
74 HLock(self->ob_itself);
75 res = PyString_FromStringAndSize(
77 GetHandleSize(self->ob_itself));
78 HUnlock(self->ob_itself);
79 HSetState(self->ob_itself, state);
82 if (strcmp(name, "__members__") == 0)
83 return Py_BuildValue("[ss]", "data", "size");
88 ResObj_setattr(self, name, value)
96 if (strcmp(name, "data") != 0 || value == NULL )
98 if ( !PyString_Check(value) )
100 size = PyString_Size(value);
101 data = PyString_AsString(value);
102 /* XXXX Do I need the GetState/SetState calls? */
103 SetHandleSize(self->ob_itself, size);
106 HLock(self->ob_itself);
107 memcpy((char *)*self->ob_itself, data, size);
108 HUnlock(self->ob_itself);
109 /* XXXX Should I do the Changed call immedeately? */
114 class ResDefiniton(GlobalObjectDefinition
):
116 def outputCheckNewArg(self
):
117 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
119 def outputGetattrHook(self
):
120 Output(getattrHookCode
)
122 def outputSetattr(self
):
126 resobject
= ResDefiniton('Resource', 'ResObj', 'Handle')
127 module
.addobject(resobject
)
132 execfile('resgen.py')
133 execfile('resedit.py')
135 for f
in functions
: module
.add(f
)
136 for f
in resmethods
: resobject
.add(f
)
138 SetOutputFileName('Resmodule.c')