|
2 | 2 | import contextlib |
3 | 3 | import contextvars |
4 | 4 | import threading |
5 | | -from typing import Any, Union |
| 5 | +from typing import Any, Dict, Union |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class _CVar: |
9 | 9 | """Storage utility for Local.""" |
10 | 10 |
|
11 | 11 | def __init__(self) -> None: |
12 | | - self._data: dict[str, contextvars.ContextVar[Any]] = {} |
| 12 | + self._data: "contextvars.ContextVar[Dict[str, Any]]" = contextvars.ContextVar( |
| 13 | + "asgiref.local" |
| 14 | + ) |
13 | 15 |
|
14 | | - def __getattr__(self, key: str) -> Any: |
| 16 | + def __getattr__(self, key): |
| 17 | + storage_object = self._data.get({}) |
15 | 18 | try: |
16 | | - var = self._data[key] |
| 19 | + return storage_object[key] |
17 | 20 | except KeyError: |
18 | 21 | raise AttributeError(f"{self!r} object has no attribute {key!r}") |
19 | 22 |
|
20 | | - try: |
21 | | - return var.get() |
22 | | - except LookupError: |
23 | | - raise AttributeError(f"{self!r} object has no attribute {key!r}") |
24 | | - |
25 | 23 | def __setattr__(self, key: str, value: Any) -> None: |
26 | 24 | if key == "_data": |
27 | 25 | return super().__setattr__(key, value) |
28 | 26 |
|
29 | | - var = self._data.get(key) |
30 | | - if var is None: |
31 | | - self._data[key] = var = contextvars.ContextVar(key) |
32 | | - var.set(value) |
| 27 | + storage_object = self._data.get({}).copy() |
| 28 | + storage_object[key] = value |
| 29 | + self._data.set(storage_object) |
33 | 30 |
|
34 | 31 | def __delattr__(self, key: str) -> None: |
35 | | - if key in self._data: |
36 | | - del self._data[key] |
| 32 | + storage_object = self._data.get({}).copy() |
| 33 | + if key in storage_object: |
| 34 | + del storage_object[key] |
| 35 | + self._data.set(storage_object) |
37 | 36 | else: |
38 | 37 | raise AttributeError(f"{self!r} object has no attribute {key!r}") |
39 | 38 |
|
|
0 commit comments