diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-04-15 21:34:27 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-04-15 21:34:27 (GMT) |
commit | 2808d3c41847ba1f8e07ee678742b6f12528face (patch) | |
tree | ce0ca8f56fa2bcba75bae34c0eca30cb54505dd4 | |
parent | 50a14695573807aebdeea1c7c057b816b1bbd4a3 (diff) | |
download | cpython-2808d3c41847ba1f8e07ee678742b6f12528face.zip cpython-2808d3c41847ba1f8e07ee678742b6f12528face.tar.gz cpython-2808d3c41847ba1f8e07ee678742b6f12528face.tar.bz2 |
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
........
-rw-r--r-- | Lib/test/test_float.py | 7 | ||||
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Objects/floatobject.c | 4 |
3 files changed, 12 insertions, 1 deletions
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 2d3b357..123a69d 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -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 [ @@ -12,6 +12,8 @@ What's New in Python 3.1 beta 1? Core and Builtins ----------------- +- Issue #5759: float() didn't call __float__ on str subclasses. + - The string.maketrans() function is deprecated; there is a new static method maketrans() on the bytes and bytearray classes. This removes confusion about the types string.maketrans() is supposed to work with, and mirrors the diff --git a/Objects/floatobject.c b/Objects/floatobject.c index e77b2dc..2ef4d1a 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -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); } |