Ticket #22278: patch-macosmodule.diff
File patch-macosmodule.diff, 10.0 KB (added by jaroel (Roel Bruggink), 14 years ago) |
---|
-
Mac/Modules/macosmodule.c
40 40 41 41 typedef struct { 42 42 PyObject_HEAD 43 shortfRefNum;43 FSIORefNum fRefNum; 44 44 int isclosed; 45 45 } rfobject; 46 46 … … 54 54 do_close(rfobject *self) 55 55 { 56 56 if (self->isclosed ) return; 57 (void)FSClose (self->fRefNum);57 (void)FSCloseFork(self->fRefNum); 58 58 self->isclosed = 1; 59 59 } 60 60 … … 68 68 long n; 69 69 PyObject *v; 70 70 OSErr err; 71 ByteCount n2; 71 72 72 73 if (self->isclosed) { 73 74 PyErr_SetString(PyExc_ValueError, "Operation on closed file"); … … 81 82 if (v == NULL) 82 83 return NULL; 83 84 84 err = FSRead (self->fRefNum, &n, PyString_AsString(v));85 err = FSReadFork(self->fRefNum, fsAtMark, 0, n, PyString_AsString(v), &n2); 85 86 if (err && err != eofErr) { 86 87 PyMac_Error(err); 87 88 Py_DECREF(v); 88 89 return NULL; 89 90 } 90 _PyString_Resize(&v, n );91 _PyString_Resize(&v, n2); 91 92 return v; 92 93 } 93 94 … … 109 110 } 110 111 if (!PyArg_ParseTuple(args, "s#", &buffer, &size)) 111 112 return NULL; 112 err = FSWrite (self->fRefNum, &size, buffer);113 err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL); 113 114 if (err) { 114 115 PyMac_Error(err); 115 116 return NULL; … … 126 127 static PyObject * 127 128 rf_seek(rfobject *self, PyObject *args) 128 129 { 129 long amount , pos;130 long amount; 130 131 int whence = SEEK_SET; 131 long eof;132 int mode; 132 133 OSErr err; 133 134 134 135 if (self->isclosed) { 135 136 PyErr_SetString(PyExc_ValueError, "Operation on closed file"); 136 137 return NULL; 137 138 } 138 if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) 139 if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) { 139 140 return NULL; 140 141 if ((err = GetEOF(self->fRefNum, &eof))) 142 goto ioerr; 141 } 143 142 144 143 switch (whence) { 145 144 case SEEK_CUR: 146 if ((err = GetFPos(self->fRefNum, &pos))) 147 goto ioerr; 145 mode = fsFromMark; 148 146 break; 149 147 case SEEK_END: 150 pos = eof;148 mode = fsFromLEOF; 151 149 break; 152 150 case SEEK_SET: 153 pos = 0;151 mode = fsFromStart; 154 152 break; 155 153 default: 156 154 PyErr_BadArgument(); 157 155 return NULL; 158 156 } 159 160 pos += amount; 161 162 /* Don't bother implementing seek past EOF */ 163 if (pos > eof || pos < 0) { 164 PyErr_BadArgument(); 165 return NULL; 166 } 167 168 if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) { 169 ioerr: 157 158 err = FSSetForkPosition(self->fRefNum, mode, amount); 159 if (err != noErr) { 170 160 PyMac_Error(err); 171 161 return NULL; 172 162 } … … 182 172 static PyObject * 183 173 rf_tell(rfobject *self, PyObject *args) 184 174 { 185 long where;175 long long where; 186 176 OSErr err; 187 177 188 178 if (self->isclosed) { … … 191 181 } 192 182 if (!PyArg_ParseTuple(args, "")) 193 183 return NULL; 194 if ((err = GetFPos(self->fRefNum, &where)) ) { 184 185 err = FSGetForkPosition(self->fRefNum, &where); 186 if (err != noErr) { 195 187 PyMac_Error(err); 196 188 return NULL; 197 189 } 198 return Py Int_FromLong(where);190 return PyLong_FromLongLong(where); 199 191 } 200 192 201 193 static char rf_close__doc__[] = … … 281 273 Rftype__doc__ /* Documentation string */ 282 274 }; 283 275 276 284 277 /* End of code for Resource fork objects */ 285 278 /* -------------------------------------------------------- */ 286 279 … … 292 285 static PyObject * 293 286 MacOS_GetCreatorAndType(PyObject *self, PyObject *args) 294 287 { 295 FSSpec fss;296 FInfo info;297 288 PyObject *creator, *type, *res; 298 289 OSErr err; 299 300 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss)) 290 FSRef ref; 291 FSCatalogInfo cataloginfo; 292 FileInfo* finfo; 293 294 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) { 295 #ifndef __LP64__ 296 /* This function is documented to take an FSSpec as well, 297 * which only works in 32-bit mode. 298 */ 299 PyErr_Clear(); 300 FSSpec fss; 301 FInfo info; 302 303 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss)) 304 return NULL; 305 306 if ((err = FSpGetFInfo(&fss, &info)) != noErr) { 307 return PyErr_Mac(MacOS_Error, err); 308 } 309 creator = PyString_FromStringAndSize( 310 (char *)&info.fdCreator, 4); 311 type = PyString_FromStringAndSize((char *)&info.fdType, 4); 312 res = Py_BuildValue("OO", creator, type); 313 Py_DECREF(creator); 314 Py_DECREF(type); 315 return res; 316 #else /* __LP64__ */ 317 return NULL; 318 #endif /* __LP64__ */ 319 } 320 321 err = FSGetCatalogInfo(&ref, 322 kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo, 323 NULL, NULL, NULL); 324 if (err != noErr) { 325 PyErr_Mac(MacOS_Error, err); 301 326 return NULL; 302 if ((err = FSpGetFInfo(&fss, &info)) != noErr) 303 return PyErr_Mac(MacOS_Error, err); 304 creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4); 305 type = PyString_FromStringAndSize((char *)&info.fdType, 4); 327 } 328 329 if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) { 330 /* Directory: doesn't have type/creator info. 331 * 332 * The specific error code is for backward compatibility with 333 * earlier versions. 334 */ 335 PyErr_Mac(MacOS_Error, fnfErr); 336 return NULL; 337 338 } 339 finfo = (FileInfo*)&(cataloginfo.finderInfo); 340 creator = PyString_FromStringAndSize((char*)&(finfo->fileCreator), 4); 341 type = PyString_FromStringAndSize((char*)&(finfo->fileType), 4); 342 306 343 res = Py_BuildValue("OO", creator, type); 307 344 Py_DECREF(creator); 308 345 Py_DECREF(type); … … 314 351 static PyObject * 315 352 MacOS_SetCreatorAndType(PyObject *self, PyObject *args) 316 353 { 317 FSSpec fss;318 354 ResType creator, type; 319 FInfo info; 355 FSRef ref; 356 FileInfo* finfo; 320 357 OSErr err; 321 358 FSCatalogInfo cataloginfo; 359 322 360 if (!PyArg_ParseTuple(args, "O&O&O&", 361 PyMac_GetFSRef, &ref, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) { 362 #ifndef __LP64__ 363 /* Try to handle FSSpec arguments, for backward compatibility */ 364 FSSpec fss; 365 FInfo info; 366 367 if (!PyArg_ParseTuple(args, "O&O&O&", 323 368 PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) 369 return NULL; 370 371 if ((err = FSpGetFInfo(&fss, &info)) != noErr) 372 return PyErr_Mac(MacOS_Error, err); 373 374 info.fdCreator = creator; 375 info.fdType = type; 376 377 if ((err = FSpSetFInfo(&fss, &info)) != noErr) 378 return PyErr_Mac(MacOS_Error, err); 379 Py_INCREF(Py_None); 380 return Py_None; 381 #else /* __LP64__ */ 324 382 return NULL; 325 if ((err = FSpGetFInfo(&fss, &info)) != noErr) 326 return PyErr_Mac(MacOS_Error, err); 327 info.fdCreator = creator; 328 info.fdType = type; 329 if ((err = FSpSetFInfo(&fss, &info)) != noErr) 330 return PyErr_Mac(MacOS_Error, err); 383 #endif /* __LP64__ */ 384 } 385 386 err = FSGetCatalogInfo(&ref, 387 kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo, 388 NULL, NULL, NULL); 389 if (err != noErr) { 390 PyErr_Mac(MacOS_Error, err); 391 return NULL; 392 } 393 394 if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) { 395 /* Directory: doesn't have type/creator info. 396 * 397 * The specific error code is for backward compatibility with 398 * earlier versions. 399 */ 400 PyErr_Mac(MacOS_Error, fnfErr); 401 return NULL; 402 403 } 404 finfo = (FileInfo*)&(cataloginfo.finderInfo); 405 finfo->fileCreator = creator; 406 finfo->fileType = type; 407 408 err = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &cataloginfo); 409 if (err != noErr) { 410 PyErr_Mac(MacOS_Error, fnfErr); 411 return NULL; 412 } 413 331 414 Py_INCREF(Py_None); 332 415 return Py_None; 333 416 } … … 375 458 /* And try again... */ 376 459 h = GetResource('Estr', err); 377 460 } 461 Py_DECREF(m); 378 462 } 379 463 } 380 464 /* … … 398 482 return Py_BuildValue("s", buf); 399 483 } 400 484 485 486 #ifndef __LP64__ 487 401 488 static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)"; 402 489 403 490 static PyObject * … … 416 503 return NULL; 417 504 olddialog = curdialog; 418 505 curdialog = NULL; 419 506 420 507 if ( resid != -1 ) { 421 508 curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1); 422 509 if ( curdialog ) { … … 451 538 452 539 if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object)) 453 540 return NULL; 541 454 542 DebugStr(message); 455 543 Py_INCREF(Py_None); 456 544 return Py_None; 457 545 } 458 546 547 459 548 static char SysBeep_doc[] = "BEEEEEP!!!"; 460 549 461 550 static PyObject * … … 470 559 return Py_None; 471 560 } 472 561 562 #endif /* __LP64__ */ 563 473 564 static char WMAvailable_doc[] = 474 565 "True if this process can interact with the display." 475 566 "Will foreground the application on the first call as a side-effect." … … 529 620 { 530 621 OSErr err; 531 622 char *mode = "r"; 532 FS Spec fss;533 S ignedByte permission = 1;623 FSRef ref; 624 SInt8 permission = fsRdPerm; 534 625 rfobject *fp; 626 HFSUniStr255 name; 535 627 536 if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFS Spec, &fss, &mode))628 if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSRef, &ref, &mode)) 537 629 return NULL; 538 630 while (*mode) { 539 631 switch (*mode++) { 540 632 case '*': break; 541 case 'r': permission = 1; break;542 case 'w': permission = 2; break;633 case 'r': permission = fsRdPerm; break; 634 case 'w': permission = fsWrPerm; break; 543 635 case 'b': break; 544 636 default: 545 637 PyErr_BadArgument(); 546 638 return NULL; 547 639 } 548 640 } 641 642 err = FSGetResourceForkName(&name); 643 if (err != noErr) { 644 PyMac_Error(err); 645 return NULL; 646 } 549 647 550 648 if ( (fp = newrfobject()) == NULL ) 551 649 return NULL; 552 553 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum); 650 554 651 555 if ( err == fnfErr ) { 556 /* In stead of doing complicated things here to get creator/type 557 ** correct we let the standard i/o library handle it 558 */ 559 FILE *tfp; 560 char pathname[PATHNAMELEN]; 561 562 if ( (err=PyMac_GetFullPathname(&fss, pathname, PATHNAMELEN)) ) { 563 PyMac_Error(err); 564 Py_DECREF(fp); 565 return NULL; 566 } 567 568 if ( (tfp = fopen(pathname, "w")) == NULL ) { 569 PyMac_Error(fnfErr); /* What else... */ 570 Py_DECREF(fp); 571 return NULL; 572 } 573 fclose(tfp); 574 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum); 575 } 576 if ( err ) { 652 err = FSOpenFork(&ref, name.length, name.unicode, permission, &fp->fRefNum); 653 if (err != noErr) { 577 654 Py_DECREF(fp); 578 655 PyMac_Error(err); 579 656 return NULL; … … 583 660 } 584 661 585 662 663 586 664 static PyMethodDef MacOS_Methods[] = { 587 665 {"GetCreatorAndType", MacOS_GetCreatorAndType, 1, getcrtp_doc}, 588 666 {"SetCreatorAndType", MacOS_SetCreatorAndType, 1, setcrtp_doc}, 589 667 {"GetErrorString", MacOS_GetErrorString, 1, geterr_doc}, 590 668 {"openrf", MacOS_openrf, 1, openrf_doc}, 669 #ifndef __LP64__ 591 670 {"splash", MacOS_splash, 1, splash_doc}, 592 671 {"DebugStr", MacOS_DebugStr, 1, DebugStr_doc}, 593 {"GetTicks", MacOS_GetTicks, 1, GetTicks_doc},594 672 {"SysBeep", MacOS_SysBeep, 1, SysBeep_doc}, 673 #endif /* __LP64__ */ 674 {"GetTicks", MacOS_GetTicks, 1, GetTicks_doc}, 595 675 {"WMAvailable", MacOS_WMAvailable, 1, WMAvailable_doc}, 596 676 {NULL, NULL} /* Sentinel */ 597 677 };