Skip to content

Commit 12928e0

Browse files
fantix1st1
authored andcommitted
Fixed #3, use immutables
1 parent ba79525 commit 12928e0

File tree

2 files changed

+6
-31
lines changed

2 files changed

+6
-31
lines changed

contextvars/__init__.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
11
import collections.abc
22
import threading
33

4+
from immutables import Map
5+
46

57
__all__ = ('ContextVar', 'Context', 'Token', 'copy_context')
68

79

810
_NO_DEFAULT = object()
911

1012

11-
class _ContextData:
12-
13-
def __init__(self):
14-
self._mapping = dict()
15-
16-
def __getitem__(self, key):
17-
return self._mapping[key]
18-
19-
def __contains__(self, key):
20-
return key in self._mapping
21-
22-
def __len__(self):
23-
return len(self._mapping)
24-
25-
def __iter__(self):
26-
return iter(self._mapping)
27-
28-
def set(self, key, value):
29-
copy = _ContextData()
30-
copy._mapping = self._mapping.copy()
31-
copy._mapping[key] = value
32-
return copy
33-
34-
def delete(self, key):
35-
copy = _ContextData()
36-
copy._mapping = self._mapping.copy()
37-
del copy._mapping[key]
38-
return copy
39-
40-
4113
class ContextMeta(type(collections.abc.Mapping)):
4214

4315
# contextvars.Context is not subclassable.
@@ -52,7 +24,7 @@ def __new__(mcls, names, bases, dct):
5224
class Context(collections.abc.Mapping, metaclass=ContextMeta):
5325

5426
def __init__(self):
55-
self._data = _ContextData()
27+
self._data = Map()
5628
self._prev_context = None
5729

5830
def run(self, callable, *args, **kwargs):

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
author_email='[email protected]',
1717
packages=['contextvars'],
1818
provides=['contextvars'],
19+
install_requires=[
20+
'immutables==0.5',
21+
],
1922
license='Apache License, Version 2.0',
2023
classifiers=[
2124
'Development Status :: 3 - Alpha',

0 commit comments

Comments
 (0)