Patch #612627: Add encoding attribute to file objects, and determine
the terminal encoding on Windows and Unix.
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 92cfa5b..40ce759 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -116,6 +116,7 @@
 
 	Py_DECREF(f->f_name);
 	Py_DECREF(f->f_mode);
+	Py_DECREF(f->f_encoding);
 #ifdef Py_USING_UNICODE
 	if (wname)
 		f->f_name = PyUnicode_FromObject(wname);
@@ -133,7 +134,9 @@
 	f->f_newlinetypes = NEWLINE_UNKNOWN;
 	f->f_skipnextlf = 0;
 #endif
-
+	Py_INCREF(Py_None);
+	f->f_encoding = Py_None;
+	
 	if (f->f_name == NULL || f->f_mode == NULL)
 		return NULL;
 	f->f_fp = fp;
@@ -302,6 +305,21 @@
 	}
 }
 
+/* Set the encoding used to output Unicode strings.
+   Returh 1 on success, 0 on failure. */
+
+int
+PyFile_SetEncoding(PyObject *f, const char *enc)
+{
+	PyFileObject *file = (PyFileObject*)f;
+	PyObject *str = PyString_FromString(enc);
+	if (!str)
+		return 0;
+	Py_DECREF(file->f_encoding);
+	file->f_encoding = str;
+	return 1;
+}
+
 static PyObject *
 err_closed(void)
 {
@@ -323,6 +341,7 @@
 	}
 	Py_XDECREF(f->f_name);
 	Py_XDECREF(f->f_mode);
+	Py_XDECREF(f->f_encoding);
 	drop_readahead(f);
 	f->ob_type->tp_free((PyObject *)f);
 }
@@ -1667,6 +1686,8 @@
 	 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
 	{"name",	T_OBJECT,	OFF(f_name),	RO,
 	 "file name"},
+	{"encoding",	T_OBJECT,	OFF(f_encoding),	RO,
+	 "file encoding"},
 	/* getattr(f, "closed") is implemented without this table */
 	{NULL}	/* Sentinel */
 };
@@ -1851,6 +1872,8 @@
 		((PyFileObject *)self)->f_name = not_yet_string;
 		Py_INCREF(not_yet_string);
 		((PyFileObject *)self)->f_mode = not_yet_string;
+		Py_INCREF(Py_None);
+		((PyFileObject *)self)->f_encoding = Py_None;
 	}
 	return self;
 }
@@ -2034,11 +2057,28 @@
 	}
 	else if (PyFile_Check(f)) {
 		FILE *fp = PyFile_AsFile(f);
+		PyObject *enc = ((PyFileObject*)f)->f_encoding;
+		int result;
 		if (fp == NULL) {
 			err_closed();
 			return -1;
 		}
+#ifdef Py_USING_UNICODE
+                if (PyUnicode_Check(v) && enc != Py_None) {
+			char *cenc = PyString_AS_STRING(enc);
+			value = PyUnicode_AsEncodedString(v, cenc, "strict");
+			if (value == NULL)
+				return -1;
+		} else {
+			value = v;
+			Py_INCREF(value);
+		}
+		result = PyObject_Print(value, fp, flags);
+		Py_DECREF(value);
+		return result;
+#else
 		return PyObject_Print(v, fp, flags);
+#endif
 	}
 	writer = PyObject_GetAttrString(f, "write");
 	if (writer == NULL)