This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Mac / Modules / res / ressupport.py
blob30c44453906e2766ec604cc0bee570b69bcaa63c
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 *
8 class ResMixIn:
10 def checkit(self):
11 if self.returntype.__class__ != OSErrType:
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, OSErrWeakLinkFunctionGenerator): pass
19 class ResMethod(ResMixIn, OSErrWeakLinkMethodGenerator): pass
21 RsrcChainLocation = Type("RsrcChainLocation", "h")
22 FSCatalogInfoBitmap = FakeType("0") # Type("FSCatalogInfoBitmap", "l")
23 FSCatalogInfo_ptr = FakeType("(FSCatalogInfo *)0")
25 # includestuff etc. are imported from macsupport
27 includestuff = includestuff + """
28 #ifdef WITHOUT_FRAMEWORKS
29 #include <Resources.h>
30 #include <string.h>
31 #else
32 #include <Carbon/Carbon.h>
33 #endif
35 #ifdef USE_TOOLBOX_OBJECT_GLUE
36 extern PyObject *_ResObj_New(Handle);
37 extern int _ResObj_Convert(PyObject *, Handle *);
38 extern PyObject *_OptResObj_New(Handle);
39 extern int _OptResObj_Convert(PyObject *, Handle *);
40 #define ResObj_New _ResObj_New
41 #define ResObj_Convert _ResObj_Convert
42 #define OptResObj_New _OptResObj_New
43 #define OptResObj_Convert _OptResObj_Convert
44 #endif
46 /* Function to dispose a resource, with a "normal" calling sequence */
47 static void
48 PyMac_AutoDisposeHandle(Handle h)
50 DisposeHandle(h);
52 """
54 finalstuff = finalstuff + """
56 /* Alternative version of ResObj_New, which returns None for null argument */
57 PyObject *OptResObj_New(Handle itself)
59 if (itself == NULL) {
60 Py_INCREF(Py_None);
61 return Py_None;
63 return ResObj_New(itself);
66 int OptResObj_Convert(PyObject *v, Handle *p_itself)
68 PyObject *tmp;
70 if ( v == Py_None ) {
71 *p_itself = NULL;
72 return 1;
74 if (ResObj_Check(v))
76 *p_itself = ((ResourceObject *)v)->ob_itself;
77 return 1;
79 /* If it isn't a resource yet see whether it is convertible */
80 if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
81 *p_itself = ((ResourceObject *)tmp)->ob_itself;
82 Py_DECREF(tmp);
83 return 1;
85 PyErr_Clear();
86 PyErr_SetString(PyExc_TypeError, "Resource required");
87 return 0;
89 """
91 initstuff = initstuff + """
92 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New);
93 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert);
94 PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New);
95 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert);
96 """
98 module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff)
100 getattrHookCode = """
101 if (strcmp(name, "size") == 0)
102 return PyInt_FromLong(GetHandleSize(self->ob_itself));
103 if (strcmp(name, "data") == 0) {
104 PyObject *res;
105 char state;
106 state = HGetState(self->ob_itself);
107 HLock(self->ob_itself);
108 res = PyString_FromStringAndSize(
109 *self->ob_itself,
110 GetHandleSize(self->ob_itself));
111 HUnlock(self->ob_itself);
112 HSetState(self->ob_itself, state);
113 return res;
115 if (strcmp(name, "__members__") == 0)
116 return Py_BuildValue("[ss]", "data", "size");
119 setattrCode = """
120 static int
121 ResObj_setattr(ResourceObject *self, char *name, PyObject *value)
123 char *data;
124 long size;
126 if (strcmp(name, "data") != 0 || value == NULL )
127 return -1;
128 if ( !PyString_Check(value) )
129 return -1;
130 size = PyString_Size(value);
131 data = PyString_AsString(value);
132 /* XXXX Do I need the GetState/SetState calls? */
133 SetHandleSize(self->ob_itself, size);
134 if ( MemError())
135 return -1;
136 HLock(self->ob_itself);
137 memcpy((char *)*self->ob_itself, data, size);
138 HUnlock(self->ob_itself);
139 /* XXXX Should I do the Changed call immedeately? */
140 return 0;
144 class ResDefinition(GlobalObjectDefinition):
146 def outputCheckNewArg(self):
147 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
149 def outputCheckConvertArg(self):
150 # if it isn't a resource we may be able to coerce it
151 Output("if (!%s_Check(v))", self.prefix)
152 OutLbrace()
153 Output("PyObject *tmp;")
154 Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
155 OutLbrace()
156 Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
157 Output("Py_DECREF(tmp);")
158 Output("return 1;")
159 OutRbrace()
160 Output("PyErr_Clear();")
161 OutRbrace()
163 def outputGetattrHook(self):
164 Output(getattrHookCode)
166 def outputSetattr(self):
167 Output(setattrCode)
169 def outputStructMembers(self):
170 GlobalObjectDefinition.outputStructMembers(self)
171 Output("void (*ob_freeit)(%s ptr);", self.itselftype)
173 def outputInitStructMembers(self):
174 GlobalObjectDefinition.outputInitStructMembers(self)
175 Output("it->ob_freeit = NULL;")
177 def outputCleanupStructMembers(self):
178 Output("if (self->ob_freeit && self->ob_itself)")
179 OutLbrace()
180 Output("self->ob_freeit(self->ob_itself);")
181 OutRbrace()
182 Output("self->ob_itself = NULL;")
185 resobject = ResDefinition('Resource', 'ResObj', 'Handle')
186 module.addobject(resobject)
188 functions = []
189 resmethods = []
191 execfile('resgen.py')
192 execfile('resedit.py')
194 for f in functions: module.add(f)
195 for f in resmethods: resobject.add(f)
197 SetOutputFileName('_Resmodule.c')
198 module.generate()