aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-06-28 13:20:29 -0400
committerGitHub <noreply@github.com>2018-06-28 13:20:29 -0400
commit41cb0baea96a80360971908a0bd79d9d40dd5e44 (patch)
treedaae24f132152e040a3d40aa66922b90155da8ec
parentbpo-31546: Fix input hook integration (GH-7978) (diff)
downloadcpython-41cb0baea96a80360971908a0bd79d9d40dd5e44.tar.gz
cpython-41cb0baea96a80360971908a0bd79d9d40dd5e44.tar.bz2
cpython-41cb0baea96a80360971908a0bd79d9d40dd5e44.zip
bpo-33985: Implement ContextVar.name attribute. (GH-7980)
-rw-r--r--Doc/library/contextvars.rst2
-rw-r--r--Lib/test/test_context.py9
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2018-06-27-18-56-41.bpo-33985.ILJ3Af.rst1
-rw-r--r--Python/context.c5
4 files changed, 15 insertions, 2 deletions
diff --git a/Doc/library/contextvars.rst b/Doc/library/contextvars.rst
index 9c7ea2bdb7b..8805661c456 100644
--- a/Doc/library/contextvars.rst
+++ b/Doc/library/contextvars.rst
@@ -48,6 +48,8 @@ Context Variables
The name of the variable. This is a read-only property.
+ .. versionadded:: 3.7.1
+
.. method:: get([default])
Return a value for the context variable for the current context.
diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py
index 74d05fc5c41..efd7319a23a 100644
--- a/Lib/test/test_context.py
+++ b/Lib/test/test_context.py
@@ -30,8 +30,13 @@ class ContextTest(unittest.TestCase):
with self.assertRaisesRegex(TypeError, 'must be a str'):
contextvars.ContextVar(1)
- c = contextvars.ContextVar('a')
- self.assertNotEqual(hash(c), hash('a'))
+ c = contextvars.ContextVar('aaa')
+ self.assertEqual(c.name, 'aaa')
+
+ with self.assertRaises(AttributeError):
+ c.name = 'bbb'
+
+ self.assertNotEqual(hash(c), hash('aaa'))
def test_context_var_new_2(self):
self.assertIsNone(contextvars.ContextVar[int])
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-06-27-18-56-41.bpo-33985.ILJ3Af.rst b/Misc/NEWS.d/next/Core and Builtins/2018-06-27-18-56-41.bpo-33985.ILJ3Af.rst
new file mode 100644
index 00000000000..07c8f907868
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-06-27-18-56-41.bpo-33985.ILJ3Af.rst
@@ -0,0 +1 @@
+Implement contextvars.ContextVar.name attribute.
diff --git a/Python/context.c b/Python/context.c
index b727748ee73..8ee048ccdd5 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -940,6 +940,10 @@ contextvar_cls_getitem(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
+static PyMemberDef PyContextVar_members[] = {
+ {"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY},
+ {NULL}
+};
static PyMethodDef PyContextVar_methods[] = {
_CONTEXTVARS_CONTEXTVAR_GET_METHODDEF
@@ -955,6 +959,7 @@ PyTypeObject PyContextVar_Type = {
"ContextVar",
sizeof(PyContextVar),
.tp_methods = PyContextVar_methods,
+ .tp_members = PyContextVar_members,
.tp_dealloc = (destructor)contextvar_tp_dealloc,
.tp_getattro = PyObject_GenericGetAttr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,