Skip to content

Commit 258f517

Browse files
authored
bpo-32894: Support unparsing of infinity numbers in ast_unparser.c (pythonGH-17426)
1 parent 4c3da78 commit 258f517

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

Lib/test/test_future.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from textwrap import dedent
66
import os
77
import re
8+
import sys
89

910
rx = re.compile(r'\((\S+).py, line (\d+)')
1011

@@ -308,5 +309,18 @@ def test_fstring_debug_annotations(self):
308309
self.assertAnnotationEqual("f'{x=!a}'", expected="f'x={x!a}'")
309310
self.assertAnnotationEqual("f'{x=!s:*^20}'", expected="f'x={x!s:*^20}'")
310311

312+
def test_infinity_numbers(self):
313+
inf = "1e" + repr(sys.float_info.max_10_exp + 1)
314+
infj = f"{inf}j"
315+
self.assertAnnotationEqual("1e1000", expected=inf)
316+
self.assertAnnotationEqual("1e1000j", expected=infj)
317+
self.assertAnnotationEqual("-1e1000", expected=f"-{inf}")
318+
self.assertAnnotationEqual("3+1e1000j", expected=f"3 + {infj}")
319+
self.assertAnnotationEqual("(1e1000, 1e1000j)", expected=f"({inf}, {infj})")
320+
self.assertAnnotationEqual("'inf'")
321+
self.assertAnnotationEqual("('inf', 1e1000, 'infxxx', 1e1000j)", expected=f"('inf', {inf}, 'infxxx', {infj})")
322+
self.assertAnnotationEqual("(1e1000, (1e1000j,))", expected=f"({inf}, ({infj},))")
323+
324+
311325
if __name__ == "__main__":
312326
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support unparsing of infinity numbers in postponed annotations. Patch by Batuhan Taşkaya.

Python/ast_unparse.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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;
67
static PyObject *_str_dbl_open_br;
78
static PyObject *_str_close_br;
89
static 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. */
1114
static PyObject *
@@ -61,13 +64,28 @@ append_charp(_PyUnicodeWriter *writer, const char *charp)
6164
static int
6265
append_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+
700740
static int
701741
append_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

Comments
 (0)