Skip to content

Commit 9b0ea41

Browse files
jtratnerjreback
authored andcommitted
Fix unbound local with bad engine (#16511)
1 parent d4f80b0 commit 9b0ea41

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/source/whatsnew/v0.20.2.txt

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Bug Fixes
4141
detecting the terminal size. This fix only applies to python 3 (:issue:`16496`)
4242
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
4343
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
44+
- Passing an invalid engine to :func:`read_csv` now raises an informative
45+
``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`)
46+
4447

4548

4649

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
@@ -223,3 +223,10 @@ def test_next(self):
223223
assert next_line.strip() == line.strip()
224224

225225
pytest.raises(StopIteration, next, wrapper)
226+
227+
def test_unknown_engine(self):
228+
with tm.ensure_clean() as path:
229+
df = tm.makeDataFrame()
230+
df.to_csv(path)
231+
with tm.assert_raises_regex(ValueError, 'Unknown engine'):
232+
read_csv(path, engine='pyt')

0 commit comments

Comments
 (0)