Patch #462849: Pass Unicode objects to file's .write method.
diff --git a/Misc/NEWS b/Misc/NEWS
index f103705..9ea0ac2 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -3,6 +3,11 @@
Core
+- PyFile_WriteObject now passes Unicode object to the file's write
+ method. As a result, all file-like object which may be the target
+ of a print statement must support Unicode objects, i.e. they must
+ at least convert them into ASCII strings.
+
- The builtin file type can be subclassed now. In the usual pattern,
"file" is the name of the builtin type, and file() is a new builtin
constructor, with the same signature as the builtin open() function.
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 779b5fa..64bdb65 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1503,9 +1503,14 @@
writer = PyObject_GetAttrString(f, "write");
if (writer == NULL)
return -1;
- if (flags & Py_PRINT_RAW)
- value = PyObject_Str(v);
- else
+ if (flags & Py_PRINT_RAW) {
+ if (PyUnicode_Check(v)) {
+ value = v;
+ Py_INCREF(value);
+ } else
+ value = PyObject_Str(v);
+ }
+ else
value = PyObject_Repr(v);
if (value == NULL) {
Py_DECREF(writer);