summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-12-08 03:53:07 -0800
committerGitHub <noreply@github.com>2019-12-08 03:53:07 -0800
commit9baa870c27b54c26f9dd292c26936b70bdb64a06 (patch)
tree2a69d29f82306f2142c80bd538bb397cd955329e
parenta197f8aa7493e66bc54c3db8f796d00cef1c3042 (diff)
downloadcpython-git-9baa870c27b54c26f9dd292c26936b70bdb64a06.tar.gz
bpo-38979: fix ContextVar "__class_getitem__" method (GH-17497)
now contextvars.ContextVar "__class_getitem__" method returns ContextVar class, not None. https://bugs.python.org/issue38979 Automerge-Triggered-By: @asvetlov (cherry picked from commit 28c91631c24e53713ad0e8a2bbae716373f5e53d) Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
-rw-r--r--Lib/test/test_context.py7
-rw-r--r--Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst1
-rw-r--r--Python/context.c7
3 files changed, 9 insertions, 6 deletions
diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py
index efd7319a23..b9e991a400 100644
--- a/Lib/test/test_context.py
+++ b/Lib/test/test_context.py
@@ -38,9 +38,6 @@ class ContextTest(unittest.TestCase):
self.assertNotEqual(hash(c), hash('aaa'))
- def test_context_var_new_2(self):
- self.assertIsNone(contextvars.ContextVar[int])
-
@isolated_context
def test_context_var_repr_1(self):
c = contextvars.ContextVar('a')
@@ -361,6 +358,10 @@ class ContextTest(unittest.TestCase):
tp.shutdown()
self.assertEqual(results, list(range(10)))
+ def test_contextvar_getitem(self):
+ clss = contextvars.ContextVar
+ self.assertEqual(clss[str], clss)
+
# HAMT Tests
diff --git a/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst
new file mode 100644
index 0000000000..6a91a12e49
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-07-16-32-42.bpo-38979.q0sIHy.rst
@@ -0,0 +1 @@
+Return class from ``ContextVar.__class_getitem__`` to simplify subclassing.
diff --git a/Python/context.c b/Python/context.c
index 90c71e386d..c1575c9e20 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -974,9 +974,10 @@ _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token)
static PyObject *
-contextvar_cls_getitem(PyObject *self, PyObject *args)
+contextvar_cls_getitem(PyObject *self, PyObject *arg)
{
- Py_RETURN_NONE;
+ Py_INCREF(self);
+ return self;
}
static PyMemberDef PyContextVar_members[] = {
@@ -989,7 +990,7 @@ static PyMethodDef PyContextVar_methods[] = {
_CONTEXTVARS_CONTEXTVAR_SET_METHODDEF
_CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF
{"__class_getitem__", contextvar_cls_getitem,
- METH_VARARGS | METH_STATIC, NULL},
+ METH_O | METH_CLASS, NULL},
{NULL, NULL}
};