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

Use EAFP approach in local.py #463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions asgiref/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ def __setattr__(self, key: str, value: Any) -> None:

def __delattr__(self, key: str) -> None:
storage_object = self._data.get({})
if key in storage_object:
try:
del storage_object[key]
self._data.set(storage_object)
else:
except KeyError:
raise AttributeError(f"{self!r} object has no attribute {key!r}")
else:
self._data.set(storage_object)


class Local:
Expand Down Expand Up @@ -98,15 +99,15 @@ def _lock_storage(self):
# local to this thread, but additionally should
# behave like a context var (is only visible with
# the same async call stack)

# Ensure context exists in the current thread
if not hasattr(self._storage, "cvar"):
self._storage.cvar = _CVar()

# self._storage is a thread local, so the members
# can't be accessed in another thread (we don't
# need any locks)
yield self._storage.cvar
try:
yield self._storage.cvar
except AttributeError:
# Ensure context exists in the current thread
self._storage.cvar = _CVar()
yield self._storage.cvar
else:
# Lock for thread_critical=False as other threads
# can access the exact same storage object
Expand Down
Loading