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 [
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.
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);
}