1+ #include <float.h> /* DBL_MAX_10_EXP */
12#include <stdbool.h>
23#include "Python.h"
34#include "Python-ast.h"
@@ -6,6 +7,8 @@ static PyObject *_str_open_br;
67static PyObject * _str_dbl_open_br ;
78static PyObject * _str_close_br ;
89static PyObject * _str_dbl_close_br ;
10+ static PyObject * _str_inf ;
11+ static PyObject * _str_replace_inf ;
912
1013/* Forward declarations for recursion via helper functions. */
1114static PyObject *
@@ -61,13 +64,28 @@ append_charp(_PyUnicodeWriter *writer, const char *charp)
6164static int
6265append_repr (_PyUnicodeWriter * writer , PyObject * obj )
6366{
64- int ret ;
65- PyObject * repr ;
66- repr = PyObject_Repr (obj );
67+ PyObject * repr = PyObject_Repr (obj );
68+
6769 if (!repr ) {
6870 return -1 ;
6971 }
70- ret = _PyUnicodeWriter_WriteStr (writer , repr );
72+
73+ if ((PyFloat_CheckExact (obj ) && Py_IS_INFINITY (PyFloat_AS_DOUBLE (obj ))) ||
74+ PyComplex_CheckExact (obj ))
75+ {
76+ PyObject * new_repr = PyUnicode_Replace (
77+ repr ,
78+ _str_inf ,
79+ _str_replace_inf ,
80+ -1
81+ );
82+ Py_DECREF (repr );
83+ if (!new_repr ) {
84+ return -1 ;
85+ }
86+ repr = new_repr ;
87+ }
88+ int ret = _PyUnicodeWriter_WriteStr (writer , repr );
7189 Py_DECREF (repr );
7290 return ret ;
7391}
@@ -697,6 +715,28 @@ append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e)
697715 APPEND_STR_FINISH ("}" );
698716}
699717
718+ static int
719+ append_ast_constant (_PyUnicodeWriter * writer , PyObject * constant )
720+ {
721+ if (PyTuple_CheckExact (constant )) {
722+ Py_ssize_t i , elem_count ;
723+
724+ elem_count = PyTuple_GET_SIZE (constant );
725+ APPEND_STR ("(" );
726+ for (i = 0 ; i < elem_count ; i ++ ) {
727+ APPEND_STR_IF (i > 0 , ", " );
728+ if (append_ast_constant (writer , PyTuple_GET_ITEM (constant , i )) < 0 ) {
729+ return -1 ;
730+ }
731+ }
732+
733+ APPEND_STR_IF (elem_count == 1 , "," );
734+ APPEND_STR (")" );
735+ return 0 ;
736+ }
737+ return append_repr (writer , constant );
738+ }
739+
700740static int
701741append_ast_attribute (_PyUnicodeWriter * writer , expr_ty e )
702742{
@@ -835,7 +875,7 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
835875 if (e -> v .Constant .value == Py_Ellipsis ) {
836876 APPEND_STR_FINISH ("..." );
837877 }
838- return append_repr (writer , e -> v .Constant .value );
878+ return append_ast_constant (writer , e -> v .Constant .value );
839879 case JoinedStr_kind :
840880 return append_joinedstr (writer , e , false);
841881 case FormattedValue_kind :
@@ -883,6 +923,14 @@ maybe_init_static_strings(void)
883923 !(_str_dbl_close_br = PyUnicode_InternFromString ("}}" ))) {
884924 return -1 ;
885925 }
926+ if (!_str_inf &&
927+ !(_str_inf = PyUnicode_FromString ("inf" ))) {
928+ return -1 ;
929+ }
930+ if (!_str_replace_inf &&
931+ !(_str_replace_inf = PyUnicode_FromFormat ("1e%d" , 1 + DBL_MAX_10_EXP ))) {
932+ return -1 ;
933+ }
886934 return 0 ;
887935}
888936
0 commit comments