@@ -50,6 +50,7 @@ _Py_IDENTIFIER(ps2);
5050_Py_IDENTIFIER (last_type );
5151_Py_IDENTIFIER (last_value );
5252_Py_IDENTIFIER (last_traceback );
53+ _Py_static_string (PyId_string , "<string>" );
5354
5455#ifdef Py_REF_DEBUG
5556static
@@ -1625,8 +1626,8 @@ PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
16251626}
16261627
16271628static int
1628- parse_syntax_error (PyObject * err , PyObject * * message , const char * * filename ,
1629- int * lineno , int * offset , const char * * text )
1629+ parse_syntax_error (PyObject * err , PyObject * * message , PyObject * * filename ,
1630+ int * lineno , int * offset , PyObject * * text )
16301631{
16311632 long hold ;
16321633 PyObject * v ;
@@ -1637,6 +1638,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
16371638 _Py_IDENTIFIER (text );
16381639
16391640 * message = NULL ;
1641+ * filename = NULL ;
16401642
16411643 /* new style errors. `err' is an instance */
16421644 * message = _PyObject_GetAttrId (err , & PyId_msg );
@@ -1648,13 +1650,13 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
16481650 goto finally ;
16491651 if (v == Py_None ) {
16501652 Py_DECREF (v );
1651- * filename = NULL ;
1653+ * filename = _PyUnicode_FromId (& PyId_string );
1654+ if (* filename == NULL )
1655+ goto finally ;
1656+ Py_INCREF (* filename );
16521657 }
16531658 else {
1654- * filename = _PyUnicode_AsString (v );
1655- Py_DECREF (v );
1656- if (!* filename )
1657- goto finally ;
1659+ * filename = v ;
16581660 }
16591661
16601662 v = _PyObject_GetAttrId (err , & PyId_lineno );
@@ -1688,15 +1690,13 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
16881690 * text = NULL ;
16891691 }
16901692 else {
1691- * text = _PyUnicode_AsString (v );
1692- Py_DECREF (v );
1693- if (!* text )
1694- goto finally ;
1693+ * text = v ;
16951694 }
16961695 return 1 ;
16971696
16981697finally :
16991698 Py_XDECREF (* message );
1699+ Py_XDECREF (* filename );
17001700 return 0 ;
17011701}
17021702
@@ -1707,9 +1707,15 @@ PyErr_Print(void)
17071707}
17081708
17091709static void
1710- print_error_text (PyObject * f , int offset , const char * text )
1710+ print_error_text (PyObject * f , int offset , PyObject * text_obj )
17111711{
1712+ char * text ;
17121713 char * nl ;
1714+
1715+ text = _PyUnicode_AsString (text_obj );
1716+ if (text == NULL )
1717+ return ;
1718+
17131719 if (offset >= 0 ) {
17141720 if (offset > 0 && offset == strlen (text ) && text [offset - 1 ] == '\n' )
17151721 offset -- ;
@@ -1880,27 +1886,30 @@ print_exception(PyObject *f, PyObject *value)
18801886 if (err == 0 &&
18811887 _PyObject_HasAttrId (value , & PyId_print_file_and_line ))
18821888 {
1883- PyObject * message ;
1884- const char * filename , * text ;
1889+ PyObject * message , * filename , * text ;
18851890 int lineno , offset ;
18861891 if (!parse_syntax_error (value , & message , & filename ,
18871892 & lineno , & offset , & text ))
18881893 PyErr_Clear ();
18891894 else {
1890- char buf [10 ];
1891- PyFile_WriteString (" File \"" , f );
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 );
1895+ PyObject * line ;
1896+
19021897 Py_DECREF (value );
19031898 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+
19041913 /* Can't be bothered to check all those
19051914 PyFile_WriteString() calls */
19061915 if (PyErr_Occurred ())
@@ -2061,7 +2070,6 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
20612070 PyObject * ret = NULL ;
20622071 mod_ty mod ;
20632072 PyArena * arena ;
2064- _Py_static_string (PyId_string , "<string>" );
20652073 PyObject * filename ;
20662074
20672075 filename = _PyUnicode_FromId (& PyId_string ); /* borrowed */
0 commit comments