bpo-32894: Support unparsing of infinity numbers in ast_unparser.c (GH-17426)

This commit is contained in:
Batuhan Taşkaya 2020-04-14 01:51:31 +03:00 committed by GitHub
parent 4c3da783cf
commit 258f5179f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 5 deletions

View file

@ -1,3 +1,4 @@
#include <float.h> /* DBL_MAX_10_EXP */
#include <stdbool.h>
#include "Python.h"
#include "Python-ast.h"
@ -6,6 +7,8 @@ static PyObject *_str_open_br;
static PyObject *_str_dbl_open_br;
static PyObject *_str_close_br;
static PyObject *_str_dbl_close_br;
static PyObject *_str_inf;
static PyObject *_str_replace_inf;
/* Forward declarations for recursion via helper functions. */
static PyObject *
@ -61,13 +64,28 @@ append_charp(_PyUnicodeWriter *writer, const char *charp)
static int
append_repr(_PyUnicodeWriter *writer, PyObject *obj)
{
int ret;
PyObject *repr;
repr = PyObject_Repr(obj);
PyObject *repr = PyObject_Repr(obj);
if (!repr) {
return -1;
}
ret = _PyUnicodeWriter_WriteStr(writer, repr);
if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) ||
PyComplex_CheckExact(obj))
{
PyObject *new_repr = PyUnicode_Replace(
repr,
_str_inf,
_str_replace_inf,
-1
);
Py_DECREF(repr);
if (!new_repr) {
return -1;
}
repr = new_repr;
}
int ret = _PyUnicodeWriter_WriteStr(writer, repr);
Py_DECREF(repr);
return ret;
}
@ -697,6 +715,28 @@ append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e)
APPEND_STR_FINISH("}");
}
static int
append_ast_constant(_PyUnicodeWriter *writer, PyObject *constant)
{
if (PyTuple_CheckExact(constant)) {
Py_ssize_t i, elem_count;
elem_count = PyTuple_GET_SIZE(constant);
APPEND_STR("(");
for (i = 0; i < elem_count; i++) {
APPEND_STR_IF(i > 0, ", ");
if (append_ast_constant(writer, PyTuple_GET_ITEM(constant, i)) < 0) {
return -1;
}
}
APPEND_STR_IF(elem_count == 1, ",");
APPEND_STR(")");
return 0;
}
return append_repr(writer, constant);
}
static int
append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e)
{
@ -835,7 +875,7 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
if (e->v.Constant.value == Py_Ellipsis) {
APPEND_STR_FINISH("...");
}
return append_repr(writer, e->v.Constant.value);
return append_ast_constant(writer, e->v.Constant.value);
case JoinedStr_kind:
return append_joinedstr(writer, e, false);
case FormattedValue_kind:
@ -883,6 +923,14 @@ maybe_init_static_strings(void)
!(_str_dbl_close_br = PyUnicode_InternFromString("}}"))) {
return -1;
}
if (!_str_inf &&
!(_str_inf = PyUnicode_FromString("inf"))) {
return -1;
}
if (!_str_replace_inf &&
!(_str_replace_inf = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP))) {
return -1;
}
return 0;
}