Skip to content

Commit a026992

Browse files
committed
Avoid bound method allocation in PyFile_WriteObject
1 parent 75d96f8 commit a026992

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

‎Objects/fileobject.c‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,20 @@ PyFile_GetLine(PyObject *f, int n)
106106
int
107107
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
108108
{
109-
PyObject *writer, *value, *result;
110-
111109
if (f == NULL) {
112110
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
113111
return -1;
114112
}
115-
writer = PyObject_GetAttr(f, &_Py_ID(write));
116-
if (writer == NULL)
117-
return -1;
118-
if (flags & Py_PRINT_RAW) {
119-
value = PyObject_Str(v);
120-
}
121-
else
122-
value = PyObject_Repr(v);
113+
114+
Py_INCREF(f);
115+
PyObject *value = (flags & Py_PRINT_RAW) ? PyObject_Str(v) : PyObject_Repr(v);
123116
if (value == NULL) {
124-
Py_DECREF(writer);
117+
Py_DECREF(f);
125118
return -1;
126119
}
127-
result = PyObject_CallOneArg(writer, value);
120+
PyObject *result = PyObject_CallMethodOneArg(f, &_Py_ID(write), value);
128121
Py_DECREF(value);
129-
Py_DECREF(writer);
122+
Py_DECREF(f);
130123
if (result == NULL)
131124
return -1;
132125
Py_DECREF(result);

0 commit comments

Comments
 (0)