Skip to content

Commit 2120719

Browse files
committed
A memory 'stress' test of parser.pyx to cause corruption or segfault
1 parent 434f1e0 commit 2120719

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pandas/tests/test_parser.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import subprocess
3+
4+
import pandas.util.testing as tm
5+
6+
class TestParser(tm.TestCase):
7+
_multiprocess_can_split_ = True
8+
9+
def test_parse_trim_buffers(self):
10+
code_ = """\n
11+
import pandas as pd
12+
from cStringIO import StringIO
13+
record_ = "9999-9,99:99,,,,ZZ,ZZ,,,ZZZ-ZZZZ,.Z-ZZZZ,-9.99,,,9.99,ZZZZZ,,-99,9,ZZZ-ZZZZ,ZZ-ZZZZ,,9.99,ZZZ-ZZZZZ,ZZZ-ZZZZZ,ZZZ-ZZZZ,ZZZ-ZZZZ,ZZZ-ZZZZ,ZZZ-ZZZZ,ZZZ-ZZZZ,ZZZ-ZZZZ,999,ZZZ-ZZZZ,,ZZ-ZZZZ,,,,,ZZZZ,ZZZ-ZZZZZ,ZZZ-ZZZZ,,,9,9,9,9,99,99,999,999,ZZZZZ,ZZZ-ZZZZZ,ZZZ-ZZZZ,9,ZZ-ZZZZ,9.99,ZZ-ZZZZ,ZZ-ZZZZ,,,,ZZZZ,,,ZZ,ZZ,,,,,,,,,,,,,9,,,999.99,999.99,,,ZZZZZ,,,Z9,,,,,,,ZZZ,ZZZ,,,,,,,,,,,ZZZZZ,ZZZZZ,ZZZ-ZZZZZZ,ZZZ-ZZZZZZ,ZZ-ZZZZ,ZZ-ZZZZ,ZZ-ZZZZ,ZZ-ZZZZ,,,999999,999999,ZZZ,ZZZ,,,ZZZ,ZZZ,999.99,999.99,,,,ZZZ-ZZZ,ZZZ-ZZZ,-9.99,-9.99,9,9,,99,,9.99,9.99,9,9,9.99,9.99,,,,9.99,9.99,,99,,99,9.99,9.99,,,ZZZ,ZZZ,,999.99,,999.99,ZZZ,ZZZ-ZZZZ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_]*173) + "\\n"
15+
for n_lines in range(82, 90):
16+
iterator_ = pd.read_csv(StringIO(csv_data), header=None, engine="c",
17+
dtype=object, chunksize=n_lines, na_filter=True)
18+
for chunk_ in iterator_:
19+
print n_lines, chunk_.iloc[0, 0], chunk_.iloc[-1, 0]
20+
exit(0)
21+
"""
22+
expected_ = "".join("%d 9999-9 9999-9\n"%(n_lines,)
23+
for n_lines in range(82, 90)
24+
for _ in range((173 + n_lines - 1) // n_lines))
25+
26+
# Run the faulty code via ang explicit argumnet to python
27+
proc_ = subprocess.Popen(("python", "-c", code_), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
28+
29+
# Wait until the subprocess finishes and then collect the output
30+
stdout_, stderr_ = proc_.communicate()
31+
exit_code = proc_.poll()
32+
33+
# Check whether a segfault or memory corruption occurred
34+
# self.assertTrue(exit_code == -11 or (exit_code == 0 and stdout_ != expected_))
35+
36+
# Check for correct exit code and output
37+
self.assertTrue(exit_code == 0 and stdout_ == expected_, msg="success")
38+
39+
if __name__ == '__main__':
40+
import nose
41+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
42+
exit=False)

0 commit comments

Comments
 (0)