FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-126451: Register contextvars.Context to collections.abc.Mapping by tungol · Pull Request #126452 · python/cpython · GitHub

/ cpython Public
4 changes: 4 additions & 0 deletions Lib/contextvars.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import _collections_abc
from _contextvars import Context, ContextVar, Token, copy_context


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


_collections_abc.Mapping.register(Context)
14 changes: 14 additions & 0 deletions Lib/test/test_context.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections.abc
import concurrent.futures
import contextvars
import functools
Expand Down Expand Up @@ -350,6 +351,19 @@ def ctx2_fun():

ctx1.run(ctx1_fun)

def test_context_isinstance(self):
ctx = contextvars.Context()
self.assertIsInstance(ctx, collections.abc.Mapping)
Comment thread
tungol marked this conversation as resolved.
self.assertTrue(issubclass(contextvars.Context, collections.abc.Mapping))

mapping_methods = (
'__contains__', '__eq__', '__getitem__', '__iter__', '__len__',
'__ne__', 'get', 'items', 'keys', 'values',
)
for name in mapping_methods:
Comment thread
kumaraditya303 marked this conversation as resolved.
with self.subTest(name=name):
self.assertTrue(callable(getattr(ctx, name)))

@isolated_context
@threading_helper.requires_working_threading()
def test_context_threads_1(self):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Register the :class:`contextvars.Context` type to
:class:`collections.abc.Mapping`.

Back | FazBrowse Home | New Git URL