Skip to content

Commit

Permalink
Python3.12: Fix, compiled function "__type_params__" needs to be writ…
Browse files Browse the repository at this point in the history
…able

* Otherwise "functools.wraps" fails copying it over.
  • Loading branch information
kayhayen committed May 15, 2024
1 parent da0f277 commit 5ed85ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions nuitka/build/include/nuitka/compiled_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ struct Nuitka_FunctionObject {
PyObject *m_qualname;
#endif

#if PYTHON_VERSION >= 0x3c0
PyObject *m_type_params;
#endif

// Constant return value to use.
PyObject *m_constant_return_value;

Expand Down
17 changes: 16 additions & 1 deletion nuitka/build/static_src/CompiledFunctionType.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,19 @@ static PyObject *Nuitka_Function_get_builtins(struct Nuitka_FunctionObject *func
#if PYTHON_VERSION >= 0x3c0
static int Nuitka_Function_set_type_params(struct Nuitka_FunctionObject *function, PyObject *value) {
CHECK_OBJECT((PyObject *)function);
CHECK_OBJECT_X(value);
assert(Nuitka_Function_Check((PyObject *)function));
assert(_PyObject_GC_IS_TRACKED(function));

if (unlikely(value == NULL || !PyTuple_Check(value))) {
PyThreadState *tstate = PyThreadState_GET();

SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "readonly attribute");
SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_TypeError, "__type_params__ must be set to a tuple");
return -1;
}

Py_SETREF(function->m_type_params, Py_NewRef(value));
return 0;
}

static PyObject *Nuitka_Function_get_type_params(struct Nuitka_FunctionObject *function) {
Expand Down Expand Up @@ -729,6 +735,10 @@ static void Nuitka_Function_tp_dealloc(struct Nuitka_FunctionObject *function) {
Py_DECREF(function->m_qualname);
#endif

#if PYTHON_VERSION >= 0x3c0
Py_DECREF(function->m_type_params);
#endif

// These may actually resurrect the object, not?
Py_XDECREF(function->m_dict);
Py_DECREF(function->m_defaults);
Expand Down Expand Up @@ -1318,6 +1328,11 @@ struct Nuitka_FunctionObject *Nuitka_Function_New(function_impl_code c_code, PyO
result->m_vectorcall = (vectorcallfunc)Nuitka_Function_tp_vectorcall;
#endif

#if PYTHON_VERSION >= 0x3c0
result->m_type_params = const_tuple_empty;
assert(_Py_IsImmortal(result->m_type_params));
#endif

Nuitka_GC_Track(result);

assert(Py_REFCNT(result) == 1);
Expand Down

0 comments on commit 5ed85ff

Please sign in to comment.