1995-01-30 07:53:55 -04:00
|
|
|
# This script will generate the Resources interface for Python.
|
|
|
|
# It uses the "bgen" package to generate C code.
|
|
|
|
# It execs the file resgen.py which contain the function definitions
|
|
|
|
# (resgen.py was generated by resscan.py, scanning the <Resources.h> header file).
|
|
|
|
|
|
|
|
from macsupport import *
|
|
|
|
|
|
|
|
|
|
|
|
class ResMixIn:
|
|
|
|
|
|
|
|
def checkit(self):
|
|
|
|
OutLbrace()
|
|
|
|
Output("OSErr _err = ResError();")
|
|
|
|
Output("if (_err != noErr) return PyMac_Error(_err);")
|
|
|
|
OutRbrace()
|
|
|
|
FunctionGenerator.checkit(self) # XXX
|
|
|
|
|
|
|
|
class ResFunction(ResMixIn, FunctionGenerator): pass
|
|
|
|
class ResMethod(ResMixIn, MethodGenerator): pass
|
|
|
|
|
|
|
|
# includestuff etc. are imported from macsupport
|
|
|
|
|
|
|
|
includestuff = includestuff + """
|
|
|
|
#include <Resources.h>
|
1997-08-15 11:36:45 -03:00
|
|
|
#include <string.h>
|
1995-01-30 07:53:55 -04:00
|
|
|
|
|
|
|
#define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
|
2000-03-08 12:58:15 -04:00
|
|
|
|
|
|
|
/* Function to dispose a resource, with a "normal" calling sequence */
|
|
|
|
static void
|
|
|
|
PyMac_AutoDisposeHandle(Handle h)
|
|
|
|
{
|
|
|
|
DisposeHandle(h);
|
|
|
|
}
|
1995-01-30 07:53:55 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
finalstuff = finalstuff + """
|
1995-08-17 11:29:23 -03:00
|
|
|
|
|
|
|
/* Alternative version of ResObj_New, which returns None for null argument */
|
1995-12-12 11:02:03 -04:00
|
|
|
PyObject *OptResObj_New(itself)
|
1995-08-17 11:29:23 -03:00
|
|
|
Handle itself;
|
|
|
|
{
|
|
|
|
if (itself == NULL) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
return ResObj_New(itself);
|
|
|
|
}
|
|
|
|
|
1995-12-12 11:02:03 -04:00
|
|
|
OptResObj_Convert(v, p_itself)
|
|
|
|
PyObject *v;
|
|
|
|
Handle *p_itself;
|
|
|
|
{
|
1999-12-12 18:57:29 -04:00
|
|
|
PyObject *tmp;
|
|
|
|
|
1995-12-12 11:02:03 -04:00
|
|
|
if ( v == Py_None ) {
|
|
|
|
*p_itself = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
1999-12-12 18:57:29 -04:00
|
|
|
if (ResObj_Check(v))
|
1995-12-12 11:02:03 -04:00
|
|
|
{
|
1999-12-12 18:57:29 -04:00
|
|
|
*p_itself = ((ResourceObject *)v)->ob_itself;
|
|
|
|
return 1;
|
1995-12-12 11:02:03 -04:00
|
|
|
}
|
1999-12-12 18:57:29 -04:00
|
|
|
/* If it isn't a resource yet see whether it is convertible */
|
|
|
|
if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
|
|
|
|
*p_itself = ((ResourceObject *)tmp)->ob_itself;
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Resource required");
|
|
|
|
return 0;
|
1995-12-12 11:02:03 -04:00
|
|
|
}
|
1995-01-30 07:53:55 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
initstuff = initstuff + """
|
|
|
|
"""
|
|
|
|
|
|
|
|
module = MacModule('Res', 'Res', includestuff, finalstuff, initstuff)
|
|
|
|
|
|
|
|
getattrHookCode = """
|
|
|
|
if (strcmp(name, "size") == 0)
|
|
|
|
return PyInt_FromLong(GetHandleSize(self->ob_itself));
|
|
|
|
if (strcmp(name, "data") == 0) {
|
|
|
|
PyObject *res;
|
|
|
|
char state;
|
|
|
|
state = HGetState(self->ob_itself);
|
|
|
|
HLock(self->ob_itself);
|
|
|
|
res = PyString_FromStringAndSize(
|
|
|
|
*self->ob_itself,
|
|
|
|
GetHandleSize(self->ob_itself));
|
|
|
|
HUnlock(self->ob_itself);
|
|
|
|
HSetState(self->ob_itself, state);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (strcmp(name, "__members__") == 0)
|
|
|
|
return Py_BuildValue("[ss]", "data", "size");
|
|
|
|
"""
|
|
|
|
|
1995-06-18 17:20:27 -03:00
|
|
|
setattrCode = """
|
|
|
|
static int
|
|
|
|
ResObj_setattr(self, name, value)
|
|
|
|
ResourceObject *self;
|
|
|
|
char *name;
|
|
|
|
PyObject *value;
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
long size;
|
|
|
|
|
|
|
|
if (strcmp(name, "data") != 0 || value == NULL )
|
|
|
|
return -1;
|
|
|
|
if ( !PyString_Check(value) )
|
|
|
|
return -1;
|
|
|
|
size = PyString_Size(value);
|
|
|
|
data = PyString_AsString(value);
|
|
|
|
/* XXXX Do I need the GetState/SetState calls? */
|
|
|
|
SetHandleSize(self->ob_itself, size);
|
|
|
|
if ( MemError())
|
|
|
|
return -1;
|
|
|
|
HLock(self->ob_itself);
|
|
|
|
memcpy((char *)*self->ob_itself, data, size);
|
|
|
|
HUnlock(self->ob_itself);
|
|
|
|
/* XXXX Should I do the Changed call immedeately? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2000-03-08 12:58:15 -04:00
|
|
|
class ResDefinition(GlobalObjectDefinition):
|
1995-01-30 07:53:55 -04:00
|
|
|
|
|
|
|
def outputCheckNewArg(self):
|
|
|
|
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
|
1999-12-12 18:57:29 -04:00
|
|
|
|
|
|
|
def outputCheckConvertArg(self):
|
|
|
|
# if it isn't a resource we may be able to coerce it
|
|
|
|
Output("if (!%s_Check(v))", self.prefix)
|
|
|
|
OutLbrace()
|
|
|
|
Output("PyObject *tmp;")
|
|
|
|
Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
|
|
|
|
OutLbrace()
|
|
|
|
Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
|
|
|
|
Output("Py_DECREF(tmp);")
|
|
|
|
Output("return 1;")
|
|
|
|
OutRbrace()
|
|
|
|
Output("PyErr_Clear();")
|
|
|
|
OutRbrace()
|
1995-01-30 07:53:55 -04:00
|
|
|
|
|
|
|
def outputGetattrHook(self):
|
|
|
|
Output(getattrHookCode)
|
1995-06-18 17:20:27 -03:00
|
|
|
|
|
|
|
def outputSetattr(self):
|
|
|
|
Output(setattrCode)
|
1995-01-30 07:53:55 -04:00
|
|
|
|
2000-03-08 12:58:15 -04:00
|
|
|
def outputStructMembers(self):
|
|
|
|
GlobalObjectDefinition.outputStructMembers(self)
|
|
|
|
Output("void (*ob_freeit)(%s ptr);", self.itselftype)
|
|
|
|
|
|
|
|
def outputInitStructMembers(self):
|
|
|
|
GlobalObjectDefinition.outputInitStructMembers(self)
|
|
|
|
Output("it->ob_freeit = NULL;")
|
|
|
|
|
|
|
|
def outputCleanupStructMembers(self):
|
|
|
|
Output("if (self->ob_freeit && self->ob_itself)")
|
|
|
|
OutLbrace()
|
|
|
|
Output("self->ob_freeit(self->ob_itself);")
|
|
|
|
OutRbrace()
|
|
|
|
Output("self->ob_itself = NULL;")
|
|
|
|
|
1995-01-30 07:53:55 -04:00
|
|
|
|
2000-03-08 12:58:15 -04:00
|
|
|
resobject = ResDefinition('Resource', 'ResObj', 'Handle')
|
1995-01-30 07:53:55 -04:00
|
|
|
module.addobject(resobject)
|
|
|
|
|
|
|
|
functions = []
|
|
|
|
resmethods = []
|
|
|
|
|
|
|
|
execfile('resgen.py')
|
1995-02-05 12:54:27 -04:00
|
|
|
execfile('resedit.py')
|
1995-01-30 07:53:55 -04:00
|
|
|
|
|
|
|
for f in functions: module.add(f)
|
|
|
|
for f in resmethods: resobject.add(f)
|
|
|
|
|
|
|
|
SetOutputFileName('Resmodule.c')
|
|
|
|
module.generate()
|