Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 86984:7c71d2f97196
print_error_text() doesn't encode the filename anymore
Use aslo PyUnicode_FromFormat() to format the line so only one call to
PyFile_WriteObject() is needed. tb_displayline() of Python/traceback.c has
similar implementation.
| author | Victor Stinner <victor.stinner@gmail.com> |
|---|---|
| date | Thu, 07 Nov 2013 12:37:56 +0100 |
| parents | 77bebcf5c4cf |
| children | 01c4a0af73cf |
comparison
equal
deleted
inserted
replaced
| 86983:89873f3b18fd | 86984:7c71d2f97196 |
|---|---|
| 48 _Py_IDENTIFIER(ps1); | 48 _Py_IDENTIFIER(ps1); |
| 49 _Py_IDENTIFIER(ps2); | 49 _Py_IDENTIFIER(ps2); |
| 50 _Py_IDENTIFIER(last_type); | 50 _Py_IDENTIFIER(last_type); |
| 51 _Py_IDENTIFIER(last_value); | 51 _Py_IDENTIFIER(last_value); |
| 52 _Py_IDENTIFIER(last_traceback); | 52 _Py_IDENTIFIER(last_traceback); |
| 53 _Py_static_string(PyId_string, "<string>"); | |
| 53 | 54 |
| 54 #ifdef Py_REF_DEBUG | 55 #ifdef Py_REF_DEBUG |
| 55 static | 56 static |
| 56 void _print_total_refs(void) { | 57 void _print_total_refs(void) { |
| 57 PyObject *xoptions, *value; | 58 PyObject *xoptions, *value; |
| 1623 Py_DECREF(v); | 1624 Py_DECREF(v); |
| 1624 return 0; | 1625 return 0; |
| 1625 } | 1626 } |
| 1626 | 1627 |
| 1627 static int | 1628 static int |
| 1628 parse_syntax_error(PyObject *err, PyObject **message, const char **filename, | 1629 parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename, |
| 1629 int *lineno, int *offset, const char **text) | 1630 int *lineno, int *offset, PyObject **text) |
| 1630 { | 1631 { |
| 1631 long hold; | 1632 long hold; |
| 1632 PyObject *v; | 1633 PyObject *v; |
| 1633 _Py_IDENTIFIER(msg); | 1634 _Py_IDENTIFIER(msg); |
| 1634 _Py_IDENTIFIER(filename); | 1635 _Py_IDENTIFIER(filename); |
| 1635 _Py_IDENTIFIER(lineno); | 1636 _Py_IDENTIFIER(lineno); |
| 1636 _Py_IDENTIFIER(offset); | 1637 _Py_IDENTIFIER(offset); |
| 1637 _Py_IDENTIFIER(text); | 1638 _Py_IDENTIFIER(text); |
| 1638 | 1639 |
| 1639 *message = NULL; | 1640 *message = NULL; |
| 1641 *filename = NULL; | |
| 1640 | 1642 |
| 1641 /* new style errors. `err' is an instance */ | 1643 /* new style errors. `err' is an instance */ |
| 1642 *message = _PyObject_GetAttrId(err, &PyId_msg); | 1644 *message = _PyObject_GetAttrId(err, &PyId_msg); |
| 1643 if (!*message) | 1645 if (!*message) |
| 1644 goto finally; | 1646 goto finally; |
| 1646 v = _PyObject_GetAttrId(err, &PyId_filename); | 1648 v = _PyObject_GetAttrId(err, &PyId_filename); |
| 1647 if (!v) | 1649 if (!v) |
| 1648 goto finally; | 1650 goto finally; |
| 1649 if (v == Py_None) { | 1651 if (v == Py_None) { |
| 1650 Py_DECREF(v); | 1652 Py_DECREF(v); |
| 1651 *filename = NULL; | 1653 *filename = _PyUnicode_FromId(&PyId_string); |
| 1654 if (*filename == NULL) | |
| 1655 goto finally; | |
| 1656 Py_INCREF(*filename); | |
| 1652 } | 1657 } |
| 1653 else { | 1658 else { |
| 1654 *filename = _PyUnicode_AsString(v); | 1659 *filename = v; |
| 1655 Py_DECREF(v); | |
| 1656 if (!*filename) | |
| 1657 goto finally; | |
| 1658 } | 1660 } |
| 1659 | 1661 |
| 1660 v = _PyObject_GetAttrId(err, &PyId_lineno); | 1662 v = _PyObject_GetAttrId(err, &PyId_lineno); |
| 1661 if (!v) | 1663 if (!v) |
| 1662 goto finally; | 1664 goto finally; |
| 1686 if (v == Py_None) { | 1688 if (v == Py_None) { |
| 1687 Py_DECREF(v); | 1689 Py_DECREF(v); |
| 1688 *text = NULL; | 1690 *text = NULL; |
| 1689 } | 1691 } |
| 1690 else { | 1692 else { |
| 1691 *text = _PyUnicode_AsString(v); | 1693 *text = v; |
| 1692 Py_DECREF(v); | |
| 1693 if (!*text) | |
| 1694 goto finally; | |
| 1695 } | 1694 } |
| 1696 return 1; | 1695 return 1; |
| 1697 | 1696 |
| 1698 finally: | 1697 finally: |
| 1699 Py_XDECREF(*message); | 1698 Py_XDECREF(*message); |
| 1699 Py_XDECREF(*filename); | |
| 1700 return 0; | 1700 return 0; |
| 1701 } | 1701 } |
| 1702 | 1702 |
| 1703 void | 1703 void |
| 1704 PyErr_Print(void) | 1704 PyErr_Print(void) |
| 1705 { | 1705 { |
| 1706 PyErr_PrintEx(1); | 1706 PyErr_PrintEx(1); |
| 1707 } | 1707 } |
| 1708 | 1708 |
| 1709 static void | 1709 static void |
| 1710 print_error_text(PyObject *f, int offset, const char *text) | 1710 print_error_text(PyObject *f, int offset, PyObject *text_obj) |
| 1711 { | 1711 { |
| 1712 char *text; | |
| 1712 char *nl; | 1713 char *nl; |
| 1714 | |
| 1715 text = _PyUnicode_AsString(text_obj); | |
| 1716 if (text == NULL) | |
| 1717 return; | |
| 1718 | |
| 1713 if (offset >= 0) { | 1719 if (offset >= 0) { |
| 1714 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n') | 1720 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n') |
| 1715 offset--; | 1721 offset--; |
| 1716 for (;;) { | 1722 for (;;) { |
| 1717 nl = strchr(text, '\n'); | 1723 nl = strchr(text, '\n'); |
| 1878 if (tb && tb != Py_None) | 1884 if (tb && tb != Py_None) |
| 1879 err = PyTraceBack_Print(tb, f); | 1885 err = PyTraceBack_Print(tb, f); |
| 1880 if (err == 0 && | 1886 if (err == 0 && |
| 1881 _PyObject_HasAttrId(value, &PyId_print_file_and_line)) | 1887 _PyObject_HasAttrId(value, &PyId_print_file_and_line)) |
| 1882 { | 1888 { |
| 1883 PyObject *message; | 1889 PyObject *message, *filename, *text; |
| 1884 const char *filename, *text; | |
| 1885 int lineno, offset; | 1890 int lineno, offset; |
| 1886 if (!parse_syntax_error(value, &message, &filename, | 1891 if (!parse_syntax_error(value, &message, &filename, |
| 1887 &lineno, &offset, &text)) | 1892 &lineno, &offset, &text)) |
| 1888 PyErr_Clear(); | 1893 PyErr_Clear(); |
| 1889 else { | 1894 else { |
| 1890 char buf[10]; | 1895 PyObject *line; |
| 1891 PyFile_WriteString(" File \"", f); | 1896 |
| 1892 if (filename == NULL) | |
| 1893 PyFile_WriteString("<string>", f); | |
| 1894 else | |
| 1895 PyFile_WriteString(filename, f); | |
| 1896 PyFile_WriteString("\", line ", f); | |
| 1897 PyOS_snprintf(buf, sizeof(buf), "%d", lineno); | |
| 1898 PyFile_WriteString(buf, f); | |
| 1899 PyFile_WriteString("\n", f); | |
| 1900 if (text != NULL) | |
| 1901 print_error_text(f, offset, text); | |
| 1902 Py_DECREF(value); | 1897 Py_DECREF(value); |
| 1903 value = message; | 1898 value = message; |
| 1899 | |
| 1900 line = PyUnicode_FromFormat(" File \"%U\", line %d\n", | |
| 1901 filename, lineno); | |
| 1902 Py_DECREF(filename); | |
| 1903 if (line != NULL) { | |
| 1904 PyFile_WriteObject(line, f, Py_PRINT_RAW); | |
| 1905 Py_DECREF(line); | |
| 1906 } | |
| 1907 | |
| 1908 if (text != NULL) { | |
| 1909 print_error_text(f, offset, text); | |
| 1910 Py_DECREF(text); | |
| 1911 } | |
| 1912 | |
| 1904 /* Can't be bothered to check all those | 1913 /* Can't be bothered to check all those |
| 1905 PyFile_WriteString() calls */ | 1914 PyFile_WriteString() calls */ |
| 1906 if (PyErr_Occurred()) | 1915 if (PyErr_Occurred()) |
| 1907 err = -1; | 1916 err = -1; |
| 1908 } | 1917 } |
| 2059 PyObject *locals, PyCompilerFlags *flags) | 2068 PyObject *locals, PyCompilerFlags *flags) |
| 2060 { | 2069 { |
| 2061 PyObject *ret = NULL; | 2070 PyObject *ret = NULL; |
| 2062 mod_ty mod; | 2071 mod_ty mod; |
| 2063 PyArena *arena; | 2072 PyArena *arena; |
| 2064 _Py_static_string(PyId_string, "<string>"); | |
| 2065 PyObject *filename; | 2073 PyObject *filename; |
| 2066 | 2074 |
| 2067 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */ | 2075 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */ |
| 2068 if (filename == NULL) | 2076 if (filename == NULL) |
| 2069 return NULL; | 2077 return NULL; |
