diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index 86080fac7163838..90176631c51034c 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -484,6 +484,7 @@ func,PyMemoryView_GetContiguous,3.2,, data,PyMemoryView_Type,3.2,, type,PyMethodDef,3.2,,full-abi data,PyMethodDescr_Type,3.2,, +func,PyMethod_New,3.16,, type,PyModuleDef,3.2,,abi3t-opaque type,PyModuleDef_Base,3.2,,abi3t-opaque func,PyModuleDef_Init,3.5,, diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index b017535b96979d9..aa7a5f74d1a9f50 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -872,7 +872,8 @@ C API changes New features ------------ -* TODO +* Add :c:func:`PyMethod_New` function to the limited C API. + (Contributed by Nicolas Trangez in :gh:`155588`.) Porting to Python 3.16 ---------------------- diff --git a/Include/Python.h b/Include/Python.h index 337119c15fe8b6f..d51b7d88e6e7466 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -104,6 +104,7 @@ __pragma(warning(disable: 4201)) #include "moduleobject.h" #include "cpython/monitoring.h" #include "cpython/funcobject.h" +#include "classobject.h" #include "cpython/classobject.h" #include "fileobject.h" #include "pycapsule.h" diff --git a/Include/classobject.h b/Include/classobject.h new file mode 100644 index 000000000000000..a32c85c8d81fcd7 --- /dev/null +++ b/Include/classobject.h @@ -0,0 +1,24 @@ +/* Limited C API of PyMethod API + */ + +#ifndef Py_CLASSOBJECT_H +#define Py_CLASSOBJECT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000 +/* Return a new method object, with `func` being any callable object and + * `self` the instance the method should be bound. + * + * `func` is the function that will be called when the method is called. + * `self` must not be NULL. + */ +PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *); +#endif + +#ifdef __cplusplus +} +#endif +#endif /* !Py_CLASSOBJECT_H */ diff --git a/Include/cpython/classobject.h b/Include/cpython/classobject.h index d7c9ddd1336c46d..0c6bcf512faa33d 100644 --- a/Include/cpython/classobject.h +++ b/Include/cpython/classobject.h @@ -3,8 +3,8 @@ /* Revealing some structures (not for general use) */ #ifndef Py_LIMITED_API -#ifndef Py_CLASSOBJECT_H -#define Py_CLASSOBJECT_H +#ifndef Py_CPYTHON_CLASSOBJECT_H +#define Py_CPYTHON_CLASSOBJECT_H #ifdef __cplusplus extern "C" { #endif @@ -67,5 +67,5 @@ static inline PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *meth) { #ifdef __cplusplus } #endif -#endif // !Py_CLASSOBJECT_H +#endif // !Py_CPYTHON_CLASSOBJECT_H #endif // !Py_LIMITED_API diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index 09ee2d53f98f585..f43e20d9fd9decd 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -476,6 +476,7 @@ def test_windows_feature_macros(self): "PyMemoryView_GetContiguous", "PyMemoryView_Type", "PyMethodDescr_Type", + "PyMethod_New", "PyModuleDef_Init", "PyModuleDef_Type", "PyModule_Add", diff --git a/Misc/NEWS.d/next/C_API/2026-08-11-21-36-49.gh-issue-155588.y98XoK.rst b/Misc/NEWS.d/next/C_API/2026-08-11-21-36-49.gh-issue-155588.y98XoK.rst new file mode 100644 index 000000000000000..5ebac790c76782b --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-08-11-21-36-49.gh-issue-155588.y98XoK.rst @@ -0,0 +1,2 @@ +Add :c:func:`PyObject_CallFinalizerFromDealloc` function to the limited C +API. Patch by Nicolas Trangez. diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index d59a7c788fa9e02..2aafe4a1e1bd27a 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2832,3 +2832,6 @@ [function.PyObject_CallFinalizerFromDealloc] added = '3.15' + +[function.PyMethod_New] + added = '3.16' diff --git a/PC/python3dll.c b/PC/python3dll.c index e0be9d65a93cda6..1800949c7a72aa5 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -428,6 +428,7 @@ EXPORT_FUNC(PyMemoryView_FromBuffer) EXPORT_FUNC(PyMemoryView_FromMemory) EXPORT_FUNC(PyMemoryView_FromObject) EXPORT_FUNC(PyMemoryView_GetContiguous) +EXPORT_FUNC(PyMethod_New) EXPORT_FUNC(PyModule_Add) EXPORT_FUNC(PyModule_AddFunctions) EXPORT_FUNC(PyModule_AddIntConstant)