]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-139924: Add PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME event for function watchers...
authorDino Viehland <dinoviehland@meta.com>
Fri, 10 Oct 2025 22:25:38 +0000 (15:25 -0700)
committerGitHub <noreply@github.com>
Fri, 10 Oct 2025 22:25:38 +0000 (15:25 -0700)
Add PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME event for function watchers

Doc/c-api/function.rst
Include/cpython/funcobject.h
Lib/test/test_capi/test_watchers.py
Misc/NEWS.d/next/C_API/2025-10-10-20-59-07.gh-issue-139924.ALByCb.rst [new file with mode: 0644]
Objects/funcobject.c

index 5fb8567ef8c95fbec9a7b99f208c07955ba8efa9..764b2ac610be4db8ae0d12dee9f1d6d689f0c8e7 100644 (file)
@@ -175,6 +175,9 @@ There are a few functions specific to Python functions.
 
    .. versionadded:: 3.12
 
+    - ``PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME``
+
+   .. versionadded:: 3.15
 
 .. c:type:: int (*PyFunction_WatchCallback)(PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value)
 
index 598cd330bc9ca958a50a4ebc6ea09892d4a693d6..9e1599a76485646166183c0af1582ae260fead06 100644 (file)
@@ -134,7 +134,8 @@ PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
     V(DESTROY)                   \
     V(MODIFY_CODE)               \
     V(MODIFY_DEFAULTS)           \
-    V(MODIFY_KWDEFAULTS)
+    V(MODIFY_KWDEFAULTS)         \
+    V(MODIFY_QUALNAME)
 
 typedef enum {
     #define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,
index 8644479d83d5edece4922b6a0ea5c5cd10efec2f..bef72032513da5d13ae9ec7ce38abd7f50d59536 100644 (file)
@@ -514,6 +514,10 @@ class TestFuncWatchers(unittest.TestCase):
             _testcapi.set_func_kwdefaults_via_capi(myfunc, new_kwdefaults)
             self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_KWDEFAULTS, myfunc, new_kwdefaults), events)
 
+            new_qualname = "foo.bar"
+            myfunc.__qualname__ = new_qualname
+            self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_QUALNAME, myfunc, new_qualname), events)
+
             # Clear events reference to func
             events = []
             del myfunc
diff --git a/Misc/NEWS.d/next/C_API/2025-10-10-20-59-07.gh-issue-139924.ALByCb.rst b/Misc/NEWS.d/next/C_API/2025-10-10-20-59-07.gh-issue-139924.ALByCb.rst
new file mode 100644 (file)
index 0000000..a53d5d0
--- /dev/null
@@ -0,0 +1 @@
+Function watchers can now receive a PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME event when a watched functions qualname is changed.
index d8a1007557808751e9c95d9a4080897b23baa855..43198aaf8a70483ebc881852d70ba77094e4eab4 100644 (file)
@@ -62,6 +62,7 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
         case PyFunction_EVENT_MODIFY_CODE:
         case PyFunction_EVENT_MODIFY_DEFAULTS:
         case PyFunction_EVENT_MODIFY_KWDEFAULTS:
+        case PyFunction_EVENT_MODIFY_QUALNAME:
             RARE_EVENT_INTERP_INC(interp, func_modification);
             break;
         default:
@@ -747,6 +748,7 @@ func_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
                         "__qualname__ must be set to a string object");
         return -1;
     }
+    handle_func_event(PyFunction_EVENT_MODIFY_QUALNAME, (PyFunctionObject *) op, value);
     Py_XSETREF(op->func_qualname, Py_NewRef(value));
     return 0;
 }