diff --git a/pandas/_config/config.py b/pandas/_config/config.py index 5c3db40828fe3..aef5c3049f295 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -763,10 +763,12 @@ def inner(key: str, *args, **kwds): set_option = wrap(set_option) get_option = wrap(get_option) register_option = wrap(register_option) - yield None - set_option = _set_option - get_option = _get_option - register_option = _register_option + try: + yield + finally: + set_option = _set_option + get_option = _get_option + register_option = _register_option # These factories and methods are handy for use as the validator diff --git a/pandas/_testing/contexts.py b/pandas/_testing/contexts.py index b92772761e0a7..db35b2e7b4cab 100644 --- a/pandas/_testing/contexts.py +++ b/pandas/_testing/contexts.py @@ -191,8 +191,10 @@ def with_csv_dialect(name, **kwargs): raise ValueError("Cannot override builtin dialect.") csv.register_dialect(name, **kwargs) - yield - csv.unregister_dialect(name) + try: + yield + finally: + csv.unregister_dialect(name) @contextmanager @@ -206,9 +208,11 @@ def use_numexpr(use, min_elements=None): oldmin = expr._MIN_ELEMENTS set_option("compute.use_numexpr", use) expr._MIN_ELEMENTS = min_elements - yield - expr._MIN_ELEMENTS = oldmin - set_option("compute.use_numexpr", olduse) + try: + yield + finally: + expr._MIN_ELEMENTS = oldmin + set_option("compute.use_numexpr", olduse) class RNGContext: diff --git a/pandas/util/_test_decorators.py b/pandas/util/_test_decorators.py index f63212c777048..7c5c0959ceaa0 100644 --- a/pandas/util/_test_decorators.py +++ b/pandas/util/_test_decorators.py @@ -259,17 +259,18 @@ def file_leak_context(): flist = proc.open_files() conns = proc.connections() - yield - - flist2 = proc.open_files() - # on some builds open_files includes file position, which we _dont_ - # expect to remain unchanged, so we need to compare excluding that - flist_ex = [(x.path, x.fd) for x in flist] - flist2_ex = [(x.path, x.fd) for x in flist2] - assert flist2_ex == flist_ex, (flist2, flist) - - conns2 = proc.connections() - assert conns2 == conns, (conns2, conns) + try: + yield + finally: + flist2 = proc.open_files() + # on some builds open_files includes file position, which we _dont_ + # expect to remain unchanged, so we need to compare excluding that + flist_ex = [(x.path, x.fd) for x in flist] + flist2_ex = [(x.path, x.fd) for x in flist2] + assert flist2_ex == flist_ex, (flist2, flist) + + conns2 = proc.connections() + assert conns2 == conns, (conns2, conns) def async_mark():