From 5cfd5e2af48f7e5491bb9cc1ed67366dc6854963 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 29 Feb 2024 18:55:48 -0800 Subject: [PATCH] blurb --- Include/internal/pycore_ast.h | 4 +-- Include/internal/pycore_ast_state.h | 1 + ...-02-29-18-55-45.gh-issue-116129.wsFnIq.rst | 2 ++ Python/Python-ast.c | 33 ++++++++++--------- 4 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-02-29-18-55-45.gh-issue-116129.wsFnIq.rst diff --git a/Include/internal/pycore_ast.h b/Include/internal/pycore_ast.h index f7dedcd5c5a411..54100734cba9c4 100644 --- a/Include/internal/pycore_ast.h +++ b/Include/internal/pycore_ast.h @@ -657,7 +657,7 @@ struct _type_param { struct { identifier name; expr_ty bound; - expr_ty default_; + expr_ty default; } TypeVar; struct { @@ -895,7 +895,7 @@ pattern_ty _PyAST_MatchOr(asdl_pattern_seq * patterns, int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena *arena); type_ignore_ty _PyAST_TypeIgnore(int lineno, string tag, PyArena *arena); -type_param_ty _PyAST_TypeVar(identifier name, expr_ty bound, expr_ty default_, +type_param_ty _PyAST_TypeVar(identifier name, expr_ty bound, expr_ty default, int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena *arena); type_param_ty _PyAST_ParamSpec(identifier name, expr_ty default_, int lineno, diff --git a/Include/internal/pycore_ast_state.h b/Include/internal/pycore_ast_state.h index b32ee8fdce53da..e89d8b86ea9cd1 100644 --- a/Include/internal/pycore_ast_state.h +++ b/Include/internal/pycore_ast_state.h @@ -184,6 +184,7 @@ struct ast_state { PyObject *conversion; PyObject *ctx; PyObject *decorator_list; + PyObject *default; PyObject *default_; PyObject *defaults; PyObject *elt; diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-02-29-18-55-45.gh-issue-116129.wsFnIq.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-29-18-55-45.gh-issue-116129.wsFnIq.rst new file mode 100644 index 00000000000000..9f034e66c48a41 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-02-29-18-55-45.gh-issue-116129.wsFnIq.rst @@ -0,0 +1,2 @@ +Add grammar support for defaults on type parameters, as specified in +:pep:`696`. Patch by Jelle Zijlstra. diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 019dae629752e3..fa58668343d3f6 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -203,6 +203,7 @@ void _PyAST_Fini(PyInterpreterState *interp) Py_CLEAR(state->conversion); Py_CLEAR(state->ctx); Py_CLEAR(state->decorator_list); + Py_CLEAR(state->default); Py_CLEAR(state->default_); Py_CLEAR(state->defaults); Py_CLEAR(state->elt); @@ -312,6 +313,7 @@ static int init_identifiers(struct ast_state *state) if ((state->conversion = PyUnicode_InternFromString("conversion")) == NULL) return -1; if ((state->ctx = PyUnicode_InternFromString("ctx")) == NULL) return -1; if ((state->decorator_list = PyUnicode_InternFromString("decorator_list")) == NULL) return -1; + if ((state->default = PyUnicode_InternFromString("default")) == NULL) return -1; if ((state->default_ = PyUnicode_InternFromString("default_")) == NULL) return -1; if ((state->defaults = PyUnicode_InternFromString("defaults")) == NULL) return -1; if ((state->elt = PyUnicode_InternFromString("elt")) == NULL) return -1; @@ -811,7 +813,7 @@ static PyObject* ast2obj_type_param(struct ast_state *state, struct validator static const char * const TypeVar_fields[]={ "name", "bound", - "default_", + "default", }; static const char * const ParamSpec_fields[]={ "name", @@ -4926,7 +4928,7 @@ add_ast_annotations(struct ast_state *state) Py_DECREF(TypeVar_annotations); return 0; } - cond = PyDict_SetItemString(TypeVar_annotations, "default_", type) == 0; + cond = PyDict_SetItemString(TypeVar_annotations, "default", type) == 0; Py_DECREF(type); if (!cond) { Py_DECREF(TypeVar_annotations); @@ -6284,7 +6286,7 @@ init_types(struct ast_state *state) if (!state->TypeIgnore_type) return -1; state->type_param_type = make_type(state, "type_param", state->AST_type, NULL, 0, - "type_param = TypeVar(identifier name, expr? bound, expr? default_)\n" + "type_param = TypeVar(identifier name, expr? bound, expr? default)\n" " | ParamSpec(identifier name, expr? default_)\n" " | TypeVarTuple(identifier name, expr? default_)"); if (!state->type_param_type) return -1; @@ -6292,11 +6294,11 @@ init_types(struct ast_state *state) < 0) return -1; state->TypeVar_type = make_type(state, "TypeVar", state->type_param_type, TypeVar_fields, 3, - "TypeVar(identifier name, expr? bound, expr? default_)"); + "TypeVar(identifier name, expr? bound, expr? default)"); if (!state->TypeVar_type) return -1; if (PyObject_SetAttr(state->TypeVar_type, state->bound, Py_None) == -1) return -1; - if (PyObject_SetAttr(state->TypeVar_type, state->default_, Py_None) == -1) + if (PyObject_SetAttr(state->TypeVar_type, state->default, Py_None) == -1) return -1; state->ParamSpec_type = make_type(state, "ParamSpec", state->type_param_type, ParamSpec_fields, @@ -8103,9 +8105,8 @@ _PyAST_TypeIgnore(int lineno, string tag, PyArena *arena) } type_param_ty -_PyAST_TypeVar(identifier name, expr_ty bound, expr_ty default_, int lineno, - int col_offset, int end_lineno, int end_col_offset, PyArena - *arena) +_PyAST_TypeVar(identifier name, expr_ty bound, expr_ty default, int lineno, int + col_offset, int end_lineno, int end_col_offset, PyArena *arena) { type_param_ty p; if (!name) { @@ -8119,7 +8120,7 @@ _PyAST_TypeVar(identifier name, expr_ty bound, expr_ty default_, int lineno, p->kind = TypeVar_kind; p->v.TypeVar.name = name; p->v.TypeVar.bound = bound; - p->v.TypeVar.default_ = default_; + p->v.TypeVar.default = default; p->lineno = lineno; p->col_offset = col_offset; p->end_lineno = end_lineno; @@ -10132,9 +10133,9 @@ ast2obj_type_param(struct ast_state *state, struct validator *vstate, void* _o) if (PyObject_SetAttr(result, state->bound, value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_expr(state, vstate, o->v.TypeVar.default_); + value = ast2obj_expr(state, vstate, o->v.TypeVar.default); if (!value) goto failed; - if (PyObject_SetAttr(result, state->default_, value) == -1) + if (PyObject_SetAttr(result, state->default, value) == -1) goto failed; Py_DECREF(value); break; @@ -17003,7 +17004,7 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out, if (isinstance) { identifier name; expr_ty bound; - expr_ty default_; + expr_ty default; if (PyObject_GetOptionalAttr(obj, state->name, &tmp) < 0) { return -1; @@ -17039,24 +17040,24 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out, if (res != 0) goto failed; Py_CLEAR(tmp); } - if (PyObject_GetOptionalAttr(obj, state->default_, &tmp) < 0) { + if (PyObject_GetOptionalAttr(obj, state->default, &tmp) < 0) { return -1; } if (tmp == NULL || tmp == Py_None) { Py_CLEAR(tmp); - default_ = NULL; + default = NULL; } else { int res; if (_Py_EnterRecursiveCall(" while traversing 'TypeVar' node")) { goto failed; } - res = obj2ast_expr(state, tmp, &default_, arena); + res = obj2ast_expr(state, tmp, &default, arena); _Py_LeaveRecursiveCall(); if (res != 0) goto failed; Py_CLEAR(tmp); } - *out = _PyAST_TypeVar(name, bound, default_, lineno, col_offset, + *out = _PyAST_TypeVar(name, bound, default, lineno, col_offset, end_lineno, end_col_offset, arena); if (*out == NULL) goto failed; return 0;