]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 71628 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Wed, 15 Apr 2009 21:40:50 +0000 (21:40 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 15 Apr 2009 21:40:50 +0000 (21:40 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r71628 | benjamin.peterson | 2009-04-15 16:34:27 -0500 (Wed, 15 Apr 2009) | 11 lines

  Merged revisions 71627 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r71627 | benjamin.peterson | 2009-04-15 16:26:36 -0500 (Wed, 15 Apr 2009) | 4 lines

    call __float__ on str subclasses #5759

    tests by R. David Murray
  ........
................

Lib/test/test_float.py
Misc/NEWS
Objects/floatobject.c

index 2d3b3575ccbb5a4111d633b5ed4a62f33ff8c747..123a69d404d3eb190a09b5fa88bfd27e91c8b6ee 100644 (file)
@@ -78,11 +78,18 @@ class GeneralFloatCases(unittest.TestCase):
             def __float__(self):
                 return 42
 
+        # Issue 5759: __float__ not called on str subclasses (though it is on
+        # unicode subclasses).
+        class FooStr(str):
+            def __float__(self):
+                return float(str(self)) + 1
+
         self.assertAlmostEqual(float(Foo0()), 42.)
         self.assertAlmostEqual(float(Foo1()), 42.)
         self.assertAlmostEqual(float(Foo2()), 42.)
         self.assertAlmostEqual(float(Foo3(21)), 42.)
         self.assertRaises(TypeError, float, Foo4(42))
+        self.assertAlmostEqual(float(FooStr('8')), 9.)
 
     def test_floatasratio(self):
         for f, ratio in [
index 8d65ae4c9a9361b4e344e9c71706565594157203..356d36aa7646ab204cf389c1499694537837cd1e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,8 @@ Core and Builtins
   imp.find_module() were converted to UTF-8 while the path is
   converted to the default filesystem encoding, causing nonsense.
 
+- Issue #5759: float() didn't call __float__ on str subclasses.
+
 - Issue #5392: when a very low recursion limit was set, the interpreter would
   abort with a fatal error after the recursion limit was hit twice.
 
index e77b2dc855ef6c92d93ba675a0890bc7c6095dab..2ef4d1a865a1364e55523b99625f3d0f14e209b8 100644 (file)
@@ -1533,7 +1533,9 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                return float_subtype_new(type, args, kwds); /* Wimp out */
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
                return NULL;
-       if (PyUnicode_Check(x))
+       /* If it's a string, but not a string subclass, use
+          PyFloat_FromString. */
+       if (PyUnicode_CheckExact(x))
                return PyFloat_FromString(x);
        return PyNumber_Float(x);
 }