Index: Include/floatobject.h =================================================================== --- Include/floatobject.h (revision 64515) +++ Include/floatobject.h (working copy) @@ -111,6 +111,8 @@ Py_UNICODE *format_spec, Py_ssize_t format_spec_len); +PyAPI_FUNC(PyObject *) _PyFloat_to_base(PyObject *v, int base); + #ifdef __cplusplus } #endif Index: Objects/abstract.c =================================================================== --- Objects/abstract.c (revision 64515) +++ Objects/abstract.c (working copy) @@ -1453,18 +1453,26 @@ PyObject *res = NULL; PyObject *index = PyNumber_Index(n); - if (!index) - return NULL; - if (PyLong_Check(index)) + if (index) { + assert(PyLong_Check(index)); res = _PyLong_Format(index, base); - else - /* It should not be possible to get here, as - PyNumber_Index already has a check for the same - condition */ - PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not " - "int or long"); - Py_DECREF(index); - return res; + Py_DECREF(index); + return res; + } + PyErr_Clear(); + if (PyFloat_Check(n)) + return _PyFloat_to_base(n, base); + if (PyObject_HasAttrString(n, "__float__")) { + PyObject *f = PyNumber_Float(n); + if (f == NULL) + return NULL; + res = _PyFloat_to_base(f, base); + Py_DECREF(f); + return res; + } + PyErr_SetString(PyExc_ValueError, + "Argument needs to support __index__() or __float__()."); + return NULL; } Index: Objects/floatobject.c =================================================================== --- Objects/floatobject.c (revision 64515) +++ Objects/floatobject.c (working copy) @@ -1113,7 +1113,44 @@ ">>> (-.25).as_integer_ratio()\n" "(-1, 4)"); +PyObject * +_PyFloat_to_base(PyObject *v, int base) +{ + PyObject *mant, *conv, *result; + double x, fr, fradj; + int i, exp, expadj; + if (!PyFloat_Check(v)) { + PyErr_BadInternalCall(); + return NULL; + } + CONVERT_TO_DOUBLE(v, x); + if (!Py_IS_FINITE(x)) + return PyObject_Repr(v); + fr = frexp(x, &exp); + if (fr != 0.0) { + fr *= 2.0; + exp--; + assert(1.0 <= fabs(fr) && fabs(fr) < 2.0); + } + expadj = (base == 16) ? 4 : ((base == 8) ? 3 : 1); + fradj = (float) base; + for (i=0; i<300 && fr != floor(fr) ; i++) { + fr *= fradj; + exp -= expadj; + } + mant = PyLong_FromDouble(floor(fr)); + if (mant == NULL) + return NULL; + conv = PyNumber_ToBase(mant, base); + Py_DECREF(mant); + if (conv == NULL) + return NULL; + result = PyUnicode_FromFormat("%U * 2.0 ** %d", conv, exp); + Py_DECREF(conv); + return result; +} + static PyObject * float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); Index: Doc/library/functions.rst =================================================================== --- Doc/library/functions.rst (revision 64450) +++ Doc/library/functions.rst (working copy) @@ -101,11 +101,10 @@ .. function:: bin(x) - Convert an integer number to a binary string. The result is a valid Python - expression. If *x* is not a Python :class:`int` object, it has to define an - :meth:`__index__` method that returns an integer. + Convert an integer or float to a binary string. + If *x* is not a Python :class:`int` object or a :class:`float` object, it + has to define an :meth:`__index__` or :meth:`__float__` method. - .. function:: bool([x]) Convert a value to a Boolean, using the standard truth testing procedure. If @@ -533,11 +532,10 @@ .. function:: hex(x) - Convert an integer number to a hexadecimal string. The result is a valid Python - expression. If *x* is not a Python :class:`int` object, it has to define an - :meth:`__index__` method that returns an integer. + Convert an integer or float to a hexadecimal string. + If *x* is not a Python :class:`int` object or a :class:`float` object, it + has to define an :meth:`__index__` or :meth:`__float__` method. - .. function:: id(object) Return the "identity" of an object. This is an integer which @@ -701,9 +699,9 @@ .. function:: oct(x) - Convert an integer number to an octal string. The result is a valid Python - expression. If *x* is not a Python :class:`int` object, it has to define an - :meth:`__index__` method that returns an integer. + Convert an integer or float to an octal string. + If *x* is not a Python :class:`int` object or a :class:`float` object, it + has to define an :meth:`__index__` or :meth:`__float__` method. .. function:: open(file[, mode='r'[, buffering=None[, encoding=None[, errors=None[, newline=None[, closefd=True]]]]]]) Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (revision 64515) +++ Lib/test/test_builtin.py (working copy) @@ -4,6 +4,7 @@ from test.support import fcmp, TESTFN, unlink, run_unittest, \ run_with_locale from operator import neg +from decimal import Decimal import sys, warnings, random, collections, io, fractions warnings.filterwarnings("ignore", "hex../oct.. of negative int", @@ -552,7 +553,18 @@ self.assertEqual(hex(16), '0x10') self.assertEqual(hex(-16), '-0x10') self.assertEqual(hex(-16), '-0x10') - self.assertRaises(TypeError, hex, {}) + self.assertRaises(ValueError, hex, {}) + self.assertEqual(hex(3.125), '0x19 * 2.0 ** -3') + self.assertEqual(hex(Decimal('3.125')), '0x19 * 2.0 ** -3') + self.assertEqual(hex(0.0), '0x0 * 2.0 ** 0') + for sv in float('nan'), float('inf'), float('-inf'): + self.assertEqual(hex(sv), repr(sv)) + for i in range(100): + x = random.expovariate(.05) + self.assertEqual(eval(hex(x)), x, (x, hex(x), eval(hex(x)))) + self.assertEqual(eval(hex(-x)), -x) + self.assertEqual(hex(-x), ('-' + hex(x))) + self.assert_('x1' in hex(x)) # check normalization def test_id(self): id(None) @@ -795,7 +807,18 @@ self.assertEqual(oct(100), '0o144') self.assertEqual(oct(-100), '-0o144') self.assertEqual(oct(-100), '-0o144') - self.assertRaises(TypeError, oct, ()) + self.assertRaises(ValueError, oct, ()) + self.assertEqual(oct(3.125), '0o144 * 2.0 ** -5') + self.assertEqual(oct(Decimal('3.125')), '0o144 * 2.0 ** -5') + self.assertEqual(oct(0.0), '0o0 * 2.0 ** 0') + for sv in float('nan'), float('inf'), float('-inf'): + self.assertEqual(oct(sv), repr(sv)) + for i in range(100): + x = random.expovariate(.05) + self.assertEqual(eval(oct(x)), x) + self.assertEqual(eval(oct(-x)), -x) + self.assertEqual(oct(-x), ('-' + oct(x))) + self.assert_('o1' in oct(x)) # check normalization def write_testfile(self): # NB the first 4 lines are also used to test input, below @@ -1213,6 +1236,16 @@ self.assertEqual(bin(2**65-1), '0b' + '1' * 65) self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65) self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65) + self.assertEqual(bin(3.125), '0b11001 * 2.0 ** -3') + self.assertEqual(bin(Decimal('3.125')), '0b11001 * 2.0 ** -3') + self.assertEqual(bin(0.0), '0b0 * 2.0 ** 0') + for sv in float('nan'), float('inf'), float('-inf'): + self.assertEqual(bin(sv), repr(sv)) + for i in range(100): + x = random.expovariate(.05) + self.assertEqual(eval(bin(x)), x) + self.assertEqual(eval(bin(-x)), -x) + self.assertEqual(bin(-x), ('-' + bin(x))) class TestSorted(unittest.TestCase):