Skip to content

Commit e966c26

Browse files
committed
Test case for patch, plus fix to not swallow exceptions
1 parent 506520b commit e966c26

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

pandas/io/tests/parser/common.py

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import csv
44
import os
55
import platform
6+
import codecs
67

78
import re
89
import sys
@@ -45,6 +46,20 @@ def test_empty_decimal_marker(self):
4546
with tm.assertRaisesRegexp(ValueError, msg):
4647
self.read_csv(StringIO(data), decimal='')
4748

49+
def test_bad_stream_exception(self):
50+
handle = open(self.csv_shiftjs, "rb")
51+
codec = codecs.lookup("utf-8")
52+
utf8 = codecs.lookup('utf-8')
53+
# stream must be binary UTF8
54+
stream = codecs.StreamRecoder(
55+
handle, utf8.encode, utf8.decode, codec.streamreader, codec.streamwriter)
56+
if compat.PY3:
57+
msg = "'utf-8' codec can't decode byte"
58+
else:
59+
msg = "'utf8' codec can't decode byte"
60+
with tm.assertRaisesRegexp(UnicodeDecodeError, msg):
61+
self.read_csv(stream)
62+
4863
def test_read_csv(self):
4964
if not compat.PY3:
5065
if compat.is_platform_windows():
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
num, text
2+
1,�T�E�����iSauron�A�A�C�k�A�̑n���̎� - ��O�I3019�N3��25���j�́AJ�ER�ER�E�g�[���L���̒����𕑑�Ƃ��������w�z�r�b�g�̖`���x�w�w�֕���x�w�V���}�����̕���x�̓o��l���B
3+
2,�w�z�r�b�g�̖`���x�Ɍ��y�̂���u���l����Ȃ��t�v�i�f��w�z�r�b�g�V���[�Y�x�̎����ł́u���l�����i�l�N���}���T�[�j�v�j�Ƃ͔ނ̂��Ƃł���B
4+
3,���̑��҂ł���w�w�֕���x�ɂ����Ắu��‚̎w�ցithe One Ring�j�v�̍���A�u�����iDark Lord�j�v�A�u���̎ҁithe One�j[1]�v�Ƃ��ēo�ꂷ��B�O�j�ɂ�����w�V���}�����̕���x�ł́A����̖��������S�X�̍ł��͂��鑤�߂ł������B
5+
4,�T�E�����͌����A�A���_�i�n���j�̑n����S�����V�g�I�푰�A�C�k�A�̈���ł��������A�僁���R�[���̔��t�ɉ��S���đ—����A�A���_�ɊQ���Ȃ����݂ƂȂ����B
6+
5,�u�T�E�����v�Ƃ̓N�E�F�����Łu�g�̖т̂悾�‚��́v�Ƃ����Ӗ��ł���A�V���_�����œ��l�̈Ӗ��ł��閼�O�u�S���T�E�A�v�ƌĂ΂�邱�Ƃ�����B
7+
6,�����́A�T�E����������A���݌������G���t�ɂ�閼�ł���A�w�w�֕���x�쒆�ɂ����ăA���S�����́u����i�T�E�����j�͎����̖{���̖��͎g��Ȃ����A��������ɏ���������ɏo�����肷�邱�Ƃ������Ȃ��v�Ɣ������Ă���B
8+
7,���̂ق��A���I�ɃG���t�ɑ΂��Ď��̂����Ƃ���閼�ɁA�u�A���i�^�[���i������N�j�v�A�u�A���^�m�i���M�ȍ׍H�t�j�v�A�u�A�E�����f�B���i�A�E���̉��l�j�v������B
9+
8,���I�̍��̃T�E�����́A���݂ɕϐg����\�͂������Ă����B
10+
9,���̔\�͂��g���Ό��ڗ킵�����h�ȊO���𑕂����Ƃ�A�܂�����ȘT��z����������Ƃ����������ɕς��邱�Ƃ��ł��A�G���t���狰���ꂽ�B
11+
10,���I�Ɉ�‚̎w�ւ����グ���T�E�����́A���̗͂̎w�ւŐ�����鎖���₻�̏��L�҂��x�z�ł���悤�ɂȂ����B
12+
11,�܂��A���̂��łтĂ��w�ւ�������艽�x�ł��h�邱�Ƃ��ł����B
13+
12,�������k�[���m�[���v���̍ۂɔ��������̂�j�󂳂ꂽ��́A��x�Ɣ������ϐg���邱�Ƃ͂ł��Ȃ��Ȃ�A���̈��ӂ̋�̂悤�Ȍ�������낵���p�����Ƃ�Ȃ��Ȃ����Ƃ����B
14+
13,�܂����΂��΁u�܂Ԃ��̂Ȃ��΂ɉ����ꂽ�ځv�Ƃ������S�ە\���ő�����ꂽ�B

pandas/io/tests/parser/test_parsers.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def setUp(self):
4444
self.csv1 = os.path.join(self.dirpath, 'test1.csv')
4545
self.csv2 = os.path.join(self.dirpath, 'test2.csv')
4646
self.xls1 = os.path.join(self.dirpath, 'test.xls')
47+
self.csv_shiftjs = os.path.join(self.dirpath, 'sauron.SHIFT_JIS.csv')
4748

4849

4950
class TestCParserHighMemory(BaseParser, CParserTests, tm.TestCase):

pandas/parser.pyx

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import warnings
1010
from csv import QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_NONE
1111
from cpython cimport (PyObject, PyBytes_FromString,
1212
PyBytes_AsString, PyBytes_Check,
13-
PyUnicode_Check, PyUnicode_AsUTF8String)
13+
PyUnicode_Check, PyUnicode_AsUTF8String,
14+
PyErr_Occurred, PyErr_Fetch)
15+
from cpython.ref cimport PyObject, Py_XDECREF
1416
from io.common import CParserError, DtypeWarning, EmptyDataError
1517

1618

@@ -1878,6 +1880,17 @@ cdef kh_float64_t* kset_float64_from_list(values) except NULL:
18781880

18791881

18801882
cdef raise_parser_error(object base, parser_t *parser):
1883+
cdef:
1884+
object old_exc
1885+
PyObject *type, *value, *traceback
1886+
if PyErr_Occurred():
1887+
PyErr_Fetch(&type, &value, &traceback);
1888+
Py_XDECREF(type)
1889+
Py_XDECREF(traceback)
1890+
if value != NULL:
1891+
old_exc = <object> value
1892+
Py_XDECREF(value)
1893+
raise old_exc
18811894
message = '%s. C error: ' % base
18821895
if parser.error_msg != NULL:
18831896
if PY3:

0 commit comments

Comments
 (0)