From a677bc85a6856fe3311509641a21791544b5a0c0 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 21 Nov 2026 12:25:41 +0530 Subject: [PATCH] gh-162227: Fix scaling of format() with a format spec on the free-threaded build PyObject_Format() now looks up __format__ with _PyObject_LookupSpecialMethod(), avoiding a temporary bound method and reference count contention on the shared method object. --- ...-11-21-12-00-00.gh-issue-162227.fMtScl.rst | 4 ++ Objects/abstract.c | 38 ++++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst new file mode 100644 index 00000000000000..06b6570b2cd26c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst @@ -0,0 +1,4 @@ +Improve multi-threaded scaling of :func:`format`, f-strings and +:meth:`str.format` with a format specification on the :term:`free-threaded +build` by avoiding the creation of a temporary bound method and reference +count contention on the shared ``__format__`` method. diff --git a/Objects/abstract.c b/Objects/abstract.c index ff56cdb9efb39b..635e1e927f2337 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -853,7 +853,6 @@ _PyBuffer_ReleaseInInterpreterAndRawFree(PyInterpreterState *interp, PyObject * PyObject_Format(PyObject *obj, PyObject *format_spec) { - PyObject *meth; PyObject *empty = NULL; PyObject *result = NULL; @@ -880,21 +879,32 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) format_spec = empty; } - /* Find the (unbound!) __format__ method */ - meth = _PyObject_LookupSpecial(obj, &_Py_ID(__format__)); - if (meth == NULL) { - PyThreadState *tstate = _PyThreadState_GET(); - if (!_PyErr_Occurred(tstate)) { - _PyErr_Format(tstate, PyExc_TypeError, - "Type %.100s doesn't define __format__", - Py_TYPE(obj)->tp_name); + /* Find the __format__ method and call it, without creating + a temporary bound method where possible. */ + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _PyStackRef self = PyStackRef_FromPyObjectBorrow(obj); + int found = _PyObject_LookupSpecialMethod(&_Py_ID(__format__), + &cref.ref, &self); + if (found > 0) { + PyObject *meth = PyStackRef_AsPyObjectBorrow(cref.ref); + PyObject *args[2] = {PyStackRef_AsPyObjectBorrow(self), format_spec}; + if (args[0] != NULL) { + /* Unbound method: prepend self. */ + result = PyObject_Vectorcall(meth, args, 2, NULL); + } + else { + result = PyObject_Vectorcall(meth, args + 1, 1, NULL); } - goto done; } - - /* And call it. */ - result = PyObject_CallOneArg(meth, format_spec); - Py_DECREF(meth); + else if (found == 0) { + _PyErr_Format(tstate, PyExc_TypeError, + "Type %.100s doesn't define __format__", + Py_TYPE(obj)->tp_name); + } + PyStackRef_XCLOSE(self); + _PyThreadState_PopCStackRef(tstate, &cref); if (result && !PyUnicode_Check(result)) { PyErr_Format(PyExc_TypeError,