summaryrefslogtreecommitdiff
path: root/numpy/core/tests
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2020-03-16 18:32:19 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2020-03-16 18:41:29 -0500
commit769c68b037972200da70cd3846992b47798466eb (patch)
treee6036d429a1e333e09d6b55e9d41715d16e3ba5c /numpy/core/tests
parentff4cfe7ecd46ee15fd88297964f6a5cb5423c291 (diff)
downloadnumpy-769c68b037972200da70cd3846992b47798466eb.tar.gz
BUG,MAINT: Remove incorrect special case in string to number casts
The string to number casts fall back to using the scalars and the type setitem function to do the cast. However, before calling setitem, they sometimes already called the Python function for string coercion. This is unnecessary. Closes gh-15608
Diffstat (limited to 'numpy/core/tests')
-rw-r--r--numpy/core/tests/test_longdouble.py34
1 files changed, 24 insertions, 10 deletions
diff --git a/numpy/core/tests/test_longdouble.py b/numpy/core/tests/test_longdouble.py
index bf12f0e1b..acef995f3 100644
--- a/numpy/core/tests/test_longdouble.py
+++ b/numpy/core/tests/test_longdouble.py
@@ -37,22 +37,36 @@ def test_repr_roundtrip():
assert_equal(np.longdouble(repr(o)), o, "repr was %s" % repr(o))
-def test_unicode():
- np.longdouble(u"1.2")
+@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
+def test_repr_roundtrip_bytes():
+ o = 1 + LD_INFO.eps
+ assert_equal(np.longdouble(repr(o).encode("ascii")), o)
-def test_string():
- np.longdouble("1.2")
+@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
+@pytest.mark.parametrize("strtype", (np.str_, np.bytes_, str, bytes))
+def test_array_and_stringlike_roundtrip(strtype):
+ """
+ Test that string representations of long-double roundtrip both
+ for array casting and scalar coercion, see also gh-15608.
+ """
+ o = 1 + LD_INFO.eps
+ if strtype in (np.bytes_, bytes):
+ o_str = strtype(repr(o).encode("ascii"))
+ else:
+ o_str = strtype(repr(o))
-def test_bytes():
- np.longdouble(b"1.2")
+ # Test that `o` is correctly coerced from the string-like
+ assert o == np.longdouble(o_str)
+ # Test that arrays also roundtrip correctly:
+ o_strarr = np.asarray([o] * 3, dtype=strtype)
+ assert (o == o_strarr.astype(np.longdouble)).all()
-@pytest.mark.skipif(string_to_longdouble_inaccurate, reason="Need strtold_l")
-def test_repr_roundtrip_bytes():
- o = 1 + LD_INFO.eps
- assert_equal(np.longdouble(repr(o).encode("ascii")), o)
+ # And array coercion and casting to string give the same as scalar repr:
+ assert (o_strarr == o_str).all()
+ assert (np.asarray([o] * 3).astype(strtype) == o_str).all()
def test_bogus_string():