mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-33985: Implement ContextVar.name attribute. (GH-7980)
This commit is contained in:
parent
9b9d58f0d8
commit
41cb0baea9
4 changed files with 15 additions and 2 deletions
|
@ -48,6 +48,8 @@ Context Variables
|
||||||
|
|
||||||
The name of the variable. This is a read-only property.
|
The name of the variable. This is a read-only property.
|
||||||
|
|
||||||
|
.. versionadded:: 3.7.1
|
||||||
|
|
||||||
.. method:: get([default])
|
.. method:: get([default])
|
||||||
|
|
||||||
Return a value for the context variable for the current context.
|
Return a value for the context variable for the current context.
|
||||||
|
|
|
@ -30,8 +30,13 @@ class ContextTest(unittest.TestCase):
|
||||||
with self.assertRaisesRegex(TypeError, 'must be a str'):
|
with self.assertRaisesRegex(TypeError, 'must be a str'):
|
||||||
contextvars.ContextVar(1)
|
contextvars.ContextVar(1)
|
||||||
|
|
||||||
c = contextvars.ContextVar('a')
|
c = contextvars.ContextVar('aaa')
|
||||||
self.assertNotEqual(hash(c), hash('a'))
|
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):
|
def test_context_var_new_2(self):
|
||||||
self.assertIsNone(contextvars.ContextVar[int])
|
self.assertIsNone(contextvars.ContextVar[int])
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Implement contextvars.ContextVar.name attribute.
|
|
@ -940,6 +940,10 @@ contextvar_cls_getitem(PyObject *self, PyObject *args)
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyMemberDef PyContextVar_members[] = {
|
||||||
|
{"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY},
|
||||||
|
{NULL}
|
||||||
|
};
|
||||||
|
|
||||||
static PyMethodDef PyContextVar_methods[] = {
|
static PyMethodDef PyContextVar_methods[] = {
|
||||||
_CONTEXTVARS_CONTEXTVAR_GET_METHODDEF
|
_CONTEXTVARS_CONTEXTVAR_GET_METHODDEF
|
||||||
|
@ -955,6 +959,7 @@ PyTypeObject PyContextVar_Type = {
|
||||||
"ContextVar",
|
"ContextVar",
|
||||||
sizeof(PyContextVar),
|
sizeof(PyContextVar),
|
||||||
.tp_methods = PyContextVar_methods,
|
.tp_methods = PyContextVar_methods,
|
||||||
|
.tp_members = PyContextVar_members,
|
||||||
.tp_dealloc = (destructor)contextvar_tp_dealloc,
|
.tp_dealloc = (destructor)contextvar_tp_dealloc,
|
||||||
.tp_getattro = PyObject_GenericGetAttr,
|
.tp_getattro = PyObject_GenericGetAttr,
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue