Skip to content

Commit d0fe59c

Browse files
jtratnerTomAugspurger
authored andcommitted
Fix unbound local with bad engine (#16511)
(cherry picked from commit 9b0ea41)
1 parent eefbaf7 commit d0fe59c

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v0.20.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Bug Fixes
3939

4040
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
4141
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
42+
- Passing an invalid engine to :func:`read_csv` now raises an informative
43+
``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`)
4244
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)
4345

4446
Conversion

pandas/io/parsers.py

+4
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,10 @@ def _make_engine(self, engine='c'):
969969
klass = PythonParser
970970
elif engine == 'python-fwf':
971971
klass = FixedWidthFieldParser
972+
else:
973+
raise ValueError('Unknown engine: {engine} (valid options are'
974+
' "c", "python", or' ' "python-fwf")'.format(
975+
engine=engine))
972976
self._engine = klass(self.f, **self.options)
973977

974978
def _failover_to_python(self):

pandas/tests/io/test_common.py

+7
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,10 @@ def test_next(self):
141141
assert next_line.strip() == line.strip()
142142

143143
pytest.raises(StopIteration, next, wrapper)
144+
145+
def test_unknown_engine(self):
146+
with tm.ensure_clean() as path:
147+
df = tm.makeDataFrame()
148+
df.to_csv(path)
149+
with tm.assert_raises_regex(ValueError, 'Unknown engine'):
150+
read_csv(path, engine='pyt')

0 commit comments

Comments
 (0)