@@ -116,6 +116,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
116116
117117 Py_DECREF (f -> f_name );
118118 Py_DECREF (f -> f_mode );
119+ Py_DECREF (f -> f_encoding );
119120#ifdef Py_USING_UNICODE
120121 if (wname )
121122 f -> f_name = PyUnicode_FromObject (wname );
@@ -133,7 +134,9 @@ fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
133134 f -> f_newlinetypes = NEWLINE_UNKNOWN ;
134135 f -> f_skipnextlf = 0 ;
135136#endif
136-
137+ Py_INCREF (Py_None );
138+ f -> f_encoding = Py_None ;
139+
137140 if (f -> f_name == NULL || f -> f_mode == NULL )
138141 return NULL ;
139142 f -> f_fp = fp ;
@@ -302,6 +305,21 @@ PyFile_SetBufSize(PyObject *f, int bufsize)
302305 }
303306}
304307
308+ /* Set the encoding used to output Unicode strings.
309+ Returh 1 on success, 0 on failure. */
310+
311+ int
312+ PyFile_SetEncoding (PyObject * f , const char * enc )
313+ {
314+ PyFileObject * file = (PyFileObject * )f ;
315+ PyObject * str = PyString_FromString (enc );
316+ if (!str )
317+ return 0 ;
318+ Py_DECREF (file -> f_encoding );
319+ file -> f_encoding = str ;
320+ return 1 ;
321+ }
322+
305323static PyObject *
306324err_closed (void )
307325{
@@ -323,6 +341,7 @@ file_dealloc(PyFileObject *f)
323341 }
324342 Py_XDECREF (f -> f_name );
325343 Py_XDECREF (f -> f_mode );
344+ Py_XDECREF (f -> f_encoding );
326345 drop_readahead (f );
327346 f -> ob_type -> tp_free ((PyObject * )f );
328347}
@@ -1667,6 +1686,8 @@ static PyMemberDef file_memberlist[] = {
16671686 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)" },
16681687 {"name" , T_OBJECT , OFF (f_name ), RO ,
16691688 "file name" },
1689+ {"encoding" , T_OBJECT , OFF (f_encoding ), RO ,
1690+ "file encoding" },
16701691 /* getattr(f, "closed") is implemented without this table */
16711692 {NULL } /* Sentinel */
16721693};
@@ -1851,6 +1872,8 @@ file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18511872 ((PyFileObject * )self )-> f_name = not_yet_string ;
18521873 Py_INCREF (not_yet_string );
18531874 ((PyFileObject * )self )-> f_mode = not_yet_string ;
1875+ Py_INCREF (Py_None );
1876+ ((PyFileObject * )self )-> f_encoding = Py_None ;
18541877 }
18551878 return self ;
18561879}
@@ -2034,11 +2057,28 @@ PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
20342057 }
20352058 else if (PyFile_Check (f )) {
20362059 FILE * fp = PyFile_AsFile (f );
2060+ PyObject * enc = ((PyFileObject * )f )-> f_encoding ;
2061+ int result ;
20372062 if (fp == NULL ) {
20382063 err_closed ();
20392064 return -1 ;
20402065 }
2066+ #ifdef Py_USING_UNICODE
2067+ if (PyUnicode_Check (v ) && enc != Py_None ) {
2068+ char * cenc = PyString_AS_STRING (enc );
2069+ value = PyUnicode_AsEncodedString (v , cenc , "strict" );
2070+ if (value == NULL )
2071+ return -1 ;
2072+ } else {
2073+ value = v ;
2074+ Py_INCREF (value );
2075+ }
2076+ result = PyObject_Print (value , fp , flags );
2077+ Py_DECREF (value );
2078+ return result ;
2079+ #else
20412080 return PyObject_Print (v , fp , flags );
2081+ #endif
20422082 }
20432083 writer = PyObject_GetAttrString (f , "write" );
20442084 if (writer == NULL )
0 commit comments