Skip to content

Commit ef01f93

Browse files
committed
Make contextvars at least thread-local
1 parent a4baf51 commit ef01f93

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

graalpython/lib-graalpython/_contextvars.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,29 @@ class Context:
4141
pass
4242

4343

44+
_NO_DEFAULT = object()
45+
46+
4447
class ContextVar:
45-
def __init__(self, name, default=None):
48+
def __init__(self, name, default=_NO_DEFAULT):
49+
import threading
4650
self._name = name
47-
self._value = default
48-
49-
def get(self):
50-
if self._value is None:
51+
self._default = default
52+
self._local = threading.local()
53+
54+
def get(self, default=_NO_DEFAULT):
55+
try:
56+
return self._local.value
57+
except AttributeError:
58+
if default is not _NO_DEFAULT:
59+
return default
60+
if self._default is not _NO_DEFAULT:
61+
return self._default
5162
raise LookupError
52-
return self._value
53-
63+
5464
def set(self, value):
55-
self._value = value
56-
65+
self._local.value = value
66+
5767
def reset(self, token):
5868
pass
5969

@@ -64,4 +74,3 @@ class Token:
6474

6575
def copy_context():
6676
raise NotImplementedError
67-

0 commit comments

Comments
 (0)