Skip to content

Commit 449211b

Browse files
authored
CLN: contextmanagers should yield in a try-finally block (#44716)
1 parent 9db55a7 commit 449211b

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

pandas/_config/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,12 @@ def inner(key: str, *args, **kwds):
763763
set_option = wrap(set_option)
764764
get_option = wrap(get_option)
765765
register_option = wrap(register_option)
766-
yield None
767-
set_option = _set_option
768-
get_option = _get_option
769-
register_option = _register_option
766+
try:
767+
yield
768+
finally:
769+
set_option = _set_option
770+
get_option = _get_option
771+
register_option = _register_option
770772

771773

772774
# These factories and methods are handy for use as the validator

pandas/_testing/contexts.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ def with_csv_dialect(name, **kwargs):
191191
raise ValueError("Cannot override builtin dialect.")
192192

193193
csv.register_dialect(name, **kwargs)
194-
yield
195-
csv.unregister_dialect(name)
194+
try:
195+
yield
196+
finally:
197+
csv.unregister_dialect(name)
196198

197199

198200
@contextmanager
@@ -206,9 +208,11 @@ def use_numexpr(use, min_elements=None):
206208
oldmin = expr._MIN_ELEMENTS
207209
set_option("compute.use_numexpr", use)
208210
expr._MIN_ELEMENTS = min_elements
209-
yield
210-
expr._MIN_ELEMENTS = oldmin
211-
set_option("compute.use_numexpr", olduse)
211+
try:
212+
yield
213+
finally:
214+
expr._MIN_ELEMENTS = oldmin
215+
set_option("compute.use_numexpr", olduse)
212216

213217

214218
class RNGContext:

pandas/util/_test_decorators.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,18 @@ def file_leak_context():
259259
flist = proc.open_files()
260260
conns = proc.connections()
261261

262-
yield
263-
264-
flist2 = proc.open_files()
265-
# on some builds open_files includes file position, which we _dont_
266-
# expect to remain unchanged, so we need to compare excluding that
267-
flist_ex = [(x.path, x.fd) for x in flist]
268-
flist2_ex = [(x.path, x.fd) for x in flist2]
269-
assert flist2_ex == flist_ex, (flist2, flist)
270-
271-
conns2 = proc.connections()
272-
assert conns2 == conns, (conns2, conns)
262+
try:
263+
yield
264+
finally:
265+
flist2 = proc.open_files()
266+
# on some builds open_files includes file position, which we _dont_
267+
# expect to remain unchanged, so we need to compare excluding that
268+
flist_ex = [(x.path, x.fd) for x in flist]
269+
flist2_ex = [(x.path, x.fd) for x in flist2]
270+
assert flist2_ex == flist_ex, (flist2, flist)
271+
272+
conns2 = proc.connections()
273+
assert conns2 == conns, (conns2, conns)
273274

274275

275276
def async_mark():

0 commit comments

Comments
 (0)