Author: Daniel Patrick <[email protected]>
Branch: py3.5
Changeset: r87368:0bf6794a3933
Date: 2016-09-24 21:20 +0100
http://bitbucket.org/pypy/pypy/changeset/0bf6794a3933/
Log: Make error messages for dict and other non-numeric types consistent
with CPython 3.5
diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -226,7 +226,8 @@
if e.match(space, space.w_TypeError):
raise oefmt(
space.w_TypeError,
- "float() argument must be a string or a number")
+ "float() argument must be a string or a number,\
+ not
'{}'".format(type(space.unwrap(w_value)).__name__))
raise
value = _string_to_float(space, w_value, value)
w_obj = space.allocate_instance(W_FloatObject, w_floattype)
diff --git a/pypy/objspace/std/test/test_floatobject.py
b/pypy/objspace/std/test/test_floatobject.py
--- a/pypy/objspace/std/test/test_floatobject.py
+++ b/pypy/objspace/std/test/test_floatobject.py
@@ -479,6 +479,45 @@
assert hash(-1.0) == -2
assert (-1.0).__hash__() == -2
+ def test_float_from_dict(self):
+ try:
+ float({})
+ except TypeError as e:
+ assert "not 'dict'" in str(e)
+ else:
+ assert False, 'did not raise'
+
+ def test_non_numeric_input_types(self):
+ # Test possible non-numeric types for the argument x, including
+ # subclasses of the explicitly documented accepted types.
+ class CustomStr(str): pass
+ class CustomBytes(bytes): pass
+ class CustomByteArray(bytearray): pass
+
+ factories = [
+ bytes,
+ bytearray,
+ lambda b: CustomStr(b.decode()),
+ CustomBytes,
+ CustomByteArray,
+ memoryview,
+ ]
+ try:
+ from array import array
+ except ImportError:
+ pass
+ else:
+ factories.append(lambda b: array('B', b))
+
+ for f in factories:
+ x = f(b" 3.14 ")
+ assert float(x) == 3.14
+ try:
+ float(f(b'A' * 0x10))
+ except ValueError as e:
+ assert "could not convert" in str(e)
+ else:
+ assert False, 'did not raise'
class AppTestFloatHex:
spaceconfig = {
diff --git a/rpython/rlib/rfloat.py b/rpython/rlib/rfloat.py
--- a/rpython/rlib/rfloat.py
+++ b/rpython/rlib/rfloat.py
@@ -25,7 +25,7 @@
globals().update(rffi_platform.configure(CConfig))
-INVALID_MSG = "invalid literal for float()"
+INVALID_MSG = "could not convert string to float"
def string_to_float(s):
"""
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit