Skip to content

Commit ff7bb56

Browse files
authored
pythongh-139924: Add PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME event for function watchers (python#139925)
Add PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME event for function watchers
1 parent d9b4eef commit ff7bb56

File tree

5 files changed

+12
-1
lines changed

5 files changed

+12
-1
lines changed

Doc/c-api/function.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ There are a few functions specific to Python functions.
175175
176176
.. versionadded:: 3.12
177177
178+
- ``PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME``
179+
180+
.. versionadded:: 3.15
178181
179182
.. c:type:: int (*PyFunction_WatchCallback)(PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value)
180183

Include/cpython/funcobject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
134134
V(DESTROY) \
135135
V(MODIFY_CODE) \
136136
V(MODIFY_DEFAULTS) \
137-
V(MODIFY_KWDEFAULTS)
137+
V(MODIFY_KWDEFAULTS) \
138+
V(MODIFY_QUALNAME)
138139

139140
typedef enum {
140141
#define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,

Lib/test/test_capi/test_watchers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ def myfunc():
514514
_testcapi.set_func_kwdefaults_via_capi(myfunc, new_kwdefaults)
515515
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_KWDEFAULTS, myfunc, new_kwdefaults), events)
516516

517+
new_qualname = "foo.bar"
518+
myfunc.__qualname__ = new_qualname
519+
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_QUALNAME, myfunc, new_qualname), events)
520+
517521
# Clear events reference to func
518522
events = []
519523
del myfunc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Function watchers can now receive a PyFunction_PYFUNC_EVENT_MODIFY_QUALNAME event when a watched functions qualname is changed.

Objects/funcobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
6262
case PyFunction_EVENT_MODIFY_CODE:
6363
case PyFunction_EVENT_MODIFY_DEFAULTS:
6464
case PyFunction_EVENT_MODIFY_KWDEFAULTS:
65+
case PyFunction_EVENT_MODIFY_QUALNAME:
6566
RARE_EVENT_INTERP_INC(interp, func_modification);
6667
break;
6768
default:
@@ -747,6 +748,7 @@ func_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
747748
"__qualname__ must be set to a string object");
748749
return -1;
749750
}
751+
handle_func_event(PyFunction_EVENT_MODIFY_QUALNAME, (PyFunctionObject *) op, value);
750752
Py_XSETREF(op->func_qualname, Py_NewRef(value));
751753
return 0;
752754
}

0 commit comments

Comments
 (0)