From da8652d920210a5ff783a7ad30f4c24d2b5ff2a2 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 24 Oct 2009 14:01:08 +0000 Subject: Issue #7117 (backport py3k float repr) continued: Add sys.float_repr_style attribute ('short' if short float repr is in used; 'legacy' otherwise). --- Doc/library/sys.rst | 12 ++++++++++++ Lib/test/test_sys.py | 2 ++ Python/sysmodule.c | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 8264a39..5a04f39 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -332,6 +332,18 @@ always available. .. versionadded:: 2.6 +.. data:: float_repr_style + + A string indicating how the :func:`repr` function behaves for + floats. If the string has value ``'short'`` then for a finite + float ``x``, ``repr(x)`` aims to produce a short string with the + property that ``float(repr(x)) == x``. This is the usual behaviour + in Python 2.7 and later. Otherwise, ``float_repr_style`` has value + ``'legacy'`` and ``repr(x)`` behaves in the same way as it did in + versions of Python prior to 2.7. + + .. versionadded:: 2.7 + .. function:: getcheckinterval() diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 0e364af..1d0e2ef 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -369,6 +369,8 @@ class SysModuleTest(unittest.TestCase): self.assertEqual(vi[3], vi.releaselevel) self.assertEqual(vi[4], vi.serial) self.assertTrue(vi > (1,0,0)) + self.assertIsInstance(sys.float_repr_style, str) + self.assertTrue(sys.float_repr_style in ('short', 'legacy')) def test_43581(self): # Can't use sys.stdout, as this is a cStringIO object when diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 86e0feb..c9bc4a3 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1056,6 +1056,7 @@ platform -- platform identifier\n\ executable -- pathname of this Python interpreter\n\ prefix -- prefix used to find the Python library\n\ exec_prefix -- prefix used to find the machine-specific Python library\n\ +float_repr_style -- string indicating the style of repr() output for floats\n\ " ) #ifdef MS_WINDOWS @@ -1498,6 +1499,15 @@ _PySys_Init(void) FlagsType.tp_init = NULL; FlagsType.tp_new = NULL; + /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ +#ifndef PY_NO_SHORT_FLOAT_REPR + SET_SYS_FROM_STRING("float_repr_style", + PyString_FromString("short")); +#else + SET_SYS_FROM_STRING("float_repr_style", + PyString_FromString("legacy")); +#endif + #undef SET_SYS_FROM_STRING if (PyErr_Occurred()) return NULL; -- cgit v0.12