Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(264655)

Delta Between Two Patch Sets: Python/errors.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Left Patch Set: Created 8 years, 4 months ago
Right Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Python/compile.c ('k') | Python/future.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Error handling */ 2 /* Error handling */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #ifndef __STDC__ 6 #ifndef __STDC__
7 #ifndef MS_WINDOWS 7 #ifndef MS_WINDOWS
8 extern char *strerror(int); 8 extern char *strerror(int);
9 #endif 9 #endif
10 #endif 10 #endif
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 return; 64 return;
65 } 65 }
66 Py_XINCREF(value); 66 Py_XINCREF(value);
67 exc_value = tstate->exc_value; 67 exc_value = tstate->exc_value;
68 if (exc_value != NULL && exc_value != Py_None) { 68 if (exc_value != NULL && exc_value != Py_None) {
69 /* Implicit exception chaining */ 69 /* Implicit exception chaining */
70 Py_INCREF(exc_value); 70 Py_INCREF(exc_value);
71 if (value == NULL || !PyExceptionInstance_Check(value)) { 71 if (value == NULL || !PyExceptionInstance_Check(value)) {
72 /* We must normalize the value right now */ 72 /* We must normalize the value right now */
73 PyObject *args, *fixed_value; 73 PyObject *args, *fixed_value;
74 #ifdef Py_DEBUG
75 /* in debug mode, PyEval_EvalFrameEx() fails with an assertion
76 error if an exception is set when it is called */
77 PyErr_Clear();
78 #endif
74 if (value == NULL || value == Py_None) 79 if (value == NULL || value == Py_None)
75 args = PyTuple_New(0); 80 args = PyTuple_New(0);
76 else if (PyTuple_Check(value)) { 81 else if (PyTuple_Check(value)) {
77 Py_INCREF(value); 82 Py_INCREF(value);
78 args = value; 83 args = value;
79 } 84 }
80 else 85 else
81 args = PyTuple_Pack(1, value); 86 args = PyTuple_Pack(1, value);
82 fixed_value = args ? 87 fixed_value = args ?
83 PyEval_CallObject(exception, args) : NULL; 88 PyEval_CallObject(exception, args) : NULL;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 Py_INCREF(value); 225 Py_INCREF(value);
221 } 226 }
222 227
223 if (PyExceptionInstance_Check(value)) 228 if (PyExceptionInstance_Check(value))
224 inclass = PyExceptionInstance_Class(value); 229 inclass = PyExceptionInstance_Class(value);
225 230
226 /* Normalize the exception so that if the type is a class, the 231 /* Normalize the exception so that if the type is a class, the
227 value will be an instance. 232 value will be an instance.
228 */ 233 */
229 if (PyExceptionClass_Check(type)) { 234 if (PyExceptionClass_Check(type)) {
235 int is_subclass;
236 if (inclass) {
237 is_subclass = PyObject_IsSubclass(inclass, type);
238 if (is_subclass < 0)
239 goto finally;
240 }
241 else
242 is_subclass = 0;
243
230 /* if the value was not an instance, or is not an instance 244 /* if the value was not an instance, or is not an instance
231 whose class is (or is derived from) type, then use the 245 whose class is (or is derived from) type, then use the
232 value as an argument to instantiation of the type 246 value as an argument to instantiation of the type
233 class. 247 class.
234 */ 248 */
235 if (!inclass || !PyObject_IsSubclass(inclass, type)) { 249 if (!inclass || !is_subclass) {
236 PyObject *args, *res; 250 PyObject *args, *res;
237 251
238 if (value == Py_None) 252 if (value == Py_None)
239 args = PyTuple_New(0); 253 args = PyTuple_New(0);
240 else if (PyTuple_Check(value)) { 254 else if (PyTuple_Check(value)) {
241 Py_INCREF(value); 255 Py_INCREF(value);
242 args = value; 256 args = value;
243 } 257 }
244 else 258 else
245 args = PyTuple_Pack(1, value); 259 args = PyTuple_Pack(1, value);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 tstate->curexc_value = NULL; 327 tstate->curexc_value = NULL;
314 tstate->curexc_traceback = NULL; 328 tstate->curexc_traceback = NULL;
315 } 329 }
316 330
317 void 331 void
318 PyErr_Clear(void) 332 PyErr_Clear(void)
319 { 333 {
320 PyErr_Restore(NULL, NULL, NULL); 334 PyErr_Restore(NULL, NULL, NULL);
321 } 335 }
322 336
337 void
338 PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
339 {
340 PyThreadState *tstate = PyThreadState_GET();
341
342 *p_type = tstate->exc_type;
343 *p_value = tstate->exc_value;
344 *p_traceback = tstate->exc_traceback;
345
346 Py_XINCREF(*p_type);
347 Py_XINCREF(*p_value);
348 Py_XINCREF(*p_traceback);
349 }
350
351 void
352 PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
353 {
354 PyObject *oldtype, *oldvalue, *oldtraceback;
355 PyThreadState *tstate = PyThreadState_GET();
356
357 oldtype = tstate->exc_type;
358 oldvalue = tstate->exc_value;
359 oldtraceback = tstate->exc_traceback;
360
361 tstate->exc_type = p_type;
362 tstate->exc_value = p_value;
363 tstate->exc_traceback = p_traceback;
364
365 Py_XDECREF(oldtype);
366 Py_XDECREF(oldvalue);
367 Py_XDECREF(oldtraceback);
368 }
369
323 /* Convenience functions to set a type error exception and return 0 */ 370 /* Convenience functions to set a type error exception and return 0 */
324 371
325 int 372 int
326 PyErr_BadArgument(void) 373 PyErr_BadArgument(void)
327 { 374 {
328 PyErr_SetString(PyExc_TypeError, 375 PyErr_SetString(PyExc_TypeError,
329 "bad argument type for built-in operation"); 376 "bad argument type for built-in operation");
330 return 0; 377 return 0;
331 } 378 }
332 379
333 PyObject * 380 PyObject *
334 PyErr_NoMemory(void) 381 PyErr_NoMemory(void)
335 { 382 {
383 if (Py_TYPE(PyExc_MemoryError) == NULL) {
384 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
385 initialized by _PyExc_Init() */
386 Py_FatalError("Out of memory and PyExc_MemoryError is not "
387 "initialized yet");
388 }
336 PyErr_SetNone(PyExc_MemoryError); 389 PyErr_SetNone(PyExc_MemoryError);
337 return NULL; 390 return NULL;
338 } 391 }
339 392
340 PyObject * 393 PyObject *
341 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) 394 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
342 { 395 {
343 PyObject *message; 396 PyObject *message;
344 PyObject *v; 397 PyObject *v, *args;
345 int i = errno; 398 int i = errno;
346 #ifndef MS_WINDOWS 399 #ifdef MS_WINDOWS
347 char *s;
348 #else
349 WCHAR *s_buf = NULL; 400 WCHAR *s_buf = NULL;
350 #endif /* Unix/Windows */ 401 #endif /* Unix/Windows */
351 402
352 #ifdef EINTR 403 #ifdef EINTR
353 if (i == EINTR && PyErr_CheckSignals()) 404 if (i == EINTR && PyErr_CheckSignals())
354 return NULL; 405 return NULL;
355 #endif 406 #endif
356 407
357 #ifndef MS_WINDOWS 408 #ifndef MS_WINDOWS
358 if (i == 0) 409 if (i != 0) {
359 s = "Error"; /* Sometimes errno didn't get set */ 410 char *s = strerror(i);
360 else 411 message = PyUnicode_DecodeLocale(s, "surrogateescape");
361 s = strerror(i); 412 }
362 message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); 413 else {
414 /* Sometimes errno didn't get set */
415 message = PyUnicode_FromString("Error");
416 }
363 #else 417 #else
364 if (i == 0) 418 if (i == 0)
365 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get s et */ 419 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get s et */
366 else 420 else
367 { 421 {
368 /* Note that the Win32 errors do not lineup with the 422 /* Note that the Win32 errors do not lineup with the
369 errno error. So if the error is in the MSVC error 423 errno error. So if the error is in the MSVC error
370 table, we use it, otherwise we assume it really _is_ 424 table, we use it, otherwise we assume it really _is_
371 a Win32 error code 425 a Win32 error code
372 */ 426 */
(...skipping 15 matching lines...) Expand all
388 NULL); /* no args */ 442 NULL); /* no args */
389 if (len==0) { 443 if (len==0) {
390 /* Only ever seen this in out-of-mem 444 /* Only ever seen this in out-of-mem
391 situations */ 445 situations */
392 s_buf = NULL; 446 s_buf = NULL;
393 message = PyUnicode_FromFormat("Windows Error 0x%X", i); 447 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
394 } else { 448 } else {
395 /* remove trailing cr/lf and dots */ 449 /* remove trailing cr/lf and dots */
396 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.') ) 450 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.') )
397 s_buf[--len] = L'\0'; 451 s_buf[--len] = L'\0';
398 message = PyUnicode_FromUnicode(s_buf, len); 452 message = PyUnicode_FromWideChar(s_buf, len);
399 } 453 }
400 } 454 }
401 } 455 }
402 #endif /* Unix/Windows */ 456 #endif /* Unix/Windows */
403 457
404 if (message == NULL) 458 if (message == NULL)
405 { 459 {
406 #ifdef MS_WINDOWS 460 #ifdef MS_WINDOWS
407 LocalFree(s_buf); 461 LocalFree(s_buf);
408 #endif 462 #endif
409 return NULL; 463 return NULL;
410 } 464 }
411 465
412 if (filenameObject != NULL) 466 if (filenameObject != NULL)
413 v = Py_BuildValue("(iOO)", i, message, filenameObject); 467 args = Py_BuildValue("(iOO)", i, message, filenameObject);
414 else 468 else
415 v = Py_BuildValue("(iO)", i, message); 469 args = Py_BuildValue("(iO)", i, message);
416 Py_DECREF(message); 470 Py_DECREF(message);
417 471
418 if (v != NULL) { 472 if (args != NULL) {
419 PyErr_SetObject(exc, v); 473 v = PyObject_Call(exc, args, NULL);
420 Py_DECREF(v); 474 Py_DECREF(args);
475 if (v != NULL) {
476 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
477 Py_DECREF(v);
478 }
421 } 479 }
422 #ifdef MS_WINDOWS 480 #ifdef MS_WINDOWS
423 LocalFree(s_buf); 481 LocalFree(s_buf);
424 #endif 482 #endif
425 return NULL; 483 return NULL;
426 } 484 }
427 485
428 486
429 PyObject * 487 PyObject *
430 PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) 488 PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
(...skipping 26 matching lines...) Expand all
457 #ifdef MS_WINDOWS 515 #ifdef MS_WINDOWS
458 /* Windows specific error code handling */ 516 /* Windows specific error code handling */
459 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( 517 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
460 PyObject *exc, 518 PyObject *exc,
461 int ierr, 519 int ierr,
462 PyObject *filenameObject) 520 PyObject *filenameObject)
463 { 521 {
464 int len; 522 int len;
465 WCHAR *s_buf = NULL; /* Free via LocalFree */ 523 WCHAR *s_buf = NULL; /* Free via LocalFree */
466 PyObject *message; 524 PyObject *message;
467 PyObject *v; 525 PyObject *args, *v;
468 DWORD err = (DWORD)ierr; 526 DWORD err = (DWORD)ierr;
469 if (err==0) err = GetLastError(); 527 if (err==0) err = GetLastError();
470 len = FormatMessageW( 528 len = FormatMessageW(
471 /* Error API error */ 529 /* Error API error */
472 FORMAT_MESSAGE_ALLOCATE_BUFFER | 530 FORMAT_MESSAGE_ALLOCATE_BUFFER |
473 FORMAT_MESSAGE_FROM_SYSTEM | 531 FORMAT_MESSAGE_FROM_SYSTEM |
474 FORMAT_MESSAGE_IGNORE_INSERTS, 532 FORMAT_MESSAGE_IGNORE_INSERTS,
475 NULL, /* no message source */ 533 NULL, /* no message source */
476 err, 534 err,
477 MAKELANGID(LANG_NEUTRAL, 535 MAKELANGID(LANG_NEUTRAL,
478 SUBLANG_DEFAULT), /* Default language */ 536 SUBLANG_DEFAULT), /* Default language */
479 (LPWSTR) &s_buf, 537 (LPWSTR) &s_buf,
480 0, /* size not used */ 538 0, /* size not used */
481 NULL); /* no args */ 539 NULL); /* no args */
482 if (len==0) { 540 if (len==0) {
483 /* Only seen this in out of mem situations */ 541 /* Only seen this in out of mem situations */
484 message = PyUnicode_FromFormat("Windows Error 0x%X", err); 542 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
485 s_buf = NULL; 543 s_buf = NULL;
486 } else { 544 } else {
487 /* remove trailing cr/lf and dots */ 545 /* remove trailing cr/lf and dots */
488 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) 546 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
489 s_buf[--len] = L'\0'; 547 s_buf[--len] = L'\0';
490 message = PyUnicode_FromUnicode(s_buf, len); 548 message = PyUnicode_FromWideChar(s_buf, len);
491 } 549 }
492 550
493 if (message == NULL) 551 if (message == NULL)
494 { 552 {
495 LocalFree(s_buf); 553 LocalFree(s_buf);
496 return NULL; 554 return NULL;
497 } 555 }
498 556
499 if (filenameObject != NULL) 557 if (filenameObject == NULL)
500 v = Py_BuildValue("(iOO)", err, message, filenameObject); 558 filenameObject = Py_None;
501 else 559 /* This is the constructor signature for passing a Windows error code.
502 v = Py_BuildValue("(iO)", err, message); 560 The POSIX translation will be figured out by the constructor. */
561 args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
503 Py_DECREF(message); 562 Py_DECREF(message);
504 563
505 if (v != NULL) { 564 if (args != NULL) {
506 PyErr_SetObject(exc, v); 565 v = PyObject_Call(exc, args, NULL);
507 Py_DECREF(v); 566 Py_DECREF(args);
567 if (v != NULL) {
568 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
569 Py_DECREF(v);
570 }
508 } 571 }
509 LocalFree(s_buf); 572 LocalFree(s_buf);
510 return NULL; 573 return NULL;
511 } 574 }
512 575
513 PyObject *PyErr_SetExcFromWindowsErrWithFilename( 576 PyObject *PyErr_SetExcFromWindowsErrWithFilename(
514 PyObject *exc, 577 PyObject *exc,
515 int ierr, 578 int ierr,
516 const char *filename) 579 const char *filename)
517 { 580 {
(...skipping 20 matching lines...) Expand all
538 return ret; 601 return ret;
539 } 602 }
540 603
541 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) 604 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
542 { 605 {
543 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); 606 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
544 } 607 }
545 608
546 PyObject *PyErr_SetFromWindowsErr(int ierr) 609 PyObject *PyErr_SetFromWindowsErr(int ierr)
547 { 610 {
548 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, 611 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
549 ierr, NULL); 612 ierr, NULL);
550 } 613 }
551 PyObject *PyErr_SetFromWindowsErrWithFilename( 614 PyObject *PyErr_SetFromWindowsErrWithFilename(
552 int ierr, 615 int ierr,
553 const char *filename) 616 const char *filename)
554 { 617 {
555 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; 618 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
556 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( 619 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
557 PyExc_WindowsError, 620 PyExc_OSError,
558 ierr, name); 621 ierr, name);
559 Py_XDECREF(name); 622 Py_XDECREF(name);
560 return result; 623 return result;
561 } 624 }
562 625
563 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( 626 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
564 int ierr, 627 int ierr,
565 const Py_UNICODE *filename) 628 const Py_UNICODE *filename)
566 { 629 {
567 PyObject *name = filename ? 630 PyObject *name = filename ?
568 PyUnicode_FromUnicode(filename, wcslen(filename)) : 631 PyUnicode_FromUnicode(filename, wcslen(filename)) :
569 NULL; 632 NULL;
570 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( 633 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
571 PyExc_WindowsError, 634 PyExc_OSError,
572 ierr, name); 635 ierr, name);
573 Py_XDECREF(name); 636 Py_XDECREF(name);
574 return result; 637 return result;
575 } 638 }
576 #endif /* MS_WINDOWS */ 639 #endif /* MS_WINDOWS */
577 640
641 PyObject *
642 PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
643 {
644 PyObject *args, *kwargs, *error;
645
646 if (msg == NULL)
647 return NULL;
648
649 args = PyTuple_New(1);
650 if (args == NULL)
651 return NULL;
652
653 kwargs = PyDict_New();
654 if (kwargs == NULL) {
655 Py_DECREF(args);
656 return NULL;
657 }
658
659 if (name == NULL) {
660 name = Py_None;
661 }
662
663 if (path == NULL) {
664 path = Py_None;
665 }
666
667 Py_INCREF(msg);
668 PyTuple_SET_ITEM(args, 0, msg);
669
670 if (PyDict_SetItemString(kwargs, "name", name) < 0)
671 return NULL;
672 if (PyDict_SetItemString(kwargs, "path", path) < 0)
673 return NULL;
674
675 error = PyObject_Call(PyExc_ImportError, args, kwargs);
676 if (error != NULL) {
677 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
678 Py_DECREF(error);
679 }
680
681 Py_DECREF(args);
682 Py_DECREF(kwargs);
683
684 return NULL;
685 }
686
578 void 687 void
579 _PyErr_BadInternalCall(const char *filename, int lineno) 688 _PyErr_BadInternalCall(const char *filename, int lineno)
580 { 689 {
581 PyErr_Format(PyExc_SystemError, 690 PyErr_Format(PyExc_SystemError,
582 "%s:%d: bad argument to internal function", 691 "%s:%d: bad argument to internal function",
583 filename, lineno); 692 filename, lineno);
584 } 693 }
585 694
586 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can 695 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
587 export the entry point for existing object code: */ 696 export the entry point for existing object code: */
588 #undef PyErr_BadInternalCall 697 #undef PyErr_BadInternalCall
589 void 698 void
590 PyErr_BadInternalCall(void) 699 PyErr_BadInternalCall(void)
591 { 700 {
701 assert(0 && "bad argument to internal function");
592 PyErr_Format(PyExc_SystemError, 702 PyErr_Format(PyExc_SystemError,
593 "bad argument to internal function"); 703 "bad argument to internal function");
594 } 704 }
595 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) 705 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
596 706
597 707
598 708
599 PyObject * 709 PyObject *
600 PyErr_Format(PyObject *exception, const char *format, ...) 710 PyErr_Format(PyObject *exception, const char *format, ...)
601 { 711 {
602 va_list vargs; 712 va_list vargs;
603 PyObject* string; 713 PyObject* string;
604 714
605 #ifdef HAVE_STDARG_PROTOTYPES 715 #ifdef HAVE_STDARG_PROTOTYPES
606 va_start(vargs, format); 716 va_start(vargs, format);
607 #else 717 #else
608 va_start(vargs); 718 va_start(vargs);
719 #endif
720
721 #ifdef Py_DEBUG
722 /* in debug mode, PyEval_EvalFrameEx() fails with an assertion error
723 if an exception is set when it is called */
724 PyErr_Clear();
609 #endif 725 #endif
610 726
611 string = PyUnicode_FromFormatV(format, vargs); 727 string = PyUnicode_FromFormatV(format, vargs);
612 PyErr_SetObject(exception, string); 728 PyErr_SetObject(exception, string);
613 Py_XDECREF(string); 729 Py_XDECREF(string);
614 va_end(vargs); 730 va_end(vargs);
615 return NULL; 731 return NULL;
616 } 732 }
617 733
618 734
(...skipping 30 matching lines...) Expand all
649 } 765 }
650 if (PyTuple_Check(base)) { 766 if (PyTuple_Check(base)) {
651 bases = base; 767 bases = base;
652 /* INCREF as we create a new ref in the else branch */ 768 /* INCREF as we create a new ref in the else branch */
653 Py_INCREF(bases); 769 Py_INCREF(bases);
654 } else { 770 } else {
655 bases = PyTuple_Pack(1, base); 771 bases = PyTuple_Pack(1, base);
656 if (bases == NULL) 772 if (bases == NULL)
657 goto failure; 773 goto failure;
658 } 774 }
659 /* Create a real new-style class. */ 775 /* Create a real class. */
660 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", 776 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
661 dot+1, bases, dict); 777 dot+1, bases, dict);
662 failure: 778 failure:
663 Py_XDECREF(bases); 779 Py_XDECREF(bases);
664 Py_XDECREF(mydict); 780 Py_XDECREF(mydict);
665 Py_XDECREF(classname); 781 Py_XDECREF(classname);
666 Py_XDECREF(modulename); 782 Py_XDECREF(modulename);
667 return result; 783 return result;
668 } 784 }
669 785
(...skipping 30 matching lines...) Expand all
700 Py_XDECREF(mydict); 816 Py_XDECREF(mydict);
701 return ret; 817 return ret;
702 } 818 }
703 819
704 820
705 /* Call when an exception has occurred but there is no way for Python 821 /* Call when an exception has occurred but there is no way for Python
706 to handle it. Examples: exception in __del__ or during GC. */ 822 to handle it. Examples: exception in __del__ or during GC. */
707 void 823 void
708 PyErr_WriteUnraisable(PyObject *obj) 824 PyErr_WriteUnraisable(PyObject *obj)
709 { 825 {
826 _Py_IDENTIFIER(__module__);
710 PyObject *f, *t, *v, *tb; 827 PyObject *f, *t, *v, *tb;
828 PyObject *moduleName = NULL;
829 char* className;
830
711 PyErr_Fetch(&t, &v, &tb); 831 PyErr_Fetch(&t, &v, &tb);
832
712 f = PySys_GetObject("stderr"); 833 f = PySys_GetObject("stderr");
713 if (f != NULL && f != Py_None) { 834 if (f == NULL || f == Py_None)
714 PyFile_WriteString("Exception ", f); 835 goto done;
715 if (t) { 836
716 PyObject* moduleName; 837 if (obj) {
717 char* className; 838 if (PyFile_WriteString("Exception ignored in: ", f) < 0)
718 assert(PyExceptionClass_Check(t)); 839 goto done;
719 className = PyExceptionClass_Name(t); 840 if (PyFile_WriteObject(obj, f, 0) < 0)
720 if (className != NULL) { 841 goto done;
721 char *dot = strrchr(className, '.'); 842 if (PyFile_WriteString("\n", f) < 0)
722 if (dot != NULL) 843 goto done;
723 className = dot+1; 844 }
724 } 845
725 846 if (PyTraceBack_Print(tb, f) < 0)
726 moduleName = PyObject_GetAttrString(t, "__module__"); 847 goto done;
727 if (moduleName == NULL) 848
728 PyFile_WriteString("<unknown>", f); 849 if (!t)
729 else { 850 goto done;
730 char* modstr = _PyUnicode_AsString(moduleName); 851
731 if (modstr && 852 assert(PyExceptionClass_Check(t));
732 strcmp(modstr, "builtins") != 0) 853 className = PyExceptionClass_Name(t);
733 { 854 if (className != NULL) {
734 PyFile_WriteString(modstr, f); 855 char *dot = strrchr(className, '.');
735 PyFile_WriteString(".", f); 856 if (dot != NULL)
736 } 857 className = dot+1;
737 } 858 }
738 if (className == NULL) 859
739 PyFile_WriteString("<unknown>", f); 860 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
740 else 861 if (moduleName == NULL) {
741 PyFile_WriteString(className, f); 862 PyErr_Clear();
742 if (v && v != Py_None) { 863 if (PyFile_WriteString("<unknown>", f) < 0)
743 PyFile_WriteString(": ", f); 864 goto done;
744 PyFile_WriteObject(v, f, 0); 865 }
745 } 866 else {
746 Py_XDECREF(moduleName); 867 if (PyUnicode_CompareWithASCIIString(moduleName, "builtins") != 0) {
747 } 868 if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
748 if (obj) { 869 goto done;
749 PyFile_WriteString(" in ", f); 870 if (PyFile_WriteString(".", f) < 0)
750 PyFile_WriteObject(obj, f, 0); 871 goto done;
751 } 872 }
752 PyFile_WriteString(" ignored\n", f); 873 }
753 PyErr_Clear(); /* Just in case */ 874 if (className == NULL) {
754 } 875 if (PyFile_WriteString("<unknown>", f) < 0)
876 goto done;
877 }
878 else {
879 if (PyFile_WriteString(className, f) < 0)
880 goto done;
881 }
882
883 if (v && v != Py_None) {
884 if (PyFile_WriteString(": ", f) < 0)
885 goto done;
886 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
887 goto done;
888 }
889 if (PyFile_WriteString("\n", f) < 0)
890 goto done;
891
892 done:
893 Py_XDECREF(moduleName);
755 Py_XDECREF(t); 894 Py_XDECREF(t);
756 Py_XDECREF(v); 895 Py_XDECREF(v);
757 Py_XDECREF(tb); 896 Py_XDECREF(tb);
897 PyErr_Clear(); /* Just in case */
758 } 898 }
759 899
760 extern PyObject *PyModule_GetWarningsModule(void); 900 extern PyObject *PyModule_GetWarningsModule(void);
761 901
762 902
763 void 903 void
764 PyErr_SyntaxLocation(const char *filename, int lineno) { 904 PyErr_SyntaxLocation(const char *filename, int lineno)
905 {
765 PyErr_SyntaxLocationEx(filename, lineno, -1); 906 PyErr_SyntaxLocationEx(filename, lineno, -1);
766 } 907 }
767 908
768 909
769 /* Set file and line information for the current exception. 910 /* Set file and line information for the current exception.
770 If the exception is not a SyntaxError, also sets additional attributes 911 If the exception is not a SyntaxError, also sets additional attributes
771 to make printing of exceptions believe it is a syntax error. */ 912 to make printing of exceptions believe it is a syntax error. */
772 913
773 void 914 void
774 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) 915 PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
775 { 916 {
776 PyObject *exc, *v, *tb, *tmp; 917 PyObject *exc, *v, *tb, *tmp;
918 _Py_IDENTIFIER(filename);
919 _Py_IDENTIFIER(lineno);
920 _Py_IDENTIFIER(msg);
921 _Py_IDENTIFIER(offset);
922 _Py_IDENTIFIER(print_file_and_line);
923 _Py_IDENTIFIER(text);
777 924
778 /* add attributes for the line number and filename for the error */ 925 /* add attributes for the line number and filename for the error */
779 PyErr_Fetch(&exc, &v, &tb); 926 PyErr_Fetch(&exc, &v, &tb);
780 PyErr_NormalizeException(&exc, &v, &tb); 927 PyErr_NormalizeException(&exc, &v, &tb);
781 /* XXX check that it is, indeed, a syntax error. It might not 928 /* XXX check that it is, indeed, a syntax error. It might not
782 * be, though. */ 929 * be, though. */
783 tmp = PyLong_FromLong(lineno); 930 tmp = PyLong_FromLong(lineno);
784 if (tmp == NULL) 931 if (tmp == NULL)
785 PyErr_Clear(); 932 PyErr_Clear();
786 else { 933 else {
787 if (PyObject_SetAttrString(v, "lineno", tmp)) 934 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
788 PyErr_Clear(); 935 PyErr_Clear();
789 Py_DECREF(tmp); 936 Py_DECREF(tmp);
790 } 937 }
791 if (col_offset >= 0) { 938 if (col_offset >= 0) {
792 tmp = PyLong_FromLong(col_offset); 939 tmp = PyLong_FromLong(col_offset);
793 if (tmp == NULL) 940 if (tmp == NULL)
794 PyErr_Clear(); 941 PyErr_Clear();
795 else { 942 else {
796 if (PyObject_SetAttrString(v, "offset", tmp)) 943 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
797 PyErr_Clear(); 944 PyErr_Clear();
798 Py_DECREF(tmp); 945 Py_DECREF(tmp);
799 } 946 }
800 } 947 }
801 if (filename != NULL) { 948 if (filename != NULL) {
802 tmp = PyUnicode_DecodeFSDefault(filename); 949 if (_PyObject_SetAttrId(v, &PyId_filename, filename))
803 if (tmp == NULL)
804 PyErr_Clear(); 950 PyErr_Clear();
805 else { 951
806 if (PyObject_SetAttrString(v, "filename", tmp)) 952 tmp = PyErr_ProgramTextObject(filename, lineno);
953 if (tmp) {
954 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
807 PyErr_Clear(); 955 PyErr_Clear();
808 Py_DECREF(tmp); 956 Py_DECREF(tmp);
809 } 957 }
810 958 }
811 tmp = PyErr_ProgramText(filename, lineno); 959 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
812 if (tmp) {
813 if (PyObject_SetAttrString(v, "text", tmp))
814 PyErr_Clear();
815 Py_DECREF(tmp);
816 }
817 }
818 if (PyObject_SetAttrString(v, "offset", Py_None)) {
819 PyErr_Clear(); 960 PyErr_Clear();
820 } 961 }
821 if (exc != PyExc_SyntaxError) { 962 if (exc != PyExc_SyntaxError) {
822 if (!PyObject_HasAttrString(v, "msg")) { 963 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
823 tmp = PyObject_Str(v); 964 tmp = PyObject_Str(v);
824 if (tmp) { 965 if (tmp) {
825 if (PyObject_SetAttrString(v, "msg", tmp)) 966 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
826 PyErr_Clear(); 967 PyErr_Clear();
827 Py_DECREF(tmp); 968 Py_DECREF(tmp);
828 } else { 969 } else {
829 PyErr_Clear(); 970 PyErr_Clear();
830 } 971 }
831 } 972 }
832 if (!PyObject_HasAttrString(v, "print_file_and_line")) { 973 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
833 if (PyObject_SetAttrString(v, "print_file_and_line", 974 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
834 Py_None)) 975 Py_None))
835 PyErr_Clear(); 976 PyErr_Clear();
836 } 977 }
837 } 978 }
838 PyErr_Restore(exc, v, tb); 979 PyErr_Restore(exc, v, tb);
980 }
981
982 void
983 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
984 {
985 PyObject *fileobj;
986 if (filename != NULL) {
987 fileobj = PyUnicode_DecodeFSDefault(filename);
988 if (fileobj == NULL)
989 PyErr_Clear();
990 }
991 else
992 fileobj = NULL;
993 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
994 Py_XDECREF(fileobj);
839 } 995 }
840 996
841 /* Attempt to load the line of text that the exception refers to. If it 997 /* Attempt to load the line of text that the exception refers to. If it
842 fails, it will return NULL but will not set an exception. 998 fails, it will return NULL but will not set an exception.
843 999
844 XXX The functionality of this function is quite similar to the 1000 XXX The functionality of this function is quite similar to the
845 functionality in tb_displayline() in traceback.c. */ 1001 functionality in tb_displayline() in traceback.c. */
846 1002
847 PyObject * 1003 PyObject *
848 err_programtext(FILE *fp, int lineno) 1004 err_programtext(FILE *fp, int lineno)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 FILE *fp; 1052 FILE *fp;
897 if (filename == NULL || lineno <= 0) 1053 if (filename == NULL || lineno <= 0)
898 return NULL; 1054 return NULL;
899 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); 1055 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
900 return err_programtext(fp, lineno); 1056 return err_programtext(fp, lineno);
901 } 1057 }
902 1058
903 #ifdef __cplusplus 1059 #ifdef __cplusplus
904 } 1060 }
905 #endif 1061 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+