8000 Avoid a new funcflags · python/cpython@f448248 · GitHub
[go: up one dir, main page]

Skip to content

Commit f448248

Browse files
committed
Avoid a new funcflags
1 parent 77b3807 commit f448248

11 files changed

+91
-73
lines changed

Include/cpython/funcobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct {
4141
PyObject *func_weakreflist; /* List of weak references */
4242
PyObject *func_module; /* The __module__ attribute, can be anything */
4343
PyObject *func_annotations; /* Annotations, a dict or NULL */
44-
PyObject *func_typevars; /* Tuple of active type variables or NULL */
44+
PyObject *func_typeparams; /* Tuple of active type variables or NULL */
4545
vectorcallfunc vectorcall;
4646
/* Version number for use by specializer.
4747
* Can set to non-zero when we want to specialize.

Include/internal/pycore_function.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ struct _py_func_state {
1717
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);
1818

1919
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
20+
extern PyObject *_Py_set_function_type_params(
21+
PyThreadState* unused, PyObject *func, PyObject *type_params);
2022

2123
#ifdef __cplusplus
2224
}

Include/internal/pycore_intrinsics.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#define INTRINSIC_PREP_RERAISE_STAR 1
2222
#define INTRINSIC_TYPEVAR_WITH_BOUND 2
2323
#define INTRINSIC_TYPEVAR_WITH_CONSTRAINTS 3
24+
#define INTRINSIC_SET_FUNCTION_TYPE_PARAMS 4
2425

25-
#define MAX_INTRINSIC_2 3
26+
#define MAX_INTRINSIC_2 4
2627

2728

2829
typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);

Objects/funcobject.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
127127
PyErr_Clear();
128128
}
129129
op->func_annotations = NULL;
130-
op->func_typevars = NULL;
130+
op->func_typeparams = NULL;
131131
op->vectorcall = _PyFunction_Vectorcall;
132132
op->func_version = 0;
133133
_PyObject_GC_TRACK(op);
@@ -203,7 +203,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
203203
op->func_weakreflist = NULL;
204204
op->func_module = module;
205205
op->func_annotations = NULL;
206-
op->func_typevars = NULL;
206+
op->func_typeparams = NULL;
207207
op->vectorcall = _PyFunction_Vectorcall;
208208
op->func_version = 0;
209209
_PyObject_GC_TRACK(op);
@@ -657,14 +657,29 @@ func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(igno
657657
static PyObject *
658658
func_get_type_params(PyFunctionObject *op, void *Py_UNUSED(ignored))
659659
{
660-
if (op->func_typevars == NULL) {
660+
if (op->func_typeparams == NULL) {
661661
return PyTuple_New(0);
662662
}
663663

664-
assert(PyTuple_Check(op->func_typevars));
665-
return Py_NewRef(op->func_typevars);
664+
assert(PyTuple_Check(op->func_typeparams));
665+
return Py_NewRef(op->func_typeparams);
666666
}
667667

668+
PyObject *
669+
_Py_set_function_type_params(PyThreadState* unused, PyObject *func,
670+
PyObject *type_params)
671+
{
672+
PyFunctionObject *f = (PyFunctionObject *)func;
673+
if (!PyTuple_Check(type_params)) {
674+
PyErr_SetString(PyExc_TypeError,
675+
"__type_params__ must be set to a tuple object");
676+
return NULL;
677+
}
678+
Py_XSETREF(f->func_typeparams, Py_NewRef(type_params));
679+
return Py_None;
680+
}
681+
682+
668683
static PyGetSetDef func_getsetlist[] = {
669684
{"__code__", (getter)func_get_code, (setter)func_set_code},
670685
{"__defaults__", (getter)func_get_defaults,
@@ -797,7 +812,7 @@ func_clear(PyFunctionObject *op)
797812
Py_CLEAR(op->func_dict);
798813
Py_CLEAR(op->func_closure);
799814
Py_CLEAR(op->func_annotations);
800-
Py_CLEAR(op->func_typevars);
815+
Py_CLEAR(op->func_typeparams);
801816
// Don't Py_CLEAR(op->func_code), since code is always required
802817
// to be non-NULL. Similarly, name and qualname shouldn't be NULL.
803818
// However, name and qualname could be str subclasses, so they
@@ -852,7 +867,7 @@ func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
852867
Py_VISIT(f->func_dict);
853868
Py_VISIT(f->func_closure);
854869
Py_VISIT(f->func_annotations);
855-
Py_VISIT(f->func_typevars);
870+
Py_VISIT(f->func_typeparams);
856871
Py_VISIT(f->func_qualname);
857872
return 0;
858873
}

Objects/typevarobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,8 +1580,8 @@ static void
15801580
generic_dealloc(PyObject *self)
15811581
{
15821582
PyTypeObject *tp = Py_TYPE(self);
1583-
Py_TYPE(self)->tp_free(self);
15841583
_PyObject_GC_UNTRACK(self);
1584+
Py_TYPE(self)->tp_free(self);
15851585
Py_DECREF(tp);
15861586
}
15871587

Python/bytecodes.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,7 +3185,6 @@ dummy_func(
31853185

31863186
inst(MAKE_FUNCTION, (defaults if (oparg & 0x01),
31873187
kwdefaults if (oparg & 0x02),
3188-
typevars if (oparg & 0x10),
31893188
annotations if (oparg & 0x04),
31903189
closure if (oparg & 0x08),
31913190
codeobj -- func)) {
@@ -3198,10 +3197,6 @@ dummy_func(
31983197
goto error;
31993198
}
32003199

3201-
if (oparg & 0x10) {
3202-
assert(PyTuple_CheckExact(typevars));
3203-
func_obj->func_typevars = typevars;
3204-
}
32053200
if (oparg & 0x08) {
32063201
assert(PyTuple_CheckExact(closure));
32073202
func_obj->func_closure = closure;

Python/compile.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,9 +2214,9 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22142214
}
22152215

22162216
int num_typeparam_args = 0;
2217+
_Py_DECLARE_STR(type_params, ".type_params");
22172218

22182219
if (asdl_seq_LEN(typeparams) > 0) {
2219-
funcflags |= 0x10;
22202220
PyObject *typeparams_name = PyUnicode_FromFormat("<generic parameters of %U>", name);
22212221
if (!typeparams_name) {
22222222
return ERROR;
@@ -2239,6 +2239,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22392239
num_typeparam_args += 1;
22402240
}
22412241
RETURN_IF_ERROR(compiler_type_params(c, typeparams));
2242+
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(type_params), Store));
22422243
}
22432244

22442245
annotations = compiler_visit_annotations(c, loc, args, returns);
@@ -2283,6 +2284,11 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22832284
}
22842285
Py_DECREF(co);
22852286
if (asdl_seq_LEN(typeparams) > 0) {
2287+
ADDOP_I(c, loc, COPY, 1);
2288+
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(type_params), Load));
2289+
ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_FUNCTION_TYPE_PARAMS);
2290+
ADDOP(c, loc, POP_TOP);
2291+
22862292
if (is_typeparams_in_class) {
22872293
c->u->u_metadata.u_argcount += 1;
22882294
}

0 commit comments

Comments
 (0)
0