Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1 | |
| 2 | /* =========================== Module Res =========================== */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
| 6 | |
| 7 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 8 | #include "macglue.h" |
Jack Jansen | 9d8b96c | 2000-07-14 22:16:45 +0000 | [diff] [blame] | 9 | #include "pymactoolbox.h" |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 11 | #include <Resources.h> |
Jack Jansen | eaf3c9b | 1997-08-15 14:36:45 +0000 | [diff] [blame] | 12 | #include <string.h> |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 13 | |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 14 | /* Function to dispose a resource, with a "normal" calling sequence */ |
| 15 | static void |
| 16 | PyMac_AutoDisposeHandle(Handle h) |
| 17 | { |
| 18 | DisposeHandle(h); |
| 19 | } |
| 20 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 21 | static PyObject *Res_Error; |
| 22 | |
| 23 | /* ---------------------- Object type Resource ---------------------- */ |
| 24 | |
| 25 | PyTypeObject Resource_Type; |
| 26 | |
| 27 | #define ResObj_Check(x) ((x)->ob_type == &Resource_Type) |
| 28 | |
| 29 | typedef struct ResourceObject { |
| 30 | PyObject_HEAD |
| 31 | Handle ob_itself; |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 32 | void (*ob_freeit)(Handle ptr); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 33 | } ResourceObject; |
| 34 | |
| 35 | PyObject *ResObj_New(itself) |
Guido van Rossum | 227a423 | 1995-03-10 14:42:57 +0000 | [diff] [blame] | 36 | Handle itself; |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 37 | { |
| 38 | ResourceObject *it; |
| 39 | if (itself == NULL) return PyMac_Error(resNotFound); |
| 40 | it = PyObject_NEW(ResourceObject, &Resource_Type); |
| 41 | if (it == NULL) return NULL; |
| 42 | it->ob_itself = itself; |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 43 | it->ob_freeit = NULL; |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 44 | return (PyObject *)it; |
| 45 | } |
| 46 | ResObj_Convert(v, p_itself) |
| 47 | PyObject *v; |
| 48 | Handle *p_itself; |
| 49 | { |
| 50 | if (!ResObj_Check(v)) |
| 51 | { |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 52 | PyObject *tmp; |
| 53 | if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) |
| 54 | { |
| 55 | *p_itself = ((ResourceObject *)tmp)->ob_itself; |
| 56 | Py_DECREF(tmp); |
| 57 | return 1; |
| 58 | } |
| 59 | PyErr_Clear(); |
| 60 | } |
| 61 | if (!ResObj_Check(v)) |
| 62 | { |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 63 | PyErr_SetString(PyExc_TypeError, "Resource required"); |
| 64 | return 0; |
| 65 | } |
| 66 | *p_itself = ((ResourceObject *)v)->ob_itself; |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | static void ResObj_dealloc(self) |
| 71 | ResourceObject *self; |
| 72 | { |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 73 | if (self->ob_freeit && self->ob_itself) |
| 74 | { |
| 75 | self->ob_freeit(self->ob_itself); |
| 76 | } |
| 77 | self->ob_itself = NULL; |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 78 | PyMem_DEL(self); |
| 79 | } |
| 80 | |
| 81 | static PyObject *ResObj_HomeResFile(_self, _args) |
| 82 | ResourceObject *_self; |
| 83 | PyObject *_args; |
| 84 | { |
| 85 | PyObject *_res = NULL; |
| 86 | short _rv; |
| 87 | if (!PyArg_ParseTuple(_args, "")) |
| 88 | return NULL; |
| 89 | _rv = HomeResFile(_self->ob_itself); |
| 90 | { |
| 91 | OSErr _err = ResError(); |
| 92 | if (_err != noErr) return PyMac_Error(_err); |
| 93 | } |
| 94 | _res = Py_BuildValue("h", |
| 95 | _rv); |
| 96 | return _res; |
| 97 | } |
| 98 | |
Jack Jansen | 1c4e614 | 1998-04-21 15:23:55 +0000 | [diff] [blame] | 99 | static PyObject *ResObj_MacLoadResource(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 100 | ResourceObject *_self; |
| 101 | PyObject *_args; |
| 102 | { |
| 103 | PyObject *_res = NULL; |
| 104 | if (!PyArg_ParseTuple(_args, "")) |
| 105 | return NULL; |
Jack Jansen | 1c4e614 | 1998-04-21 15:23:55 +0000 | [diff] [blame] | 106 | MacLoadResource(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 107 | { |
| 108 | OSErr _err = ResError(); |
| 109 | if (_err != noErr) return PyMac_Error(_err); |
| 110 | } |
| 111 | Py_INCREF(Py_None); |
| 112 | _res = Py_None; |
| 113 | return _res; |
| 114 | } |
| 115 | |
| 116 | static PyObject *ResObj_ReleaseResource(_self, _args) |
| 117 | ResourceObject *_self; |
| 118 | PyObject *_args; |
| 119 | { |
| 120 | PyObject *_res = NULL; |
| 121 | if (!PyArg_ParseTuple(_args, "")) |
| 122 | return NULL; |
| 123 | ReleaseResource(_self->ob_itself); |
| 124 | { |
| 125 | OSErr _err = ResError(); |
| 126 | if (_err != noErr) return PyMac_Error(_err); |
| 127 | } |
| 128 | Py_INCREF(Py_None); |
| 129 | _res = Py_None; |
| 130 | return _res; |
| 131 | } |
| 132 | |
| 133 | static PyObject *ResObj_DetachResource(_self, _args) |
| 134 | ResourceObject *_self; |
| 135 | PyObject *_args; |
| 136 | { |
| 137 | PyObject *_res = NULL; |
| 138 | if (!PyArg_ParseTuple(_args, "")) |
| 139 | return NULL; |
| 140 | DetachResource(_self->ob_itself); |
| 141 | { |
| 142 | OSErr _err = ResError(); |
| 143 | if (_err != noErr) return PyMac_Error(_err); |
| 144 | } |
| 145 | Py_INCREF(Py_None); |
| 146 | _res = Py_None; |
| 147 | return _res; |
| 148 | } |
| 149 | |
| 150 | static PyObject *ResObj_GetResAttrs(_self, _args) |
| 151 | ResourceObject *_self; |
| 152 | PyObject *_args; |
| 153 | { |
| 154 | PyObject *_res = NULL; |
| 155 | short _rv; |
| 156 | if (!PyArg_ParseTuple(_args, "")) |
| 157 | return NULL; |
| 158 | _rv = GetResAttrs(_self->ob_itself); |
| 159 | { |
| 160 | OSErr _err = ResError(); |
| 161 | if (_err != noErr) return PyMac_Error(_err); |
| 162 | } |
| 163 | _res = Py_BuildValue("h", |
| 164 | _rv); |
| 165 | return _res; |
| 166 | } |
| 167 | |
| 168 | static PyObject *ResObj_GetResInfo(_self, _args) |
| 169 | ResourceObject *_self; |
| 170 | PyObject *_args; |
| 171 | { |
| 172 | PyObject *_res = NULL; |
| 173 | short theID; |
| 174 | ResType theType; |
| 175 | Str255 name; |
| 176 | if (!PyArg_ParseTuple(_args, "")) |
| 177 | return NULL; |
| 178 | GetResInfo(_self->ob_itself, |
| 179 | &theID, |
| 180 | &theType, |
| 181 | name); |
| 182 | { |
| 183 | OSErr _err = ResError(); |
| 184 | if (_err != noErr) return PyMac_Error(_err); |
| 185 | } |
| 186 | _res = Py_BuildValue("hO&O&", |
| 187 | theID, |
| 188 | PyMac_BuildOSType, theType, |
| 189 | PyMac_BuildStr255, name); |
| 190 | return _res; |
| 191 | } |
| 192 | |
| 193 | static PyObject *ResObj_SetResInfo(_self, _args) |
| 194 | ResourceObject *_self; |
| 195 | PyObject *_args; |
| 196 | { |
| 197 | PyObject *_res = NULL; |
| 198 | short theID; |
| 199 | Str255 name; |
| 200 | if (!PyArg_ParseTuple(_args, "hO&", |
| 201 | &theID, |
| 202 | PyMac_GetStr255, name)) |
| 203 | return NULL; |
| 204 | SetResInfo(_self->ob_itself, |
| 205 | theID, |
| 206 | name); |
| 207 | { |
| 208 | OSErr _err = ResError(); |
| 209 | if (_err != noErr) return PyMac_Error(_err); |
| 210 | } |
| 211 | Py_INCREF(Py_None); |
| 212 | _res = Py_None; |
| 213 | return _res; |
| 214 | } |
| 215 | |
| 216 | static PyObject *ResObj_AddResource(_self, _args) |
| 217 | ResourceObject *_self; |
| 218 | PyObject *_args; |
| 219 | { |
| 220 | PyObject *_res = NULL; |
| 221 | ResType theType; |
| 222 | short theID; |
| 223 | Str255 name; |
| 224 | if (!PyArg_ParseTuple(_args, "O&hO&", |
| 225 | PyMac_GetOSType, &theType, |
| 226 | &theID, |
| 227 | PyMac_GetStr255, name)) |
| 228 | return NULL; |
| 229 | AddResource(_self->ob_itself, |
| 230 | theType, |
| 231 | theID, |
| 232 | name); |
| 233 | { |
| 234 | OSErr _err = ResError(); |
| 235 | if (_err != noErr) return PyMac_Error(_err); |
| 236 | } |
| 237 | Py_INCREF(Py_None); |
| 238 | _res = Py_None; |
| 239 | return _res; |
| 240 | } |
| 241 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 242 | static PyObject *ResObj_GetResourceSizeOnDisk(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 243 | ResourceObject *_self; |
| 244 | PyObject *_args; |
| 245 | { |
| 246 | PyObject *_res = NULL; |
| 247 | long _rv; |
| 248 | if (!PyArg_ParseTuple(_args, "")) |
| 249 | return NULL; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 250 | _rv = GetResourceSizeOnDisk(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 251 | { |
| 252 | OSErr _err = ResError(); |
| 253 | if (_err != noErr) return PyMac_Error(_err); |
| 254 | } |
| 255 | _res = Py_BuildValue("l", |
| 256 | _rv); |
| 257 | return _res; |
| 258 | } |
| 259 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 260 | static PyObject *ResObj_GetMaxResourceSize(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 261 | ResourceObject *_self; |
| 262 | PyObject *_args; |
| 263 | { |
| 264 | PyObject *_res = NULL; |
| 265 | long _rv; |
| 266 | if (!PyArg_ParseTuple(_args, "")) |
| 267 | return NULL; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 268 | _rv = GetMaxResourceSize(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 269 | { |
| 270 | OSErr _err = ResError(); |
| 271 | if (_err != noErr) return PyMac_Error(_err); |
| 272 | } |
| 273 | _res = Py_BuildValue("l", |
| 274 | _rv); |
| 275 | return _res; |
| 276 | } |
| 277 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 278 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 280 | static PyObject *ResObj_RsrcMapEntry(_self, _args) |
| 281 | ResourceObject *_self; |
| 282 | PyObject *_args; |
| 283 | { |
| 284 | PyObject *_res = NULL; |
| 285 | long _rv; |
| 286 | if (!PyArg_ParseTuple(_args, "")) |
| 287 | return NULL; |
| 288 | _rv = RsrcMapEntry(_self->ob_itself); |
| 289 | { |
| 290 | OSErr _err = ResError(); |
| 291 | if (_err != noErr) return PyMac_Error(_err); |
| 292 | } |
| 293 | _res = Py_BuildValue("l", |
| 294 | _rv); |
| 295 | return _res; |
| 296 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 297 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 298 | |
| 299 | static PyObject *ResObj_SetResAttrs(_self, _args) |
| 300 | ResourceObject *_self; |
| 301 | PyObject *_args; |
| 302 | { |
| 303 | PyObject *_res = NULL; |
| 304 | short attrs; |
| 305 | if (!PyArg_ParseTuple(_args, "h", |
| 306 | &attrs)) |
| 307 | return NULL; |
| 308 | SetResAttrs(_self->ob_itself, |
| 309 | attrs); |
| 310 | { |
| 311 | OSErr _err = ResError(); |
| 312 | if (_err != noErr) return PyMac_Error(_err); |
| 313 | } |
| 314 | Py_INCREF(Py_None); |
| 315 | _res = Py_None; |
| 316 | return _res; |
| 317 | } |
| 318 | |
| 319 | static PyObject *ResObj_ChangedResource(_self, _args) |
| 320 | ResourceObject *_self; |
| 321 | PyObject *_args; |
| 322 | { |
| 323 | PyObject *_res = NULL; |
| 324 | if (!PyArg_ParseTuple(_args, "")) |
| 325 | return NULL; |
| 326 | ChangedResource(_self->ob_itself); |
| 327 | { |
| 328 | OSErr _err = ResError(); |
| 329 | if (_err != noErr) return PyMac_Error(_err); |
| 330 | } |
| 331 | Py_INCREF(Py_None); |
| 332 | _res = Py_None; |
| 333 | return _res; |
| 334 | } |
| 335 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 336 | static PyObject *ResObj_RemoveResource(_self, _args) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 337 | ResourceObject *_self; |
| 338 | PyObject *_args; |
| 339 | { |
| 340 | PyObject *_res = NULL; |
| 341 | if (!PyArg_ParseTuple(_args, "")) |
| 342 | return NULL; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 343 | RemoveResource(_self->ob_itself); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 344 | { |
| 345 | OSErr _err = ResError(); |
| 346 | if (_err != noErr) return PyMac_Error(_err); |
| 347 | } |
| 348 | Py_INCREF(Py_None); |
| 349 | _res = Py_None; |
| 350 | return _res; |
| 351 | } |
| 352 | |
| 353 | static PyObject *ResObj_WriteResource(_self, _args) |
| 354 | ResourceObject *_self; |
| 355 | PyObject *_args; |
| 356 | { |
| 357 | PyObject *_res = NULL; |
| 358 | if (!PyArg_ParseTuple(_args, "")) |
| 359 | return NULL; |
| 360 | WriteResource(_self->ob_itself); |
| 361 | { |
| 362 | OSErr _err = ResError(); |
| 363 | if (_err != noErr) return PyMac_Error(_err); |
| 364 | } |
| 365 | Py_INCREF(Py_None); |
| 366 | _res = Py_None; |
| 367 | return _res; |
| 368 | } |
| 369 | |
| 370 | static PyObject *ResObj_SetResourceSize(_self, _args) |
| 371 | ResourceObject *_self; |
| 372 | PyObject *_args; |
| 373 | { |
| 374 | PyObject *_res = NULL; |
| 375 | long newSize; |
| 376 | if (!PyArg_ParseTuple(_args, "l", |
| 377 | &newSize)) |
| 378 | return NULL; |
| 379 | SetResourceSize(_self->ob_itself, |
| 380 | newSize); |
| 381 | { |
| 382 | OSErr _err = ResError(); |
| 383 | if (_err != noErr) return PyMac_Error(_err); |
| 384 | } |
| 385 | Py_INCREF(Py_None); |
| 386 | _res = Py_None; |
| 387 | return _res; |
| 388 | } |
| 389 | |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 390 | static PyObject *ResObj_GetNextFOND(_self, _args) |
| 391 | ResourceObject *_self; |
| 392 | PyObject *_args; |
| 393 | { |
| 394 | PyObject *_res = NULL; |
| 395 | Handle _rv; |
| 396 | if (!PyArg_ParseTuple(_args, "")) |
| 397 | return NULL; |
| 398 | _rv = GetNextFOND(_self->ob_itself); |
| 399 | { |
| 400 | OSErr _err = ResError(); |
| 401 | if (_err != noErr) return PyMac_Error(_err); |
| 402 | } |
| 403 | _res = Py_BuildValue("O&", |
| 404 | ResObj_New, _rv); |
| 405 | return _res; |
| 406 | } |
| 407 | |
Jack Jansen | 1e05402 | 1995-06-18 20:20:27 +0000 | [diff] [blame] | 408 | static PyObject *ResObj_as_Control(_self, _args) |
| 409 | ResourceObject *_self; |
| 410 | PyObject *_args; |
| 411 | { |
| 412 | PyObject *_res = NULL; |
| 413 | |
| 414 | return CtlObj_New((ControlHandle)_self->ob_itself); |
| 415 | |
| 416 | } |
| 417 | |
| 418 | static PyObject *ResObj_as_Menu(_self, _args) |
| 419 | ResourceObject *_self; |
| 420 | PyObject *_args; |
| 421 | { |
| 422 | PyObject *_res = NULL; |
| 423 | |
| 424 | return MenuObj_New((MenuHandle)_self->ob_itself); |
| 425 | |
| 426 | } |
| 427 | |
Jack Jansen | e180d99 | 1998-04-24 10:28:20 +0000 | [diff] [blame] | 428 | static PyObject *ResObj_LoadResource(_self, _args) |
| 429 | ResourceObject *_self; |
| 430 | PyObject *_args; |
| 431 | { |
| 432 | PyObject *_res = NULL; |
| 433 | if (!PyArg_ParseTuple(_args, "")) |
| 434 | return NULL; |
| 435 | LoadResource(_self->ob_itself); |
| 436 | { |
| 437 | OSErr _err = ResError(); |
| 438 | if (_err != noErr) return PyMac_Error(_err); |
| 439 | } |
| 440 | Py_INCREF(Py_None); |
| 441 | _res = Py_None; |
| 442 | return _res; |
| 443 | } |
| 444 | |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 445 | static PyObject *ResObj_AutoDispose(_self, _args) |
| 446 | ResourceObject *_self; |
| 447 | PyObject *_args; |
| 448 | { |
| 449 | PyObject *_res = NULL; |
| 450 | |
| 451 | int onoff, old = 0; |
| 452 | if (!PyArg_ParseTuple(_args, "i", &onoff)) |
| 453 | return NULL; |
| 454 | if ( _self->ob_freeit ) |
| 455 | old = 1; |
| 456 | if ( onoff ) |
| 457 | _self->ob_freeit = PyMac_AutoDisposeHandle; |
| 458 | else |
| 459 | _self->ob_freeit = NULL; |
| 460 | return Py_BuildValue("i", old); |
| 461 | |
| 462 | } |
| 463 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 464 | static PyMethodDef ResObj_methods[] = { |
| 465 | {"HomeResFile", (PyCFunction)ResObj_HomeResFile, 1, |
| 466 | "() -> (short _rv)"}, |
Jack Jansen | 1c4e614 | 1998-04-21 15:23:55 +0000 | [diff] [blame] | 467 | {"MacLoadResource", (PyCFunction)ResObj_MacLoadResource, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 468 | "() -> None"}, |
| 469 | {"ReleaseResource", (PyCFunction)ResObj_ReleaseResource, 1, |
| 470 | "() -> None"}, |
| 471 | {"DetachResource", (PyCFunction)ResObj_DetachResource, 1, |
| 472 | "() -> None"}, |
| 473 | {"GetResAttrs", (PyCFunction)ResObj_GetResAttrs, 1, |
| 474 | "() -> (short _rv)"}, |
| 475 | {"GetResInfo", (PyCFunction)ResObj_GetResInfo, 1, |
| 476 | "() -> (short theID, ResType theType, Str255 name)"}, |
| 477 | {"SetResInfo", (PyCFunction)ResObj_SetResInfo, 1, |
| 478 | "(short theID, Str255 name) -> None"}, |
| 479 | {"AddResource", (PyCFunction)ResObj_AddResource, 1, |
| 480 | "(ResType theType, short theID, Str255 name) -> None"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 481 | {"GetResourceSizeOnDisk", (PyCFunction)ResObj_GetResourceSizeOnDisk, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 482 | "() -> (long _rv)"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 483 | {"GetMaxResourceSize", (PyCFunction)ResObj_GetMaxResourceSize, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 484 | "() -> (long _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 485 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 486 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 487 | {"RsrcMapEntry", (PyCFunction)ResObj_RsrcMapEntry, 1, |
| 488 | "() -> (long _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 489 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 490 | {"SetResAttrs", (PyCFunction)ResObj_SetResAttrs, 1, |
| 491 | "(short attrs) -> None"}, |
| 492 | {"ChangedResource", (PyCFunction)ResObj_ChangedResource, 1, |
| 493 | "() -> None"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 494 | {"RemoveResource", (PyCFunction)ResObj_RemoveResource, 1, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 495 | "() -> None"}, |
| 496 | {"WriteResource", (PyCFunction)ResObj_WriteResource, 1, |
| 497 | "() -> None"}, |
| 498 | {"SetResourceSize", (PyCFunction)ResObj_SetResourceSize, 1, |
| 499 | "(long newSize) -> None"}, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 500 | {"GetNextFOND", (PyCFunction)ResObj_GetNextFOND, 1, |
| 501 | "() -> (Handle _rv)"}, |
Jack Jansen | 1e05402 | 1995-06-18 20:20:27 +0000 | [diff] [blame] | 502 | {"as_Control", (PyCFunction)ResObj_as_Control, 1, |
| 503 | "Return this resource/handle as a Control"}, |
| 504 | {"as_Menu", (PyCFunction)ResObj_as_Menu, 1, |
| 505 | "Return this resource/handle as a Menu"}, |
Jack Jansen | e180d99 | 1998-04-24 10:28:20 +0000 | [diff] [blame] | 506 | {"LoadResource", (PyCFunction)ResObj_LoadResource, 1, |
| 507 | "() -> None"}, |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 508 | {"AutoDispose", (PyCFunction)ResObj_AutoDispose, 1, |
| 509 | "(int)->int. Automatically DisposeHandle the object on Python object cleanup"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 510 | {NULL, NULL, 0} |
| 511 | }; |
| 512 | |
| 513 | PyMethodChain ResObj_chain = { ResObj_methods, NULL }; |
| 514 | |
| 515 | static PyObject *ResObj_getattr(self, name) |
| 516 | ResourceObject *self; |
| 517 | char *name; |
| 518 | { |
| 519 | |
| 520 | if (strcmp(name, "size") == 0) |
| 521 | return PyInt_FromLong(GetHandleSize(self->ob_itself)); |
| 522 | if (strcmp(name, "data") == 0) { |
| 523 | PyObject *res; |
| 524 | char state; |
| 525 | state = HGetState(self->ob_itself); |
| 526 | HLock(self->ob_itself); |
| 527 | res = PyString_FromStringAndSize( |
| 528 | *self->ob_itself, |
| 529 | GetHandleSize(self->ob_itself)); |
| 530 | HUnlock(self->ob_itself); |
| 531 | HSetState(self->ob_itself, state); |
| 532 | return res; |
| 533 | } |
| 534 | if (strcmp(name, "__members__") == 0) |
| 535 | return Py_BuildValue("[ss]", "data", "size"); |
| 536 | |
| 537 | return Py_FindMethodInChain(&ResObj_chain, (PyObject *)self, name); |
| 538 | } |
| 539 | |
Jack Jansen | 1e05402 | 1995-06-18 20:20:27 +0000 | [diff] [blame] | 540 | static int |
| 541 | ResObj_setattr(self, name, value) |
| 542 | ResourceObject *self; |
| 543 | char *name; |
| 544 | PyObject *value; |
| 545 | { |
| 546 | char *data; |
| 547 | long size; |
| 548 | |
| 549 | if (strcmp(name, "data") != 0 || value == NULL ) |
| 550 | return -1; |
| 551 | if ( !PyString_Check(value) ) |
| 552 | return -1; |
| 553 | size = PyString_Size(value); |
| 554 | data = PyString_AsString(value); |
| 555 | /* XXXX Do I need the GetState/SetState calls? */ |
| 556 | SetHandleSize(self->ob_itself, size); |
| 557 | if ( MemError()) |
| 558 | return -1; |
| 559 | HLock(self->ob_itself); |
| 560 | memcpy((char *)*self->ob_itself, data, size); |
| 561 | HUnlock(self->ob_itself); |
| 562 | /* XXXX Should I do the Changed call immedeately? */ |
| 563 | return 0; |
| 564 | } |
| 565 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 566 | |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 567 | #define ResObj_compare NULL |
| 568 | |
| 569 | #define ResObj_repr NULL |
| 570 | |
| 571 | #define ResObj_hash NULL |
| 572 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 573 | PyTypeObject Resource_Type = { |
| 574 | PyObject_HEAD_INIT(&PyType_Type) |
| 575 | 0, /*ob_size*/ |
| 576 | "Resource", /*tp_name*/ |
| 577 | sizeof(ResourceObject), /*tp_basicsize*/ |
| 578 | 0, /*tp_itemsize*/ |
| 579 | /* methods */ |
| 580 | (destructor) ResObj_dealloc, /*tp_dealloc*/ |
| 581 | 0, /*tp_print*/ |
| 582 | (getattrfunc) ResObj_getattr, /*tp_getattr*/ |
| 583 | (setattrfunc) ResObj_setattr, /*tp_setattr*/ |
Jack Jansen | a05ac60 | 1999-12-12 21:41:51 +0000 | [diff] [blame] | 584 | (cmpfunc) ResObj_compare, /*tp_compare*/ |
| 585 | (reprfunc) ResObj_repr, /*tp_repr*/ |
| 586 | (PyNumberMethods *)0, /* tp_as_number */ |
| 587 | (PySequenceMethods *)0, /* tp_as_sequence */ |
| 588 | (PyMappingMethods *)0, /* tp_as_mapping */ |
| 589 | (hashfunc) ResObj_hash, /*tp_hash*/ |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | /* -------------------- End object type Resource -------------------- */ |
| 593 | |
| 594 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 595 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 597 | static PyObject *Res_InitResources(_self, _args) |
| 598 | PyObject *_self; |
| 599 | PyObject *_args; |
| 600 | { |
| 601 | PyObject *_res = NULL; |
| 602 | short _rv; |
| 603 | if (!PyArg_ParseTuple(_args, "")) |
| 604 | return NULL; |
| 605 | _rv = InitResources(); |
| 606 | { |
| 607 | OSErr _err = ResError(); |
| 608 | if (_err != noErr) return PyMac_Error(_err); |
| 609 | } |
| 610 | _res = Py_BuildValue("h", |
| 611 | _rv); |
| 612 | return _res; |
| 613 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 614 | #endif |
| 615 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 616 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 617 | |
| 618 | static PyObject *Res_RsrcZoneInit(_self, _args) |
| 619 | PyObject *_self; |
| 620 | PyObject *_args; |
| 621 | { |
| 622 | PyObject *_res = NULL; |
| 623 | if (!PyArg_ParseTuple(_args, "")) |
| 624 | return NULL; |
| 625 | RsrcZoneInit(); |
| 626 | { |
| 627 | OSErr _err = ResError(); |
| 628 | if (_err != noErr) return PyMac_Error(_err); |
| 629 | } |
| 630 | Py_INCREF(Py_None); |
| 631 | _res = Py_None; |
| 632 | return _res; |
| 633 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 634 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 635 | |
| 636 | static PyObject *Res_CloseResFile(_self, _args) |
| 637 | PyObject *_self; |
| 638 | PyObject *_args; |
| 639 | { |
| 640 | PyObject *_res = NULL; |
| 641 | short refNum; |
| 642 | if (!PyArg_ParseTuple(_args, "h", |
| 643 | &refNum)) |
| 644 | return NULL; |
| 645 | CloseResFile(refNum); |
| 646 | { |
| 647 | OSErr _err = ResError(); |
| 648 | if (_err != noErr) return PyMac_Error(_err); |
| 649 | } |
| 650 | Py_INCREF(Py_None); |
| 651 | _res = Py_None; |
| 652 | return _res; |
| 653 | } |
| 654 | |
| 655 | static PyObject *Res_ResError(_self, _args) |
| 656 | PyObject *_self; |
| 657 | PyObject *_args; |
| 658 | { |
| 659 | PyObject *_res = NULL; |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 660 | OSErr _rv; |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 661 | if (!PyArg_ParseTuple(_args, "")) |
| 662 | return NULL; |
| 663 | _rv = ResError(); |
| 664 | { |
| 665 | OSErr _err = ResError(); |
| 666 | if (_err != noErr) return PyMac_Error(_err); |
| 667 | } |
| 668 | _res = Py_BuildValue("h", |
| 669 | _rv); |
| 670 | return _res; |
| 671 | } |
| 672 | |
| 673 | static PyObject *Res_CurResFile(_self, _args) |
| 674 | PyObject *_self; |
| 675 | PyObject *_args; |
| 676 | { |
| 677 | PyObject *_res = NULL; |
| 678 | short _rv; |
| 679 | if (!PyArg_ParseTuple(_args, "")) |
| 680 | return NULL; |
| 681 | _rv = CurResFile(); |
| 682 | { |
| 683 | OSErr _err = ResError(); |
| 684 | if (_err != noErr) return PyMac_Error(_err); |
| 685 | } |
| 686 | _res = Py_BuildValue("h", |
| 687 | _rv); |
| 688 | return _res; |
| 689 | } |
| 690 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 691 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 693 | static PyObject *Res_CreateResFile(_self, _args) |
| 694 | PyObject *_self; |
| 695 | PyObject *_args; |
| 696 | { |
| 697 | PyObject *_res = NULL; |
| 698 | Str255 fileName; |
| 699 | if (!PyArg_ParseTuple(_args, "O&", |
| 700 | PyMac_GetStr255, fileName)) |
| 701 | return NULL; |
| 702 | CreateResFile(fileName); |
| 703 | { |
| 704 | OSErr _err = ResError(); |
| 705 | if (_err != noErr) return PyMac_Error(_err); |
| 706 | } |
| 707 | Py_INCREF(Py_None); |
| 708 | _res = Py_None; |
| 709 | return _res; |
| 710 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 711 | #endif |
| 712 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 713 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 714 | |
| 715 | static PyObject *Res_OpenResFile(_self, _args) |
| 716 | PyObject *_self; |
| 717 | PyObject *_args; |
| 718 | { |
| 719 | PyObject *_res = NULL; |
| 720 | short _rv; |
| 721 | Str255 fileName; |
| 722 | if (!PyArg_ParseTuple(_args, "O&", |
| 723 | PyMac_GetStr255, fileName)) |
| 724 | return NULL; |
| 725 | _rv = OpenResFile(fileName); |
| 726 | { |
| 727 | OSErr _err = ResError(); |
| 728 | if (_err != noErr) return PyMac_Error(_err); |
| 729 | } |
| 730 | _res = Py_BuildValue("h", |
| 731 | _rv); |
| 732 | return _res; |
| 733 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 734 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 735 | |
| 736 | static PyObject *Res_UseResFile(_self, _args) |
| 737 | PyObject *_self; |
| 738 | PyObject *_args; |
| 739 | { |
| 740 | PyObject *_res = NULL; |
| 741 | short refNum; |
| 742 | if (!PyArg_ParseTuple(_args, "h", |
| 743 | &refNum)) |
| 744 | return NULL; |
| 745 | UseResFile(refNum); |
| 746 | { |
| 747 | OSErr _err = ResError(); |
| 748 | if (_err != noErr) return PyMac_Error(_err); |
| 749 | } |
| 750 | Py_INCREF(Py_None); |
| 751 | _res = Py_None; |
| 752 | return _res; |
| 753 | } |
| 754 | |
| 755 | static PyObject *Res_CountTypes(_self, _args) |
| 756 | PyObject *_self; |
| 757 | PyObject *_args; |
| 758 | { |
| 759 | PyObject *_res = NULL; |
| 760 | short _rv; |
| 761 | if (!PyArg_ParseTuple(_args, "")) |
| 762 | return NULL; |
| 763 | _rv = CountTypes(); |
| 764 | { |
| 765 | OSErr _err = ResError(); |
| 766 | if (_err != noErr) return PyMac_Error(_err); |
| 767 | } |
| 768 | _res = Py_BuildValue("h", |
| 769 | _rv); |
| 770 | return _res; |
| 771 | } |
| 772 | |
| 773 | static PyObject *Res_Count1Types(_self, _args) |
| 774 | PyObject *_self; |
| 775 | PyObject *_args; |
| 776 | { |
| 777 | PyObject *_res = NULL; |
| 778 | short _rv; |
| 779 | if (!PyArg_ParseTuple(_args, "")) |
| 780 | return NULL; |
| 781 | _rv = Count1Types(); |
| 782 | { |
| 783 | OSErr _err = ResError(); |
| 784 | if (_err != noErr) return PyMac_Error(_err); |
| 785 | } |
| 786 | _res = Py_BuildValue("h", |
| 787 | _rv); |
| 788 | return _res; |
| 789 | } |
| 790 | |
| 791 | static PyObject *Res_GetIndType(_self, _args) |
| 792 | PyObject *_self; |
| 793 | PyObject *_args; |
| 794 | { |
| 795 | PyObject *_res = NULL; |
| 796 | ResType theType; |
| 797 | short index; |
| 798 | if (!PyArg_ParseTuple(_args, "h", |
| 799 | &index)) |
| 800 | return NULL; |
| 801 | GetIndType(&theType, |
| 802 | index); |
| 803 | { |
| 804 | OSErr _err = ResError(); |
| 805 | if (_err != noErr) return PyMac_Error(_err); |
| 806 | } |
| 807 | _res = Py_BuildValue("O&", |
| 808 | PyMac_BuildOSType, theType); |
| 809 | return _res; |
| 810 | } |
| 811 | |
| 812 | static PyObject *Res_Get1IndType(_self, _args) |
| 813 | PyObject *_self; |
| 814 | PyObject *_args; |
| 815 | { |
| 816 | PyObject *_res = NULL; |
| 817 | ResType theType; |
| 818 | short index; |
| 819 | if (!PyArg_ParseTuple(_args, "h", |
| 820 | &index)) |
| 821 | return NULL; |
| 822 | Get1IndType(&theType, |
| 823 | index); |
| 824 | { |
| 825 | OSErr _err = ResError(); |
| 826 | if (_err != noErr) return PyMac_Error(_err); |
| 827 | } |
| 828 | _res = Py_BuildValue("O&", |
| 829 | PyMac_BuildOSType, theType); |
| 830 | return _res; |
| 831 | } |
| 832 | |
| 833 | static PyObject *Res_SetResLoad(_self, _args) |
| 834 | PyObject *_self; |
| 835 | PyObject *_args; |
| 836 | { |
| 837 | PyObject *_res = NULL; |
| 838 | Boolean load; |
| 839 | if (!PyArg_ParseTuple(_args, "b", |
| 840 | &load)) |
| 841 | return NULL; |
| 842 | SetResLoad(load); |
| 843 | { |
| 844 | OSErr _err = ResError(); |
| 845 | if (_err != noErr) return PyMac_Error(_err); |
| 846 | } |
| 847 | Py_INCREF(Py_None); |
| 848 | _res = Py_None; |
| 849 | return _res; |
| 850 | } |
| 851 | |
| 852 | static PyObject *Res_CountResources(_self, _args) |
| 853 | PyObject *_self; |
| 854 | PyObject *_args; |
| 855 | { |
| 856 | PyObject *_res = NULL; |
| 857 | short _rv; |
| 858 | ResType theType; |
| 859 | if (!PyArg_ParseTuple(_args, "O&", |
| 860 | PyMac_GetOSType, &theType)) |
| 861 | return NULL; |
| 862 | _rv = CountResources(theType); |
| 863 | { |
| 864 | OSErr _err = ResError(); |
| 865 | if (_err != noErr) return PyMac_Error(_err); |
| 866 | } |
| 867 | _res = Py_BuildValue("h", |
| 868 | _rv); |
| 869 | return _res; |
| 870 | } |
| 871 | |
| 872 | static PyObject *Res_Count1Resources(_self, _args) |
| 873 | PyObject *_self; |
| 874 | PyObject *_args; |
| 875 | { |
| 876 | PyObject *_res = NULL; |
| 877 | short _rv; |
| 878 | ResType theType; |
| 879 | if (!PyArg_ParseTuple(_args, "O&", |
| 880 | PyMac_GetOSType, &theType)) |
| 881 | return NULL; |
| 882 | _rv = Count1Resources(theType); |
| 883 | { |
| 884 | OSErr _err = ResError(); |
| 885 | if (_err != noErr) return PyMac_Error(_err); |
| 886 | } |
| 887 | _res = Py_BuildValue("h", |
| 888 | _rv); |
| 889 | return _res; |
| 890 | } |
| 891 | |
| 892 | static PyObject *Res_GetIndResource(_self, _args) |
| 893 | PyObject *_self; |
| 894 | PyObject *_args; |
| 895 | { |
| 896 | PyObject *_res = NULL; |
| 897 | Handle _rv; |
| 898 | ResType theType; |
| 899 | short index; |
| 900 | if (!PyArg_ParseTuple(_args, "O&h", |
| 901 | PyMac_GetOSType, &theType, |
| 902 | &index)) |
| 903 | return NULL; |
| 904 | _rv = GetIndResource(theType, |
| 905 | index); |
| 906 | { |
| 907 | OSErr _err = ResError(); |
| 908 | if (_err != noErr) return PyMac_Error(_err); |
| 909 | } |
| 910 | _res = Py_BuildValue("O&", |
| 911 | ResObj_New, _rv); |
| 912 | return _res; |
| 913 | } |
| 914 | |
| 915 | static PyObject *Res_Get1IndResource(_self, _args) |
| 916 | PyObject *_self; |
| 917 | PyObject *_args; |
| 918 | { |
| 919 | PyObject *_res = NULL; |
| 920 | Handle _rv; |
| 921 | ResType theType; |
| 922 | short index; |
| 923 | if (!PyArg_ParseTuple(_args, "O&h", |
| 924 | PyMac_GetOSType, &theType, |
| 925 | &index)) |
| 926 | return NULL; |
| 927 | _rv = Get1IndResource(theType, |
| 928 | index); |
| 929 | { |
| 930 | OSErr _err = ResError(); |
| 931 | if (_err != noErr) return PyMac_Error(_err); |
| 932 | } |
| 933 | _res = Py_BuildValue("O&", |
| 934 | ResObj_New, _rv); |
| 935 | return _res; |
| 936 | } |
| 937 | |
| 938 | static PyObject *Res_GetResource(_self, _args) |
| 939 | PyObject *_self; |
| 940 | PyObject *_args; |
| 941 | { |
| 942 | PyObject *_res = NULL; |
| 943 | Handle _rv; |
| 944 | ResType theType; |
| 945 | short theID; |
| 946 | if (!PyArg_ParseTuple(_args, "O&h", |
| 947 | PyMac_GetOSType, &theType, |
| 948 | &theID)) |
| 949 | return NULL; |
| 950 | _rv = GetResource(theType, |
| 951 | theID); |
| 952 | { |
| 953 | OSErr _err = ResError(); |
| 954 | if (_err != noErr) return PyMac_Error(_err); |
| 955 | } |
| 956 | _res = Py_BuildValue("O&", |
| 957 | ResObj_New, _rv); |
| 958 | return _res; |
| 959 | } |
| 960 | |
| 961 | static PyObject *Res_Get1Resource(_self, _args) |
| 962 | PyObject *_self; |
| 963 | PyObject *_args; |
| 964 | { |
| 965 | PyObject *_res = NULL; |
| 966 | Handle _rv; |
| 967 | ResType theType; |
| 968 | short theID; |
| 969 | if (!PyArg_ParseTuple(_args, "O&h", |
| 970 | PyMac_GetOSType, &theType, |
| 971 | &theID)) |
| 972 | return NULL; |
| 973 | _rv = Get1Resource(theType, |
| 974 | theID); |
| 975 | { |
| 976 | OSErr _err = ResError(); |
| 977 | if (_err != noErr) return PyMac_Error(_err); |
| 978 | } |
| 979 | _res = Py_BuildValue("O&", |
| 980 | ResObj_New, _rv); |
| 981 | return _res; |
| 982 | } |
| 983 | |
| 984 | static PyObject *Res_GetNamedResource(_self, _args) |
| 985 | PyObject *_self; |
| 986 | PyObject *_args; |
| 987 | { |
| 988 | PyObject *_res = NULL; |
| 989 | Handle _rv; |
| 990 | ResType theType; |
| 991 | Str255 name; |
| 992 | if (!PyArg_ParseTuple(_args, "O&O&", |
| 993 | PyMac_GetOSType, &theType, |
| 994 | PyMac_GetStr255, name)) |
| 995 | return NULL; |
| 996 | _rv = GetNamedResource(theType, |
| 997 | name); |
| 998 | { |
| 999 | OSErr _err = ResError(); |
| 1000 | if (_err != noErr) return PyMac_Error(_err); |
| 1001 | } |
| 1002 | _res = Py_BuildValue("O&", |
| 1003 | ResObj_New, _rv); |
| 1004 | return _res; |
| 1005 | } |
| 1006 | |
| 1007 | static PyObject *Res_Get1NamedResource(_self, _args) |
| 1008 | PyObject *_self; |
| 1009 | PyObject *_args; |
| 1010 | { |
| 1011 | PyObject *_res = NULL; |
| 1012 | Handle _rv; |
| 1013 | ResType theType; |
| 1014 | Str255 name; |
| 1015 | if (!PyArg_ParseTuple(_args, "O&O&", |
| 1016 | PyMac_GetOSType, &theType, |
| 1017 | PyMac_GetStr255, name)) |
| 1018 | return NULL; |
| 1019 | _rv = Get1NamedResource(theType, |
| 1020 | name); |
| 1021 | { |
| 1022 | OSErr _err = ResError(); |
| 1023 | if (_err != noErr) return PyMac_Error(_err); |
| 1024 | } |
| 1025 | _res = Py_BuildValue("O&", |
| 1026 | ResObj_New, _rv); |
| 1027 | return _res; |
| 1028 | } |
| 1029 | |
| 1030 | static PyObject *Res_UniqueID(_self, _args) |
| 1031 | PyObject *_self; |
| 1032 | PyObject *_args; |
| 1033 | { |
| 1034 | PyObject *_res = NULL; |
| 1035 | short _rv; |
| 1036 | ResType theType; |
| 1037 | if (!PyArg_ParseTuple(_args, "O&", |
| 1038 | PyMac_GetOSType, &theType)) |
| 1039 | return NULL; |
| 1040 | _rv = UniqueID(theType); |
| 1041 | { |
| 1042 | OSErr _err = ResError(); |
| 1043 | if (_err != noErr) return PyMac_Error(_err); |
| 1044 | } |
| 1045 | _res = Py_BuildValue("h", |
| 1046 | _rv); |
| 1047 | return _res; |
| 1048 | } |
| 1049 | |
| 1050 | static PyObject *Res_Unique1ID(_self, _args) |
| 1051 | PyObject *_self; |
| 1052 | PyObject *_args; |
| 1053 | { |
| 1054 | PyObject *_res = NULL; |
| 1055 | short _rv; |
| 1056 | ResType theType; |
| 1057 | if (!PyArg_ParseTuple(_args, "O&", |
| 1058 | PyMac_GetOSType, &theType)) |
| 1059 | return NULL; |
| 1060 | _rv = Unique1ID(theType); |
| 1061 | { |
| 1062 | OSErr _err = ResError(); |
| 1063 | if (_err != noErr) return PyMac_Error(_err); |
| 1064 | } |
| 1065 | _res = Py_BuildValue("h", |
| 1066 | _rv); |
| 1067 | return _res; |
| 1068 | } |
| 1069 | |
| 1070 | static PyObject *Res_UpdateResFile(_self, _args) |
| 1071 | PyObject *_self; |
| 1072 | PyObject *_args; |
| 1073 | { |
| 1074 | PyObject *_res = NULL; |
| 1075 | short refNum; |
| 1076 | if (!PyArg_ParseTuple(_args, "h", |
| 1077 | &refNum)) |
| 1078 | return NULL; |
| 1079 | UpdateResFile(refNum); |
| 1080 | { |
| 1081 | OSErr _err = ResError(); |
| 1082 | if (_err != noErr) return PyMac_Error(_err); |
| 1083 | } |
| 1084 | Py_INCREF(Py_None); |
| 1085 | _res = Py_None; |
| 1086 | return _res; |
| 1087 | } |
| 1088 | |
| 1089 | static PyObject *Res_SetResPurge(_self, _args) |
| 1090 | PyObject *_self; |
| 1091 | PyObject *_args; |
| 1092 | { |
| 1093 | PyObject *_res = NULL; |
| 1094 | Boolean install; |
| 1095 | if (!PyArg_ParseTuple(_args, "b", |
| 1096 | &install)) |
| 1097 | return NULL; |
| 1098 | SetResPurge(install); |
| 1099 | { |
| 1100 | OSErr _err = ResError(); |
| 1101 | if (_err != noErr) return PyMac_Error(_err); |
| 1102 | } |
| 1103 | Py_INCREF(Py_None); |
| 1104 | _res = Py_None; |
| 1105 | return _res; |
| 1106 | } |
| 1107 | |
| 1108 | static PyObject *Res_GetResFileAttrs(_self, _args) |
| 1109 | PyObject *_self; |
| 1110 | PyObject *_args; |
| 1111 | { |
| 1112 | PyObject *_res = NULL; |
| 1113 | short _rv; |
| 1114 | short refNum; |
| 1115 | if (!PyArg_ParseTuple(_args, "h", |
| 1116 | &refNum)) |
| 1117 | return NULL; |
| 1118 | _rv = GetResFileAttrs(refNum); |
| 1119 | { |
| 1120 | OSErr _err = ResError(); |
| 1121 | if (_err != noErr) return PyMac_Error(_err); |
| 1122 | } |
| 1123 | _res = Py_BuildValue("h", |
| 1124 | _rv); |
| 1125 | return _res; |
| 1126 | } |
| 1127 | |
| 1128 | static PyObject *Res_SetResFileAttrs(_self, _args) |
| 1129 | PyObject *_self; |
| 1130 | PyObject *_args; |
| 1131 | { |
| 1132 | PyObject *_res = NULL; |
| 1133 | short refNum; |
| 1134 | short attrs; |
| 1135 | if (!PyArg_ParseTuple(_args, "hh", |
| 1136 | &refNum, |
| 1137 | &attrs)) |
| 1138 | return NULL; |
| 1139 | SetResFileAttrs(refNum, |
| 1140 | attrs); |
| 1141 | { |
| 1142 | OSErr _err = ResError(); |
| 1143 | if (_err != noErr) return PyMac_Error(_err); |
| 1144 | } |
| 1145 | Py_INCREF(Py_None); |
| 1146 | _res = Py_None; |
| 1147 | return _res; |
| 1148 | } |
| 1149 | |
| 1150 | static PyObject *Res_OpenRFPerm(_self, _args) |
| 1151 | PyObject *_self; |
| 1152 | PyObject *_args; |
| 1153 | { |
| 1154 | PyObject *_res = NULL; |
| 1155 | short _rv; |
| 1156 | Str255 fileName; |
| 1157 | short vRefNum; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1158 | SignedByte permission; |
| 1159 | if (!PyArg_ParseTuple(_args, "O&hb", |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1160 | PyMac_GetStr255, fileName, |
| 1161 | &vRefNum, |
| 1162 | &permission)) |
| 1163 | return NULL; |
| 1164 | _rv = OpenRFPerm(fileName, |
| 1165 | vRefNum, |
| 1166 | permission); |
| 1167 | { |
| 1168 | OSErr _err = ResError(); |
| 1169 | if (_err != noErr) return PyMac_Error(_err); |
| 1170 | } |
| 1171 | _res = Py_BuildValue("h", |
| 1172 | _rv); |
| 1173 | return _res; |
| 1174 | } |
| 1175 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 1176 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1177 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1178 | static PyObject *Res_RGetResource(_self, _args) |
| 1179 | PyObject *_self; |
| 1180 | PyObject *_args; |
| 1181 | { |
| 1182 | PyObject *_res = NULL; |
| 1183 | Handle _rv; |
| 1184 | ResType theType; |
| 1185 | short theID; |
| 1186 | if (!PyArg_ParseTuple(_args, "O&h", |
| 1187 | PyMac_GetOSType, &theType, |
| 1188 | &theID)) |
| 1189 | return NULL; |
| 1190 | _rv = RGetResource(theType, |
| 1191 | theID); |
| 1192 | { |
| 1193 | OSErr _err = ResError(); |
| 1194 | if (_err != noErr) return PyMac_Error(_err); |
| 1195 | } |
| 1196 | _res = Py_BuildValue("O&", |
| 1197 | ResObj_New, _rv); |
| 1198 | return _res; |
| 1199 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1200 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1201 | |
| 1202 | static PyObject *Res_HOpenResFile(_self, _args) |
| 1203 | PyObject *_self; |
| 1204 | PyObject *_args; |
| 1205 | { |
| 1206 | PyObject *_res = NULL; |
| 1207 | short _rv; |
| 1208 | short vRefNum; |
| 1209 | long dirID; |
| 1210 | Str255 fileName; |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1211 | SignedByte permission; |
| 1212 | if (!PyArg_ParseTuple(_args, "hlO&b", |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1213 | &vRefNum, |
| 1214 | &dirID, |
| 1215 | PyMac_GetStr255, fileName, |
| 1216 | &permission)) |
| 1217 | return NULL; |
| 1218 | _rv = HOpenResFile(vRefNum, |
| 1219 | dirID, |
| 1220 | fileName, |
| 1221 | permission); |
| 1222 | { |
| 1223 | OSErr _err = ResError(); |
| 1224 | if (_err != noErr) return PyMac_Error(_err); |
| 1225 | } |
| 1226 | _res = Py_BuildValue("h", |
| 1227 | _rv); |
| 1228 | return _res; |
| 1229 | } |
| 1230 | |
| 1231 | static PyObject *Res_HCreateResFile(_self, _args) |
| 1232 | PyObject *_self; |
| 1233 | PyObject *_args; |
| 1234 | { |
| 1235 | PyObject *_res = NULL; |
| 1236 | short vRefNum; |
| 1237 | long dirID; |
| 1238 | Str255 fileName; |
| 1239 | if (!PyArg_ParseTuple(_args, "hlO&", |
| 1240 | &vRefNum, |
| 1241 | &dirID, |
| 1242 | PyMac_GetStr255, fileName)) |
| 1243 | return NULL; |
| 1244 | HCreateResFile(vRefNum, |
| 1245 | dirID, |
| 1246 | fileName); |
| 1247 | { |
| 1248 | OSErr _err = ResError(); |
| 1249 | if (_err != noErr) return PyMac_Error(_err); |
| 1250 | } |
| 1251 | Py_INCREF(Py_None); |
| 1252 | _res = Py_None; |
| 1253 | return _res; |
| 1254 | } |
| 1255 | |
| 1256 | static PyObject *Res_FSpOpenResFile(_self, _args) |
| 1257 | PyObject *_self; |
| 1258 | PyObject *_args; |
| 1259 | { |
| 1260 | PyObject *_res = NULL; |
| 1261 | short _rv; |
| 1262 | FSSpec spec; |
| 1263 | SignedByte permission; |
| 1264 | if (!PyArg_ParseTuple(_args, "O&b", |
| 1265 | PyMac_GetFSSpec, &spec, |
| 1266 | &permission)) |
| 1267 | return NULL; |
| 1268 | _rv = FSpOpenResFile(&spec, |
| 1269 | permission); |
| 1270 | { |
| 1271 | OSErr _err = ResError(); |
| 1272 | if (_err != noErr) return PyMac_Error(_err); |
| 1273 | } |
| 1274 | _res = Py_BuildValue("h", |
| 1275 | _rv); |
| 1276 | return _res; |
| 1277 | } |
| 1278 | |
| 1279 | static PyObject *Res_FSpCreateResFile(_self, _args) |
| 1280 | PyObject *_self; |
| 1281 | PyObject *_args; |
| 1282 | { |
| 1283 | PyObject *_res = NULL; |
| 1284 | FSSpec spec; |
| 1285 | OSType creator; |
| 1286 | OSType fileType; |
| 1287 | ScriptCode scriptTag; |
| 1288 | if (!PyArg_ParseTuple(_args, "O&O&O&h", |
| 1289 | PyMac_GetFSSpec, &spec, |
| 1290 | PyMac_GetOSType, &creator, |
| 1291 | PyMac_GetOSType, &fileType, |
| 1292 | &scriptTag)) |
| 1293 | return NULL; |
| 1294 | FSpCreateResFile(&spec, |
| 1295 | creator, |
| 1296 | fileType, |
| 1297 | scriptTag); |
| 1298 | { |
| 1299 | OSErr _err = ResError(); |
| 1300 | if (_err != noErr) return PyMac_Error(_err); |
| 1301 | } |
| 1302 | Py_INCREF(Py_None); |
| 1303 | _res = Py_None; |
| 1304 | return _res; |
| 1305 | } |
| 1306 | |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 1307 | static PyObject *Res_Resource(_self, _args) |
| 1308 | PyObject *_self; |
| 1309 | PyObject *_args; |
| 1310 | { |
| 1311 | PyObject *_res = NULL; |
| 1312 | |
| 1313 | char *buf; |
| 1314 | int len; |
| 1315 | Handle h; |
| 1316 | |
| 1317 | if (!PyArg_ParseTuple(_args, "s#", &buf, &len)) |
| 1318 | return NULL; |
| 1319 | h = NewHandle(len); |
| 1320 | if ( h == NULL ) { |
| 1321 | PyErr_NoMemory(); |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | HLock(h); |
| 1325 | memcpy(*h, buf, len); |
| 1326 | HUnlock(h); |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 1327 | return ResObj_New(h); |
| 1328 | |
| 1329 | } |
| 1330 | |
| 1331 | static PyObject *Res_Handle(_self, _args) |
| 1332 | PyObject *_self; |
| 1333 | PyObject *_args; |
| 1334 | { |
| 1335 | PyObject *_res = NULL; |
| 1336 | |
| 1337 | char *buf; |
| 1338 | int len; |
| 1339 | Handle h; |
| 1340 | ResourceObject *rv; |
| 1341 | |
| 1342 | if (!PyArg_ParseTuple(_args, "s#", &buf, &len)) |
| 1343 | return NULL; |
| 1344 | h = NewHandle(len); |
| 1345 | if ( h == NULL ) { |
| 1346 | PyErr_NoMemory(); |
| 1347 | return NULL; |
| 1348 | } |
| 1349 | HLock(h); |
| 1350 | memcpy(*h, buf, len); |
| 1351 | HUnlock(h); |
| 1352 | rv = (ResourceObject *)ResObj_New(h); |
| 1353 | rv->ob_freeit = PyMac_AutoDisposeHandle; |
| 1354 | return (PyObject *)rv; |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 1355 | |
| 1356 | } |
| 1357 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1358 | static PyMethodDef Res_methods[] = { |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1359 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 1360 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1361 | {"InitResources", (PyCFunction)Res_InitResources, 1, |
| 1362 | "() -> (short _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1363 | #endif |
| 1364 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 1365 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1366 | {"RsrcZoneInit", (PyCFunction)Res_RsrcZoneInit, 1, |
| 1367 | "() -> None"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1368 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1369 | {"CloseResFile", (PyCFunction)Res_CloseResFile, 1, |
| 1370 | "(short refNum) -> None"}, |
| 1371 | {"ResError", (PyCFunction)Res_ResError, 1, |
Jack Jansen | 21f9687 | 1998-02-20 16:02:09 +0000 | [diff] [blame] | 1372 | "() -> (OSErr _rv)"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1373 | {"CurResFile", (PyCFunction)Res_CurResFile, 1, |
| 1374 | "() -> (short _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1375 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 1376 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1377 | {"CreateResFile", (PyCFunction)Res_CreateResFile, 1, |
| 1378 | "(Str255 fileName) -> None"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1379 | #endif |
| 1380 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 1381 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1382 | {"OpenResFile", (PyCFunction)Res_OpenResFile, 1, |
| 1383 | "(Str255 fileName) -> (short _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1384 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1385 | {"UseResFile", (PyCFunction)Res_UseResFile, 1, |
| 1386 | "(short refNum) -> None"}, |
| 1387 | {"CountTypes", (PyCFunction)Res_CountTypes, 1, |
| 1388 | "() -> (short _rv)"}, |
| 1389 | {"Count1Types", (PyCFunction)Res_Count1Types, 1, |
| 1390 | "() -> (short _rv)"}, |
| 1391 | {"GetIndType", (PyCFunction)Res_GetIndType, 1, |
| 1392 | "(short index) -> (ResType theType)"}, |
| 1393 | {"Get1IndType", (PyCFunction)Res_Get1IndType, 1, |
| 1394 | "(short index) -> (ResType theType)"}, |
| 1395 | {"SetResLoad", (PyCFunction)Res_SetResLoad, 1, |
| 1396 | "(Boolean load) -> None"}, |
| 1397 | {"CountResources", (PyCFunction)Res_CountResources, 1, |
| 1398 | "(ResType theType) -> (short _rv)"}, |
| 1399 | {"Count1Resources", (PyCFunction)Res_Count1Resources, 1, |
| 1400 | "(ResType theType) -> (short _rv)"}, |
| 1401 | {"GetIndResource", (PyCFunction)Res_GetIndResource, 1, |
| 1402 | "(ResType theType, short index) -> (Handle _rv)"}, |
| 1403 | {"Get1IndResource", (PyCFunction)Res_Get1IndResource, 1, |
| 1404 | "(ResType theType, short index) -> (Handle _rv)"}, |
| 1405 | {"GetResource", (PyCFunction)Res_GetResource, 1, |
| 1406 | "(ResType theType, short theID) -> (Handle _rv)"}, |
| 1407 | {"Get1Resource", (PyCFunction)Res_Get1Resource, 1, |
| 1408 | "(ResType theType, short theID) -> (Handle _rv)"}, |
| 1409 | {"GetNamedResource", (PyCFunction)Res_GetNamedResource, 1, |
| 1410 | "(ResType theType, Str255 name) -> (Handle _rv)"}, |
| 1411 | {"Get1NamedResource", (PyCFunction)Res_Get1NamedResource, 1, |
| 1412 | "(ResType theType, Str255 name) -> (Handle _rv)"}, |
| 1413 | {"UniqueID", (PyCFunction)Res_UniqueID, 1, |
| 1414 | "(ResType theType) -> (short _rv)"}, |
| 1415 | {"Unique1ID", (PyCFunction)Res_Unique1ID, 1, |
| 1416 | "(ResType theType) -> (short _rv)"}, |
| 1417 | {"UpdateResFile", (PyCFunction)Res_UpdateResFile, 1, |
| 1418 | "(short refNum) -> None"}, |
| 1419 | {"SetResPurge", (PyCFunction)Res_SetResPurge, 1, |
| 1420 | "(Boolean install) -> None"}, |
| 1421 | {"GetResFileAttrs", (PyCFunction)Res_GetResFileAttrs, 1, |
| 1422 | "(short refNum) -> (short _rv)"}, |
| 1423 | {"SetResFileAttrs", (PyCFunction)Res_SetResFileAttrs, 1, |
| 1424 | "(short refNum, short attrs) -> None"}, |
| 1425 | {"OpenRFPerm", (PyCFunction)Res_OpenRFPerm, 1, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1426 | "(Str255 fileName, short vRefNum, SignedByte permission) -> (short _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1427 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame^] | 1428 | #if !TARGET_API_MAC_CARBON |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1429 | {"RGetResource", (PyCFunction)Res_RGetResource, 1, |
| 1430 | "(ResType theType, short theID) -> (Handle _rv)"}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1431 | #endif |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1432 | {"HOpenResFile", (PyCFunction)Res_HOpenResFile, 1, |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 1433 | "(short vRefNum, long dirID, Str255 fileName, SignedByte permission) -> (short _rv)"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1434 | {"HCreateResFile", (PyCFunction)Res_HCreateResFile, 1, |
| 1435 | "(short vRefNum, long dirID, Str255 fileName) -> None"}, |
| 1436 | {"FSpOpenResFile", (PyCFunction)Res_FSpOpenResFile, 1, |
| 1437 | "(FSSpec spec, SignedByte permission) -> (short _rv)"}, |
| 1438 | {"FSpCreateResFile", (PyCFunction)Res_FSpCreateResFile, 1, |
| 1439 | "(FSSpec spec, OSType creator, OSType fileType, ScriptCode scriptTag) -> None"}, |
Guido van Rossum | 9bcb641 | 1995-02-05 16:54:27 +0000 | [diff] [blame] | 1440 | {"Resource", (PyCFunction)Res_Resource, 1, |
Jack Jansen | add03b6 | 2000-03-08 16:58:15 +0000 | [diff] [blame] | 1441 | "Convert a string to a resource object.\n\nThe created resource object is actually just a handle,\napply AddResource() to write it to a resource file.\nSee also the Handle() docstring.\n"}, |
| 1442 | {"Handle", (PyCFunction)Res_Handle, 1, |
| 1443 | "Convert a string to a Handle object.\n\nResource() and Handle() are very similar, but objects created with Handle() are\nby default automatically DisposeHandle()d upon object cleanup. Use AutoDispose()\nto change this.\n"}, |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1444 | {NULL, NULL, 0} |
| 1445 | }; |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1450 | /* Alternative version of ResObj_New, which returns None for null argument */ |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1451 | PyObject *OptResObj_New(itself) |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1452 | Handle itself; |
| 1453 | { |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1454 | if (itself == NULL) { |
| 1455 | Py_INCREF(Py_None); |
| 1456 | return Py_None; |
| 1457 | } |
| 1458 | return ResObj_New(itself); |
| 1459 | } |
| 1460 | |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1461 | OptResObj_Convert(v, p_itself) |
| 1462 | PyObject *v; |
| 1463 | Handle *p_itself; |
| 1464 | { |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1465 | PyObject *tmp; |
| 1466 | |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1467 | if ( v == Py_None ) { |
| 1468 | *p_itself = NULL; |
| 1469 | return 1; |
| 1470 | } |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1471 | if (ResObj_Check(v)) |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1472 | { |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1473 | *p_itself = ((ResourceObject *)v)->ob_itself; |
| 1474 | return 1; |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1475 | } |
Jack Jansen | 2d76c25 | 1999-12-12 22:57:29 +0000 | [diff] [blame] | 1476 | /* If it isn't a resource yet see whether it is convertible */ |
| 1477 | if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) { |
| 1478 | *p_itself = ((ResourceObject *)tmp)->ob_itself; |
| 1479 | Py_DECREF(tmp); |
| 1480 | return 1; |
| 1481 | } |
| 1482 | PyErr_Clear(); |
| 1483 | PyErr_SetString(PyExc_TypeError, "Resource required"); |
| 1484 | return 0; |
Jack Jansen | 425e9eb | 1995-12-12 15:02:03 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Jack Jansen | d4c2646 | 1995-08-17 14:35:56 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1488 | void initRes() |
| 1489 | { |
| 1490 | PyObject *m; |
| 1491 | PyObject *d; |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | m = Py_InitModule("Res", Res_methods); |
| 1498 | d = PyModule_GetDict(m); |
| 1499 | Res_Error = PyMac_GetOSErrException(); |
| 1500 | if (Res_Error == NULL || |
| 1501 | PyDict_SetItemString(d, "Error", Res_Error) != 0) |
| 1502 | Py_FatalError("can't initialize Res.Error"); |
Jack Jansen | a755e68 | 1997-09-20 17:40:22 +0000 | [diff] [blame] | 1503 | Resource_Type.ob_type = &PyType_Type; |
| 1504 | Py_INCREF(&Resource_Type); |
| 1505 | if (PyDict_SetItemString(d, "ResourceType", (PyObject *)&Resource_Type) != 0) |
| 1506 | Py_FatalError("can't initialize ResourceType"); |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | /* ========================= End module Res ========================= */ |
| 1510 | |