comparison Python/errors.c @ 38431:4c0823f0a0a1 legacy-trunk

Conversion of exceptions over from faked-up classes to new-style C types.
author Richard Jones <richard@commonground.com.au>
date Sat, 27 May 2006 12:29:24 +0000
parents 698198cabbc5
children 1a23706139ca
comparison
equal deleted inserted replaced
38430:795d1cb1df01 38431:4c0823f0a0a1
555 if (modulename == NULL) 555 if (modulename == NULL)
556 goto failure; 556 goto failure;
557 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) 557 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
558 goto failure; 558 goto failure;
559 } 559 }
560 classname = PyString_FromString(dot+1);
561 if (classname == NULL)
562 goto failure;
563 if (PyTuple_Check(base)) { 560 if (PyTuple_Check(base)) {
564 bases = base; 561 bases = base;
565 /* INCREF as we create a new ref in the else branch */ 562 /* INCREF as we create a new ref in the else branch */
566 Py_INCREF(bases); 563 Py_INCREF(bases);
567 } else { 564 } else {
568 bases = PyTuple_Pack(1, base); 565 bases = PyTuple_Pack(1, base);
569 if (bases == NULL) 566 if (bases == NULL)
570 goto failure; 567 goto failure;
571 } 568 }
572 result = PyClass_New(bases, dict, classname); 569 /* Create a real new-style class. */
570 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
571 dot+1, bases, dict);
573 failure: 572 failure:
574 Py_XDECREF(bases); 573 Py_XDECREF(bases);
575 Py_XDECREF(mydict); 574 Py_XDECREF(mydict);
576 Py_XDECREF(classname); 575 Py_XDECREF(classname);
577 Py_XDECREF(modulename); 576 Py_XDECREF(modulename);
588 f = PySys_GetObject("stderr"); 587 f = PySys_GetObject("stderr");
589 if (f != NULL) { 588 if (f != NULL) {
590 PyFile_WriteString("Exception ", f); 589 PyFile_WriteString("Exception ", f);
591 if (t) { 590 if (t) {
592 char* className = PyExceptionClass_Name(t); 591 char* className = PyExceptionClass_Name(t);
593 PyObject* moduleName = 592 PyObject* moduleName;
594 PyObject_GetAttrString(t, "__module__"); 593 char *dot = strrchr(className, '.');
594 if (dot != NULL)
595 className = dot+1;
596 moduleName = PyObject_GetAttrString(t, "__module__");
595 597
596 if (moduleName == NULL) 598 if (moduleName == NULL)
597 PyFile_WriteString("<unknown>", f); 599 PyFile_WriteString("<unknown>", f);
598 else { 600 else {
599 char* modstr = PyString_AsString(moduleName); 601 char* modstr = PyString_AsString(moduleName);
639 if (func == NULL) { 641 if (func == NULL) {
640 PySys_WriteStderr("warning: %s\n", message); 642 PySys_WriteStderr("warning: %s\n", message);
641 return 0; 643 return 0;
642 } 644 }
643 else { 645 else {
644 PyObject *args, *res; 646 PyObject *res;
645 647
646 if (category == NULL) 648 if (category == NULL)
647 category = PyExc_RuntimeWarning; 649 category = PyExc_RuntimeWarning;
648 args = Py_BuildValue("(sO)", message, category); 650 res = PyObject_CallFunction(func, "sO", message, category);
649 if (args == NULL)
650 return -1;
651 res = PyEval_CallObject(func, args);
652 Py_DECREF(args);
653 if (res == NULL) 651 if (res == NULL)
654 return -1; 652 return -1;
655 Py_DECREF(res); 653 Py_DECREF(res);
656 return 0; 654 return 0;
657 } 655 }
675 if (func == NULL) { 673 if (func == NULL) {
676 PySys_WriteStderr("warning: %s\n", message); 674 PySys_WriteStderr("warning: %s\n", message);
677 return 0; 675 return 0;
678 } 676 }
679 else { 677 else {
680 PyObject *args, *res; 678 PyObject *res;
681 679
682 if (category == NULL) 680 if (category == NULL)
683 category = PyExc_RuntimeWarning; 681 category = PyExc_RuntimeWarning;
684 if (registry == NULL) 682 if (registry == NULL)
685 registry = Py_None; 683 registry = Py_None;
686 args = Py_BuildValue("(sOsizO)", message, category, 684 res = PyObject_CallFunction(func, "sOsizO", message, category,
687 filename, lineno, module, registry); 685 filename, lineno, module, registry);
688 if (args == NULL)
689 return -1;
690 res = PyEval_CallObject(func, args);
691 Py_DECREF(args);
692 if (res == NULL) 686 if (res == NULL)
693 return -1; 687 return -1;
694 Py_DECREF(res); 688 Py_DECREF(res);
695 return 0; 689 return 0;
696 } 690 }
707 PyObject *exc, *v, *tb, *tmp; 701 PyObject *exc, *v, *tb, *tmp;
708 702
709 /* add attributes for the line number and filename for the error */ 703 /* add attributes for the line number and filename for the error */
710 PyErr_Fetch(&exc, &v, &tb); 704 PyErr_Fetch(&exc, &v, &tb);
711 PyErr_NormalizeException(&exc, &v, &tb); 705 PyErr_NormalizeException(&exc, &v, &tb);
712 /* XXX check that it is, indeed, a syntax error */ 706 /* XXX check that it is, indeed, a syntax error. It might not
707 * be, though. */
713 tmp = PyInt_FromLong(lineno); 708 tmp = PyInt_FromLong(lineno);
714 if (tmp == NULL) 709 if (tmp == NULL)
715 PyErr_Clear(); 710 PyErr_Clear();
716 else { 711 else {
717 if (PyObject_SetAttrString(v, "lineno", tmp)) 712 if (PyObject_SetAttrString(v, "lineno", tmp))