1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# 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>
#define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
"""
finalstuff = finalstuff + """
"""
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");
"""
class ResDefiniton(GlobalObjectDefinition):
def outputCheckNewArg(self):
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
def outputGetattrHook(self):
Output(getattrHookCode)
resobject = ResDefiniton('Resource', 'ResObj', 'Handle')
module.addobject(resobject)
functions = []
resmethods = []
execfile('resgen.py')
execfile('resedit.py')
for f in functions: module.add(f)
for f in resmethods: resobject.add(f)
SetOutputFileName('Resmodule.c')
module.generate()
|