Skip to content

Commit bbcaf73

Browse files
authored
Merge pull request numpy#26027 from ngoldbaum/fix-stringdtype-cast-error-handling
BUG: add missing error handling in string to int cast internals
2 parents 020e847 + 3c7af2a commit bbcaf73

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

numpy/_core/src/multiarray/stringdtype/casts.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ string_to_pylong(char *in, int has_null,
571571
{
572572
PyObject *val_obj = non_nullable_string_to_pystring(
573573
in, has_null, default_string, allocator);
574+
if (val_obj == NULL) {
575+
return NULL;
576+
}
574577
// interpret as an integer in base 10
575578
PyObject *pylong_value = PyLong_FromUnicodeObject(val_obj, 10);
576579
Py_DECREF(val_obj);

numpy/_core/tests/test_stringdtype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ def test_sized_integer_casts(bitsize, signed):
546546
with pytest.raises(OverflowError):
547547
np.array(oob, dtype="T").astype(idtype)
548548

549+
with pytest.raises(ValueError):
550+
np.array(["1", np.nan, "3"],
551+
dtype=StringDType(na_object=np.nan)).astype(idtype)
552+
549553

550554
@pytest.mark.parametrize("typename", ["byte", "short", "int", "longlong"])
551555
@pytest.mark.parametrize("signed", ["", "u"])

0 commit comments

Comments
 (0)