| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,8 @@ Context Variables | |||
| 48 | 48 | ||
| 49 | 49 | The name of the variable. This is a read-only property. | |
| 50 | 50 | ||
| 51 | + .. versionadded:: 3.7.1 | ||
| 52 | + | ||
| 51 | 53 | .. method:: get([default]) | |
| 52 | 54 | ||
| 53 | 55 | Return a value for the context variable for the current context. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,8 +30,13 @@ def test_context_var_new_1(self): | |||
| 30 | 30 | with self.assertRaisesRegex(TypeError, 'must be a str'): | |
| 31 | 31 | contextvars.ContextVar(1) | |
| 32 | 32 | ||
| 33 | - c = contextvars.ContextVar('a') | ||
| 34 | - self.assertNotEqual(hash(c), hash('a')) | ||
| 33 | + c = contextvars.ContextVar('aaa') | ||
| 34 | + self.assertEqual(c.name, 'aaa') | ||
| 35 | + | ||
| 36 | + with self.assertRaises(AttributeError): | ||
| 37 | + c.name = 'bbb' | ||
| 38 | + | ||
| 39 | + self.assertNotEqual(hash(c), hash('aaa')) | ||
| 35 | 40 | ||
| 36 | 41 | def test_context_var_new_2(self): | |
| 37 | 42 | self.assertIsNone(contextvars.ContextVar[int]) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Implement contextvars.ContextVar.name attribute. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -940,6 +940,10 @@ contextvar_cls_getitem(PyObject *self, PyObject *args) | |||
| 940 | 940 | Py_RETURN_NONE; | |
| 941 | 941 | } | |
| 942 | 942 | ||
| 943 | + static PyMemberDef PyContextVar_members[] = { | ||
| 944 | + {"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY}, | ||
| 945 | + {NULL} | ||
| 946 | + }; | ||
| 943 | 947 | ||
| 944 | 948 | static PyMethodDef PyContextVar_methods[] = { | |
| 945 | 949 | _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF | |
@@ -955,6 +959,7 @@ PyTypeObject PyContextVar_Type = { | |||
| 955 | 959 | "ContextVar", | |
| 956 | 960 | sizeof(PyContextVar), | |
| 957 | 961 | .tp_methods = PyContextVar_methods, | |
| 962 | + .tp_members = PyContextVar_members, | ||
| 958 | 963 | .tp_dealloc = (destructor)contextvar_tp_dealloc, | |
| 959 | 964 | .tp_getattro = PyObject_GenericGetAttr, | |
| 960 | 965 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments