From d0fe59c008041828fbe3b9727cbf568a72c56fb5 Mon Sep 17 00:00:00 2001 From: Jeff Tratner Date: Wed, 31 May 2017 03:39:46 -0700 Subject: [PATCH] Fix unbound local with bad engine (#16511) (cherry picked from commit 9b0ea41e91a3db3bfb9cce746b7c381483be3555) --- doc/source/whatsnew/v0.20.2.txt | 2 ++ pandas/io/parsers.py | 4 ++++ pandas/tests/io/test_common.py | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/doc/source/whatsnew/v0.20.2.txt b/doc/source/whatsnew/v0.20.2.txt index 07ab637dd29f5..66edf82202aa8 100644 --- a/doc/source/whatsnew/v0.20.2.txt +++ b/doc/source/whatsnew/v0.20.2.txt @@ -39,6 +39,8 @@ Bug Fixes - Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`) - Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`) +- Passing an invalid engine to :func:`read_csv` now raises an informative + ``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`) - Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`) Conversion diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index ce8643504932f..c1e3b4b19b336 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -969,6 +969,10 @@ def _make_engine(self, engine='c'): klass = PythonParser elif engine == 'python-fwf': klass = FixedWidthFieldParser + else: + raise ValueError('Unknown engine: {engine} (valid options are' + ' "c", "python", or' ' "python-fwf")'.format( + engine=engine)) self._engine = klass(self.f, **self.options) def _failover_to_python(self): diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index a1a95e09915f1..2c5a6ae15ce2a 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -141,3 +141,10 @@ def test_next(self): assert next_line.strip() == line.strip() pytest.raises(StopIteration, next, wrapper) + + def test_unknown_engine(self): + with tm.ensure_clean() as path: + df = tm.makeDataFrame() + df.to_csv(path) + with tm.assert_raises_regex(ValueError, 'Unknown engine'): + read_csv(path, engine='pyt')