]> granicus.if.org Git - python/commitdiff
Patch #462849: Pass Unicode objects to file's .write method.
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 19 Sep 2001 13:47:32 +0000 (13:47 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 19 Sep 2001 13:47:32 +0000 (13:47 +0000)
Misc/NEWS
Objects/fileobject.c

index f1037054d1c9211df65eb43da162fea158ea1c43..9ea0ac2d09396e48bbaf5356ae1a11994088a0d7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -3,6 +3,11 @@ What's New in Python 2.2a4?
 
 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.
index 779b5faadac90be85e4b4e3e841099b75785c34c..64bdb654a0d854878fdeda7f53b2589210ee3700 100644 (file)
@@ -1503,9 +1503,14 @@ PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
        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);