| 1 | n/a | # This script will generate the Resources interface for Python. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # It execs the file resgen.py which contain the function definitions |
|---|
| 4 | n/a | # (resgen.py was generated by resscan.py, scanning the <Resources.h> header file). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | from macsupport import * |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | class ResMixIn: |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | def checkit(self): |
|---|
| 11 | n/a | if self.returntype.__class__ != OSErrType: |
|---|
| 12 | n/a | OutLbrace() |
|---|
| 13 | n/a | Output("OSErr _err = ResError();") |
|---|
| 14 | n/a | Output("if (_err != noErr) return PyMac_Error(_err);") |
|---|
| 15 | n/a | OutRbrace() |
|---|
| 16 | n/a | FunctionGenerator.checkit(self) # XXX |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | class ResFunction(ResMixIn, OSErrWeakLinkFunctionGenerator): pass |
|---|
| 19 | n/a | class ResMethod(ResMixIn, OSErrWeakLinkMethodGenerator): pass |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | RsrcChainLocation = Type("RsrcChainLocation", "h") |
|---|
| 22 | n/a | FSCatalogInfoBitmap = FakeType("0") # Type("FSCatalogInfoBitmap", "l") |
|---|
| 23 | n/a | FSCatalogInfo_ptr = FakeType("(FSCatalogInfo *)0") |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # includestuff etc. are imported from macsupport |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | includestuff = includestuff + """ |
|---|
| 28 | n/a | #include <Carbon/Carbon.h> |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 31 | n/a | extern PyObject *_ResObj_New(Handle); |
|---|
| 32 | n/a | extern int _ResObj_Convert(PyObject *, Handle *); |
|---|
| 33 | n/a | extern PyObject *_OptResObj_New(Handle); |
|---|
| 34 | n/a | extern int _OptResObj_Convert(PyObject *, Handle *); |
|---|
| 35 | n/a | #define ResObj_New _ResObj_New |
|---|
| 36 | n/a | #define ResObj_Convert _ResObj_Convert |
|---|
| 37 | n/a | #define OptResObj_New _OptResObj_New |
|---|
| 38 | n/a | #define OptResObj_Convert _OptResObj_Convert |
|---|
| 39 | n/a | #endif |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | /* Function to dispose a resource, with a "normal" calling sequence */ |
|---|
| 42 | n/a | static void |
|---|
| 43 | n/a | PyMac_AutoDisposeHandle(Handle h) |
|---|
| 44 | n/a | { |
|---|
| 45 | n/a | DisposeHandle(h); |
|---|
| 46 | n/a | } |
|---|
| 47 | n/a | """ |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | finalstuff = finalstuff + """ |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | /* Alternative version of ResObj_New, which returns None for null argument */ |
|---|
| 52 | n/a | PyObject *OptResObj_New(Handle itself) |
|---|
| 53 | n/a | { |
|---|
| 54 | n/a | if (itself == NULL) { |
|---|
| 55 | n/a | Py_INCREF(Py_None); |
|---|
| 56 | n/a | return Py_None; |
|---|
| 57 | n/a | } |
|---|
| 58 | n/a | return ResObj_New(itself); |
|---|
| 59 | n/a | } |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | int OptResObj_Convert(PyObject *v, Handle *p_itself) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | PyObject *tmp; |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | if ( v == Py_None ) { |
|---|
| 66 | n/a | *p_itself = NULL; |
|---|
| 67 | n/a | return 1; |
|---|
| 68 | n/a | } |
|---|
| 69 | n/a | if (ResObj_Check(v)) |
|---|
| 70 | n/a | { |
|---|
| 71 | n/a | *p_itself = ((ResourceObject *)v)->ob_itself; |
|---|
| 72 | n/a | return 1; |
|---|
| 73 | n/a | } |
|---|
| 74 | n/a | /* If it isn't a resource yet see whether it is convertible */ |
|---|
| 75 | n/a | if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) { |
|---|
| 76 | n/a | *p_itself = ((ResourceObject *)tmp)->ob_itself; |
|---|
| 77 | n/a | Py_DECREF(tmp); |
|---|
| 78 | n/a | return 1; |
|---|
| 79 | n/a | } |
|---|
| 80 | n/a | PyErr_Clear(); |
|---|
| 81 | n/a | PyErr_SetString(PyExc_TypeError, "Resource required"); |
|---|
| 82 | n/a | return 0; |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | """ |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | initstuff = initstuff + """ |
|---|
| 87 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New); |
|---|
| 88 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert); |
|---|
| 89 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New); |
|---|
| 90 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert); |
|---|
| 91 | n/a | """ |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | class ResDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 96 | n/a | getsetlist = [ |
|---|
| 97 | n/a | ('data', |
|---|
| 98 | n/a | """ |
|---|
| 99 | n/a | PyObject *res; |
|---|
| 100 | n/a | char state; |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | state = HGetState(self->ob_itself); |
|---|
| 103 | n/a | HLock(self->ob_itself); |
|---|
| 104 | n/a | res = PyString_FromStringAndSize( |
|---|
| 105 | n/a | *self->ob_itself, |
|---|
| 106 | n/a | GetHandleSize(self->ob_itself)); |
|---|
| 107 | n/a | HUnlock(self->ob_itself); |
|---|
| 108 | n/a | HSetState(self->ob_itself, state); |
|---|
| 109 | n/a | return res; |
|---|
| 110 | n/a | """, |
|---|
| 111 | n/a | """ |
|---|
| 112 | n/a | char *data; |
|---|
| 113 | n/a | long size; |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | if ( v == NULL ) |
|---|
| 116 | n/a | return -1; |
|---|
| 117 | n/a | if ( !PyString_Check(v) ) |
|---|
| 118 | n/a | return -1; |
|---|
| 119 | n/a | size = PyString_Size(v); |
|---|
| 120 | n/a | data = PyString_AsString(v); |
|---|
| 121 | n/a | /* XXXX Do I need the GetState/SetState calls? */ |
|---|
| 122 | n/a | SetHandleSize(self->ob_itself, size); |
|---|
| 123 | n/a | if ( MemError()) |
|---|
| 124 | n/a | return -1; |
|---|
| 125 | n/a | HLock(self->ob_itself); |
|---|
| 126 | n/a | memcpy((char *)*self->ob_itself, data, size); |
|---|
| 127 | n/a | HUnlock(self->ob_itself); |
|---|
| 128 | n/a | /* XXXX Should I do the Changed call immedeately? */ |
|---|
| 129 | n/a | return 0; |
|---|
| 130 | n/a | """, |
|---|
| 131 | n/a | 'The resource data' |
|---|
| 132 | n/a | ), ( |
|---|
| 133 | n/a | 'size', |
|---|
| 134 | n/a | 'return PyInt_FromLong(GetHandleSize(self->ob_itself));', |
|---|
| 135 | n/a | None, |
|---|
| 136 | n/a | 'The length of the resource data' |
|---|
| 137 | n/a | )] |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def outputCheckNewArg(self): |
|---|
| 140 | n/a | Output("if (itself == NULL) return PyMac_Error(resNotFound);") |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | def outputCheckConvertArg(self): |
|---|
| 143 | n/a | # if it isn't a resource we may be able to coerce it |
|---|
| 144 | n/a | Output("if (!%s_Check(v))", self.prefix) |
|---|
| 145 | n/a | OutLbrace() |
|---|
| 146 | n/a | Output("PyObject *tmp;") |
|---|
| 147 | n/a | Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )') |
|---|
| 148 | n/a | OutLbrace() |
|---|
| 149 | n/a | Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;") |
|---|
| 150 | n/a | Output("Py_DECREF(tmp);") |
|---|
| 151 | n/a | Output("return 1;") |
|---|
| 152 | n/a | OutRbrace() |
|---|
| 153 | n/a | Output("PyErr_Clear();") |
|---|
| 154 | n/a | OutRbrace() |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def outputStructMembers(self): |
|---|
| 157 | n/a | GlobalObjectDefinition.outputStructMembers(self) |
|---|
| 158 | n/a | Output("void (*ob_freeit)(%s ptr);", self.itselftype) |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | def outputInitStructMembers(self): |
|---|
| 161 | n/a | GlobalObjectDefinition.outputInitStructMembers(self) |
|---|
| 162 | n/a | Output("it->ob_freeit = NULL;") |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | def outputCleanupStructMembers(self): |
|---|
| 165 | n/a | Output("if (self->ob_freeit && self->ob_itself)") |
|---|
| 166 | n/a | OutLbrace() |
|---|
| 167 | n/a | Output("self->ob_freeit(self->ob_itself);") |
|---|
| 168 | n/a | OutRbrace() |
|---|
| 169 | n/a | Output("self->ob_itself = NULL;") |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | def output_tp_newBody(self): |
|---|
| 172 | n/a | Output("PyObject *self;") |
|---|
| 173 | n/a | Output |
|---|
| 174 | n/a | Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;") |
|---|
| 175 | n/a | Output("((%s *)self)->ob_itself = NULL;", self.objecttype) |
|---|
| 176 | n/a | Output("((%s *)self)->ob_freeit = NULL;", self.objecttype) |
|---|
| 177 | n/a | Output("return self;") |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def output_tp_initBody(self): |
|---|
| 180 | n/a | Output("char *srcdata = NULL;") |
|---|
| 181 | n/a | Output("int srclen = 0;") |
|---|
| 182 | n/a | Output("%s itself;", self.itselftype); |
|---|
| 183 | n/a | Output("char *kw[] = {\"itself\", 0};") |
|---|
| 184 | n/a | Output() |
|---|
| 185 | n/a | Output("if (PyArg_ParseTupleAndKeywords(_args, _kwds, \"O&\", kw, %s_Convert, &itself))", |
|---|
| 186 | n/a | self.prefix); |
|---|
| 187 | n/a | OutLbrace() |
|---|
| 188 | n/a | Output("((%s *)_self)->ob_itself = itself;", self.objecttype) |
|---|
| 189 | n/a | Output("return 0;") |
|---|
| 190 | n/a | OutRbrace() |
|---|
| 191 | n/a | Output("PyErr_Clear();") |
|---|
| 192 | n/a | Output("if (!PyArg_ParseTupleAndKeywords(_args, _kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;") |
|---|
| 193 | n/a | Output("if ((itself = NewHandle(srclen)) == NULL)") |
|---|
| 194 | n/a | OutLbrace() |
|---|
| 195 | n/a | Output("PyErr_NoMemory();") |
|---|
| 196 | n/a | Output("return 0;") |
|---|
| 197 | n/a | OutRbrace() |
|---|
| 198 | n/a | Output("((%s *)_self)->ob_itself = itself;", self.objecttype) |
|---|
| 199 | n/a | # XXXX Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;") |
|---|
| 200 | n/a | Output("if (srclen && srcdata)") |
|---|
| 201 | n/a | OutLbrace() |
|---|
| 202 | n/a | Output("HLock(itself);") |
|---|
| 203 | n/a | Output("memcpy(*itself, srcdata, srclen);") |
|---|
| 204 | n/a | Output("HUnlock(itself);") |
|---|
| 205 | n/a | OutRbrace() |
|---|
| 206 | n/a | Output("return 0;") |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | resobject = ResDefinition('Resource', 'ResObj', 'Handle') |
|---|
| 209 | n/a | module.addobject(resobject) |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | functions = [] |
|---|
| 212 | n/a | resmethods = [] |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | execfile('resgen.py') |
|---|
| 215 | n/a | execfile('resedit.py') |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | for f in functions: module.add(f) |
|---|
| 218 | n/a | for f in resmethods: resobject.add(f) |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | SetOutputFileName('_Resmodule.c') |
|---|
| 221 | n/a | module.generate() |
|---|