Skip to content

CLN: contextmanagers should yield in a try-finally block #44716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
23 changes: 12 additions & 11 deletions pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down