This commit was manufactured by cvs2svn to create tag 'r212'.
[python/dscho.git] / Mac / Modules / res / ressupport.py
blob8be1b94cf96af90b03b3eccd24633fbb79d44910
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 *
9 class ResMixIn:
11 def checkit(self):
12 OutLbrace()
13 Output("OSErr _err = ResError();")
14 Output("if (_err != noErr) return PyMac_Error(_err);")
15 OutRbrace()
16 FunctionGenerator.checkit(self) # XXX
18 class ResFunction(ResMixIn, FunctionGenerator): pass
19 class ResMethod(ResMixIn, MethodGenerator): pass
21 RsrcChainLocation = Type("RsrcChainLocation", "h")
23 # includestuff etc. are imported from macsupport
25 includestuff = includestuff + """
26 #ifdef WITHOUT_FRAMEWORKS
27 #include <Resources.h>
28 #include <string.h>
29 #else
30 #include <Carbon/Carbon.h>
31 #endif
33 #ifdef USE_TOOLBOX_OBJECT_GLUE
34 extern PyObject *_ResObj_New(Handle);
35 extern int _ResObj_Convert(PyObject *, Handle *);
36 extern PyObject *_OptResObj_New(Handle);
37 extern int _OptResObj_Convert(PyObject *, Handle *);
38 #define ResObj_New _ResObj_New
39 #define ResObj_Convert _ResObj_Convert
40 #define OptResObj_New _OptResObj_New
41 #define OptResObj_Convert _OptResObj_Convert
42 #endif
44 /* Function to dispose a resource, with a "normal" calling sequence */
45 static void
46 PyMac_AutoDisposeHandle(Handle h)
48 DisposeHandle(h);
50 """
52 finalstuff = finalstuff + """
54 /* Alternative version of ResObj_New, which returns None for null argument */
55 PyObject *OptResObj_New(Handle itself)
57 if (itself == NULL) {
58 Py_INCREF(Py_None);
59 return Py_None;
61 return ResObj_New(itself);
64 OptResObj_Convert(PyObject *v, Handle *p_itself)
66 PyObject *tmp;
68 if ( v == Py_None ) {
69 *p_itself = NULL;
70 return 1;
72 if (ResObj_Check(v))
74 *p_itself = ((ResourceObject *)v)->ob_itself;
75 return 1;
77 /* If it isn't a resource yet see whether it is convertible */
78 if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
79 *p_itself = ((ResourceObject *)tmp)->ob_itself;
80 Py_DECREF(tmp);
81 return 1;
83 PyErr_Clear();
84 PyErr_SetString(PyExc_TypeError, "Resource required");
85 return 0;
87 """
89 initstuff = initstuff + """
90 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New);
91 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert);
92 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New);
93 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert);
94 """
96 module = MacModule('Res', 'Res', includestuff, finalstuff, initstuff)
98 getattrHookCode = """
99 if (strcmp(name, "size") == 0)
100 return PyInt_FromLong(GetHandleSize(self->ob_itself));
101 if (strcmp(name, "data") == 0) {
102 PyObject *res;
103 char state;
104 state = HGetState(self->ob_itself);
105 HLock(self->ob_itself);
106 res = PyString_FromStringAndSize(
107 *self->ob_itself,
108 GetHandleSize(self->ob_itself));
109 HUnlock(self->ob_itself);
110 HSetState(self->ob_itself, state);
111 return res;
113 if (strcmp(name, "__members__") == 0)
114 return Py_BuildValue("[ss]", "data", "size");
117 setattrCode = """
118 static int
119 ResObj_setattr(ResourceObject *self, char *name, PyObject *value)
121 char *data;
122 long size;
124 if (strcmp(name, "data") != 0 || value == NULL )
125 return -1;
126 if ( !PyString_Check(value) )
127 return -1;
128 size = PyString_Size(value);
129 data = PyString_AsString(value);
130 /* XXXX Do I need the GetState/SetState calls? */
131 SetHandleSize(self->ob_itself, size);
132 if ( MemError())
133 return -1;
134 HLock(self->ob_itself);
135 memcpy((char *)*self->ob_itself, data, size);
136 HUnlock(self->ob_itself);
137 /* XXXX Should I do the Changed call immedeately? */
138 return 0;
142 class ResDefinition(GlobalObjectDefinition):
144 def outputCheckNewArg(self):
145 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
147 def outputCheckConvertArg(self):
148 # if it isn't a resource we may be able to coerce it
149 Output("if (!%s_Check(v))", self.prefix)
150 OutLbrace()
151 Output("PyObject *tmp;")
152 Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
153 OutLbrace()
154 Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
155 Output("Py_DECREF(tmp);")
156 Output("return 1;")
157 OutRbrace()
158 Output("PyErr_Clear();")
159 OutRbrace()
161 def outputGetattrHook(self):
162 Output(getattrHookCode)
164 def outputSetattr(self):
165 Output(setattrCode)
167 def outputStructMembers(self):
168 GlobalObjectDefinition.outputStructMembers(self)
169 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
171 def outputInitStructMembers(self):
172 GlobalObjectDefinition.outputInitStructMembers(self)
173 Output("it->ob_freeit = NULL;")
175 def outputCleanupStructMembers(self):
176 Output("if (self->ob_freeit && self->ob_itself)")
177 OutLbrace()
178 Output("self->ob_freeit(self->ob_itself);")
179 OutRbrace()
180 Output("self->ob_itself = NULL;")
183 resobject = ResDefinition('Resource', 'ResObj', 'Handle')
184 module.addobject(resobject)
186 functions = []
187 resmethods = []
189 execfile('resgen.py')
190 execfile('resedit.py')
192 for f in functions: module.add(f)
193 for f in resmethods: resobject.add(f)
195 SetOutputFileName('Resmodule.c')
196 module.generate()