summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_io.py
diff options
context:
space:
mode:
authorAntony Lee <anntzer.lee@gmail.com>2021-08-05 12:20:12 +0200
committerGitHub <noreply@github.com>2021-08-05 13:20:12 +0300
commitd97e31e891e868457d710d53b8ddadb0d473ec8a (patch)
tree3515f22bb31a111ae029a9c5a5384b379f46ffca /numpy/lib/tests/test_io.py
parentb3e3567544dc2b41e1bcc89157b977cf12ef2efb (diff)
downloadnumpy-d97e31e891e868457d710d53b8ddadb0d473ec8a.tar.gz
PERF: Speed-up common case of loadtxt()ing non-hex floats. (#19598)
* PERF: Speed-up common case of loadtxt()ing non-hex floats. `python runtests.py --bench bench_io` reports a ~5-10% perf gain. * TST: Add tests to check fromhex not called unintentionally. Adds regression tests to check that the logic surrounding when the default floatconv applies the fromhex conversion does not change. Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
Diffstat (limited to 'numpy/lib/tests/test_io.py')
-rw-r--r--numpy/lib/tests/test_io.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 534ab683c..d97ad76df 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -984,6 +984,28 @@ class TestLoadTxt(LoadTxtBase):
res = np.loadtxt(c, dtype=dt)
assert_equal(res, tgt, err_msg="%s" % dt)
+ def test_default_float_converter_no_default_hex_conversion(self):
+ """
+ Ensure that fromhex is only used for values with the correct prefix and
+ is not called by default. Regression test related to gh-19598.
+ """
+ c = TextIO("a b c")
+ with pytest.raises(
+ ValueError, match="could not convert string to float"
+ ):
+ np.loadtxt(c)
+
+ def test_default_float_converter_exception(self):
+ """
+ Ensure that the exception message raised during failed floating point
+ conversion is correct. Regression test related to gh-19598.
+ """
+ c = TextIO("qrs tuv") # Invalid values for default float converter
+ with pytest.raises(
+ ValueError, match="could not convert string to float"
+ ):
+ np.loadtxt(c)
+
def test_from_complex(self):
tgt = (complex(1, 1), complex(1, -1))
c = TextIO()