Skip to content
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

Fix settings context manager leaks on exception #1193

Merged
merged 1 commit into from Apr 11, 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
17 changes: 9 additions & 8 deletions src/zeep/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ def __call__(self, **options):
current[key] = getattr(self, key)
setattr(self._tls, key, value)

yield

for key, value in current.items():
default = getattr(self, key)
if value == default:
delattr(self._tls, key)
else:
setattr(self._tls, key, value)
try:
yield
finally:
for key, value in current.items():
default = getattr(self, key)
if value == default:
delattr(self._tls, key)
else:
setattr(self._tls, key, value)

def __getattribute__(self, key):
if key != "_tls" and hasattr(self._tls, key):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ def test_settings_set_context_raw_response():
assert settings.raw_response is True
# Check that the original value returned
assert settings.raw_response is False


def test_settings_set_context_with_exception():
settings = Settings()

assert settings.raw_response is False
try:
with settings(raw_response=True):
assert settings.raw_response is True
raise RuntimeError
except RuntimeError:
pass
# Check that the original value returned
assert settings.raw_response is False