From 073d18e677a22dde1b4e3ec66c2f480bba8ab03d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 12 Dec 2019 13:50:56 -0800 Subject: [PATCH 1/2] ERR: stringify erorr message from parsers.pyx, closes #29233 --- pandas/_libs/parsers.pyx | 7 ++++--- pandas/tests/io/parser/test_common.py | 10 +++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index adc7a650b745f..bb1493280dfd2 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -657,7 +657,7 @@ cdef class TextReader: if isinstance(source, str): encoding = sys.getfilesystemencoding() or "utf-8" - + usource = source source = source.encode(encoding) if self.memory_map: @@ -677,10 +677,11 @@ cdef class TextReader: if ptr == NULL: if not os.path.exists(source): + raise FileNotFoundError( ENOENT, - f'File {source} does not exist', - source) + f'File {usource} does not exist', + usource) raise IOError('Initializing from file failed') self.parser.source = ptr diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index 590f26a76802a..37993aeb87dab 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -2,7 +2,6 @@ Tests that work on both the Python and C engines but do not have a specific classification into the other test modules. """ - import codecs from collections import OrderedDict import csv @@ -19,6 +18,7 @@ from pandas._libs.tslib import Timestamp from pandas.errors import DtypeWarning, EmptyDataError, ParserError +import pandas as pd from pandas import DataFrame, Index, MultiIndex, Series, compat, concat import pandas.util.testing as tm @@ -2160,6 +2160,14 @@ def test_suppress_error_output(all_parsers, capsys): assert captured.err == "" +def test_err_msg_bytes(): + # GH#29233 + try: + pd.read_csv("nonexistent_name") + except FileNotFoundError as err: + assert err.strerror == "File nonexistent_name does not exist" + + @pytest.mark.parametrize("filename", ["sé-es-vé.csv", "ru-sй.csv", "中文文件名.csv"]) def test_filename_with_special_chars(all_parsers, filename): # see gh-15086. From fa148401e6b63165016fe6abfe324468a50e74b3 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 12 Dec 2019 15:33:45 -0800 Subject: [PATCH 2/2] use pytest.raises --- pandas/tests/io/parser/test_common.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index 37993aeb87dab..fe360f1346c7c 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -18,7 +18,6 @@ from pandas._libs.tslib import Timestamp from pandas.errors import DtypeWarning, EmptyDataError, ParserError -import pandas as pd from pandas import DataFrame, Index, MultiIndex, Series, compat, concat import pandas.util.testing as tm @@ -978,15 +977,15 @@ def test_path_local_path(all_parsers): def test_nonexistent_path(all_parsers): # gh-2428: pls no segfault # gh-14086: raise more helpful FileNotFoundError + # GH#29233 "File foo" instead of "File b'foo'" parser = all_parsers path = "{}.csv".format(tm.rands(10)) - msg = "does not exist" if parser.engine == "c" else r"\[Errno 2\]" + msg = f"File {path} does not exist" if parser.engine == "c" else r"\[Errno 2\]" with pytest.raises(FileNotFoundError, match=msg) as e: parser.read_csv(path) filename = e.value.filename - filename = filename.decode() if isinstance(filename, bytes) else filename assert path == filename @@ -2160,14 +2159,6 @@ def test_suppress_error_output(all_parsers, capsys): assert captured.err == "" -def test_err_msg_bytes(): - # GH#29233 - try: - pd.read_csv("nonexistent_name") - except FileNotFoundError as err: - assert err.strerror == "File nonexistent_name does not exist" - - @pytest.mark.parametrize("filename", ["sé-es-vé.csv", "ru-sй.csv", "中文文件名.csv"]) def test_filename_with_special_chars(all_parsers, filename): # see gh-15086.