diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_longdouble.py | 22 |
2 files changed, 26 insertions, 5 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 53dfe7ca7..c06a1edaa 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -1868,10 +1868,17 @@ static int output.real = result; // Reading imaginary component + char **prev = endptr; str = *endptr; result = NumPyOS_ascii_strtod(str, endptr); - output.imag = result; + if (endptr && *endptr[0] == 'j') { + output.imag = result; + } + else { + endptr = prev; + output.imag = 0; + } // Skip j ++*endptr; diff --git a/numpy/core/tests/test_longdouble.py b/numpy/core/tests/test_longdouble.py index 8e1c9d153..b9652cc98 100644 --- a/numpy/core/tests/test_longdouble.py +++ b/numpy/core/tests/test_longdouble.py @@ -72,16 +72,30 @@ def test_fromstring(): def test_fromstring_complex(): - for ctypes in ["complex", "cdouble", "cfloat"]: + for ctype in ["complex", "cdouble", "cfloat"]: # Check spacing between separator - assert_equal(np.fromstring("1, 2 , 3 ,4",sep=",",dtype=ctypes), + assert_equal(np.fromstring("1, 2 , 3 ,4",sep=",",dtype=ctype), np.array([1., 2., 3., 4.])) # Real component not specified - assert_equal(np.fromstring("1j, -2j, 3j, 4e1j",sep=",",dtype=ctypes), + assert_equal(np.fromstring("1j, -2j, 3j, 4e1j",sep=",",dtype=ctype), np.array([1.j, -2.j, 3.j, 40.j])) # Both components specified - assert_equal(np.fromstring("1+1j,2-2j, -3+3j, -4e1", sep=",", dtype=ctypes), + assert_equal(np.fromstring("1+1j,2-2j, -3+3j, -4e1", sep=",", dtype=ctype), np.array([1. + 1.j, 2. - 2.j, - 3. + 3.j, - 40.])) + # Spaces at wrong places + with assert_warns(DeprecationWarning): + assert_equal(np.fromstring("1+2 j,3", dtype=ctype, sep=","), + np.array([1.])) + with assert_warns(DeprecationWarning): + assert_equal(np.fromstring("1+ 2j,3", dtype=ctype, sep=","), + np.array([1.])) + with assert_warns(DeprecationWarning): + assert_equal(np.fromstring("1 +2j,3", dtype=ctype, sep=","), + np.array([1.])) + with assert_warns(DeprecationWarning): + assert_equal(np.fromstring("1+j", dtype=ctype, sep=","), + np.array([1.])) + def test_fromstring_bogus(): |