https://github.com/python/cpython/commit/b4be61ef71908714c359cd65c4d374d77b6ddd52
commit: b4be61ef71908714c359cd65c4d374d77b6ddd52
branch: main
author: Inada Naoki <[email protected]>
committer: methane <[email protected]>
date: 2026-10-29T07:24:55Z
summary:

Avoid bound method allocation in PyFile_WriteObject (#160803)

files:
M Objects/fileobject.c

diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 79aa8889c1aa4f2..d018a0257726e47 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -106,27 +106,20 @@ PyFile_GetLine(PyObject *f, int n)
 int
 PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
 {
-    PyObject *writer, *value, *result;
-
     if (f == NULL) {
         PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
         return -1;
     }
-    writer = PyObject_GetAttr(f, &_Py_ID(write));
-    if (writer == NULL)
-        return -1;
-    if (flags & Py_PRINT_RAW) {
-        value = PyObject_Str(v);
-    }
-    else
-        value = PyObject_Repr(v);
+
+    Py_INCREF(f);
+    PyObject *value = (flags & Py_PRINT_RAW) ? PyObject_Str(v) : 
PyObject_Repr(v);
     if (value == NULL) {
-        Py_DECREF(writer);
+        Py_DECREF(f);
         return -1;
     }
-    result = PyObject_CallOneArg(writer, value);
+    PyObject *result = PyObject_CallMethodOneArg(f, &_Py_ID(write), value);
     Py_DECREF(value);
-    Py_DECREF(writer);
+    Py_DECREF(f);
     if (result == NULL)
         return -1;
     Py_DECREF(result);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to