@@ -116,6 +116,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
116
116
117
117
Py_DECREF (f -> f_name );
118
118
Py_DECREF (f -> f_mode );
119
+ Py_DECREF (f -> f_encoding );
119
120
#ifdef Py_USING_UNICODE
120
121
if (wname )
121
122
f -> f_name = PyUnicode_FromObject (wname );
@@ -133,7 +134,9 @@ fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
133
134
f -> f_newlinetypes = NEWLINE_UNKNOWN ;
134
135
f -> f_skipnextlf = 0 ;
135
136
#endif
136
-
137
+ Py_INCREF (Py_None );
138
+ f -> f_encoding = Py_None ;
139
+
137
140
if (f -> f_name == NULL || f -> f_mode == NULL )
138
141
return NULL ;
139
142
f -> f_fp = fp ;
@@ -302,6 +305,21 @@ PyFile_SetBufSize(PyObject *f, int bufsize)
302
305
}
303
306
}
304
307
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
+
305
323
static PyObject *
306
324
err_closed (void )
307
325
{
@@ -323,6 +341,7 @@ file_dealloc(PyFileObject *f)
323
341
}
324
342
Py_XDECREF (f -> f_name );
325
343
Py_XDECREF (f -> f_mode );
344
+ Py_XDECREF (f -> f_encoding );
326
345
drop_readahead (f );
327
346
f -> ob_type -> tp_free ((PyObject * )f );
328
347
}
@@ -1667,6 +1686,8 @@ static PyMemberDef file_memberlist[] = {
1667
1686
"file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)" },
1668
1687
{"name" , T_OBJECT , OFF (f_name ), RO ,
1669
1688
"file name" },
1689
+ {"encoding" , T_OBJECT , OFF (f_encoding ), RO ,
1690
+ "file encoding" },
1670
1691
/* getattr(f, "closed") is implemented without this table */
1671
1692
{NULL } /* Sentinel */
1672
1693
};
@@ -1851,6 +1872,8 @@ file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1851
1872
((PyFileObject * )self )-> f_name = not_yet_string ;
1852
1873
Py_INCREF (not_yet_string );
1853
1874
((PyFileObject * )self )-> f_mode = not_yet_string ;
1875
+ Py_INCREF (Py_None );
1876
+ ((PyFileObject * )self )-> f_encoding = Py_None ;
1854
1877
}
1855
1878
return self ;
1856
1879
}
@@ -2034,11 +2057,28 @@ PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
2034
2057
}
2035
2058
else if (PyFile_Check (f )) {
2036
2059
FILE * fp = PyFile_AsFile (f );
2060
+ PyObject * enc = ((PyFileObject * )f )-> f_encoding ;
2061
+ int result ;
2037
2062
if (fp == NULL ) {
2038
2063
err_closed ();
2039
2064
return -1 ;
2040
2065
}
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
2041
2080
return PyObject_Print (v , fp , flags );
2081
+ #endif
2042
2082
}
2043
2083
writer = PyObject_GetAttrString (f , "write" );
2044
2084
if (writer == NULL )
0 commit comments