Skip to content

Commit 8d0efca

Browse files
TST: integer overflow on parsing with insufficient user dtype
squash! TST: integer overflow on parsing with insufficient user dtype
1 parent c355145 commit 8d0efca

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pandas/tests/io/parser/test_textreader.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import pytest
1212

1313
import pandas._libs.parsers as parser
14-
from pandas._libs.parsers import TextReader
14+
from pandas._libs.parsers import TextReader, is_extension_array_dtype
1515

16-
from pandas import DataFrame
16+
from pandas import DataFrame, array
1717
import pandas._testing as tm
1818

1919
from pandas.io.parsers import (
@@ -125,6 +125,30 @@ def test_integer_thousands_alt(self):
125125
expected = DataFrame([123456, 12500])
126126
tm.assert_frame_equal(result, expected)
127127

128+
@pytest.mark.parametrize(
129+
"dtype", [
130+
"uint64", "int64", "uint32", "int32", "uint16", "int16", "uint8", "int8",
131+
"UInt64", "Int64", "UInt32", "Int32", "UInt16", "Int16", "UInt8", "Int8"
132+
]
133+
)
134+
def test_integer_overflow_with_user_dtype(self, dtype):
135+
dtype = ensure_dtype_objs(dtype)
136+
is_ext_dtype = is_extension_array_dtype(dtype)
137+
maxint = np.iinfo(dtype.type if is_ext_dtype else dtype).max
138+
139+
reader = TextReader(StringIO(f"{maxint}"), header=None, dtype=dtype)
140+
result = reader.read()
141+
if is_ext_dtype:
142+
expected = array([maxint], dtype=dtype)
143+
tm.assert_extension_array_equal(result[0], expected)
144+
else:
145+
expected = np.array([maxint], dtype=dtype)
146+
tm.assert_numpy_array_equal(result[0], expected)
147+
148+
reader = TextReader(StringIO(f"{maxint + 1}"), header=None, dtype=dtype)
149+
with pytest.raises(Exception):
150+
reader.read()
151+
128152
def test_skip_bad_lines(self, capsys):
129153
# too many lines, see #2430 for why
130154
data = "a:b:c\nd:e:f\ng:h:i\nj:k:l:m\nl:m:n\no:p:q:r"

0 commit comments

Comments
 (0)