[Python-checkins] (no subject)

Batuhan Taşkaya webhook-mailer at python.org
Mon Apr 13 18:51:38 EDT 2020




To: python-checkins at python.org
Subject:
 bpo-32894: Support unparsing of infinity numbers in ast_unparser.c (GH-17426)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/258f5179f9494189e6a80311c86981d2a88b=
a9d6
commit: 258f5179f9494189e6a80311c86981d2a88ba9d6
branch: master
author: Batuhan Ta=C5=9Fkaya <batuhanosmantaskaya at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-04-14T00:51:31+02:00
summary:

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

files:
A Misc/NEWS.d/next/Core and Builtins/2019-12-01-21-36-49.bpo-32894.5g_UQr.rst
M Lib/test/test_future.py
M Python/ast_unparse.c

diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py
index 9b88e3f464252..ebeb833d7e25b 100644
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -5,6 +5,7 @@
 from textwrap import dedent
 import os
 import re
+import sys
=20
 rx =3D re.compile(r'\((\S+).py, line (\d+)')
=20
@@ -308,5 +309,18 @@ def test_fstring_debug_annotations(self):
         self.assertAnnotationEqual("f'{x=3D!a}'", expected=3D"f'x=3D{x!a}'")
         self.assertAnnotationEqual("f'{x=3D!s:*^20}'", expected=3D"f'x=3D{x!=
s:*^20}'")
=20
+    def test_infinity_numbers(self):
+        inf =3D "1e" + repr(sys.float_info.max_10_exp + 1)
+        infj =3D f"{inf}j"
+        self.assertAnnotationEqual("1e1000", expected=3Dinf)
+        self.assertAnnotationEqual("1e1000j", expected=3Dinfj)
+        self.assertAnnotationEqual("-1e1000", expected=3Df"-{inf}")
+        self.assertAnnotationEqual("3+1e1000j", expected=3Df"3 + {infj}")
+        self.assertAnnotationEqual("(1e1000, 1e1000j)", expected=3Df"({inf},=
 {infj})")
+        self.assertAnnotationEqual("'inf'")
+        self.assertAnnotationEqual("('inf', 1e1000, 'infxxx', 1e1000j)", exp=
ected=3Df"('inf', {inf}, 'infxxx', {infj})")
+        self.assertAnnotationEqual("(1e1000, (1e1000j,))", expected=3Df"({in=
f}, ({infj},))")
+
+
 if __name__ =3D=3D "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-01-21-36-49.bpo-32894=
.5g_UQr.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-21-36-49.bpo-3289=
4.5g_UQr.rst
new file mode 100644
index 0000000000000..68f4e6774a3b1
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-01-21-36-49.bpo-32894.5g_UQr=
.rst=09
@@ -0,0 +1 @@
+Support unparsing of infinity numbers in postponed annotations. Patch by Bat=
uhan Ta=C5=9Fkaya.
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c
index 5ecd1b0fef5ba..7cf199b1b42d9 100644
--- a/Python/ast_unparse.c
+++ b/Python/ast_unparse.c
@@ -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;
=20
 /* 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 =3D PyObject_Repr(obj);
+    PyObject *repr =3D PyObject_Repr(obj);
+
     if (!repr) {
         return -1;
     }
-    ret =3D _PyUnicodeWriter_WriteStr(writer, repr);
+
+    if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) =
||
+       PyComplex_CheckExact(obj))
+    {
+        PyObject *new_repr =3D PyUnicode_Replace(
+            repr,
+            _str_inf,
+            _str_replace_inf,
+            -1
+        );
+        Py_DECREF(repr);
+        if (!new_repr) {
+            return -1;
+        }
+        repr =3D new_repr;
+    }
+    int ret =3D _PyUnicodeWriter_WriteStr(writer, repr);
     Py_DECREF(repr);
     return ret;
 }
@@ -697,6 +715,28 @@ append_formattedvalue(_PyUnicodeWriter *writer, expr_ty =
e)
     APPEND_STR_FINISH("}");
 }
=20
+static int
+append_ast_constant(_PyUnicodeWriter *writer, PyObject *constant)
+{
+    if (PyTuple_CheckExact(constant)) {
+        Py_ssize_t i, elem_count;
+
+        elem_count =3D PyTuple_GET_SIZE(constant);
+        APPEND_STR("(");
+        for (i =3D 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 =3D=3D 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 =3D=3D 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 =3D PyUnicode_InternFromString("}}"))) {
         return -1;
     }
+    if (!_str_inf &&
+        !(_str_inf =3D PyUnicode_FromString("inf"))) {
+        return -1;
+    }
+    if (!_str_replace_inf &&
+        !(_str_replace_inf =3D PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_E=
XP))) {
+        return -1;
+    }
     return 0;
 }
=20



More information about the Python-checkins mailing list