File tree 6 files changed +67
-22
lines changed
6 files changed +67
-22
lines changed Original file line number Diff line number Diff line change @@ -211,6 +211,22 @@ \section{\module{sys} ---
211
211
\versionadded {2.2}
212
212
\end {funcdesc }
213
213
214
+ \begin {funcdesc }{getfilesystemencoding}{}
215
+ Return the name of the encoding used to convert Unicode filenames
216
+ into system file names, or \code {None} if the system default encoding
217
+ is used. The result value depends on the operating system:
218
+ \begin {itemize }
219
+ \item On Windows 9x, the encoding is `` mbcs'' .
220
+ \item On Mac OS X, the encoding is `` utf-8'' .
221
+ \item On Unix, the encoding is the user's preference
222
+ according to the result of nl_langinfo(CODESET), or None if
223
+ the nl_langinfo(CODESET) failed.
224
+ \item On Windows NT+, file names are Unicode natively, so no conversion
225
+ is performed.
226
+ \end {itemize }
227
+ \versionadded {2.3}
228
+ \end {funcdesc }
229
+
214
230
\begin {funcdesc }{getrefcount}{object}
215
231
Return the reference count of the \var {object}. The count returned
216
232
is generally one higher than you might expect, because it includes
Original file line number Diff line number Diff line change @@ -109,8 +109,7 @@ def fcmp(x, y): # fuzzy comparison function
109
109
TESTFN_UNICODE = "@test-\xe0 \xf2 "
110
110
else :
111
111
TESTFN_UNICODE = unicode ("@test-\xe0 \xf2 " , "latin-1" ) # 2 latin characters.
112
- if os .name == "nt" :
113
- TESTFN_ENCODING = "mbcs"
112
+ TESTFN_ENCODING = sys .getfilesystemencoding ()
114
113
else :
115
114
TESTFN = 'test'
116
115
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ What's New in Python 2.3 beta 1?
12
12
Core and builtins
13
13
-----------------
14
14
15
+ - sys.getfilesystemencoding() was added to expose
16
+ Py_FileSystemDefaultEncoding.
15
17
16
18
- New function sys.exc_clear() clears the current exception. This is
17
19
rarely needed, but can sometimes be useful to release objects
Original file line number Diff line number Diff line change @@ -161,10 +161,6 @@ fixup_ulcase(void)
161
161
Py_DECREF (ulo );
162
162
}
163
163
164
- #if defined(HAVE_LANGINFO_H ) && defined(CODESET )
165
- static int fileencoding_uses_locale = 0 ;
166
- #endif
167
-
168
164
static PyObject *
169
165
PyLocale_setlocale (PyObject * self , PyObject * args )
170
166
{
@@ -213,22 +209,6 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
213
209
fixup_ulcase ();
214
210
/* things that got wrong up to here are ignored */
215
211
PyErr_Clear ();
216
- #if defined(HAVE_LANGINFO_H ) && defined(CODESET )
217
- if (Py_FileSystemDefaultEncoding == NULL )
218
- fileencoding_uses_locale = 1 ;
219
- if (fileencoding_uses_locale ) {
220
- char * codeset = nl_langinfo (CODESET );
221
- PyObject * enc = NULL ;
222
- if (* codeset && (enc = PyCodec_Encoder (codeset ))) {
223
- /* Release previous file encoding */
224
- if (Py_FileSystemDefaultEncoding )
225
- free ((char * )Py_FileSystemDefaultEncoding );
226
- Py_FileSystemDefaultEncoding = strdup (codeset );
227
- Py_DECREF (enc );
228
- } else
229
- PyErr_Clear ();
230
- }
231
- #endif
232
212
} else {
233
213
/* get locale */
234
214
/* restore LC_NUMERIC first, if appropriate */
Original file line number Diff line number Diff line change 17
17
#include <signal.h>
18
18
#endif
19
19
20
+ #ifdef HAVE_LANGINFO_H
21
+ #include <locale.h>
22
+ #include <langinfo.h>
23
+ #endif
24
+
20
25
#ifdef MS_WINDOWS
21
26
#undef BYTE
22
27
#include "windows.h"
@@ -181,6 +186,29 @@ Py_Initialize(void)
181
186
initsite (); /* Module site */
182
187
183
188
PyModule_WarningsModule = PyImport_ImportModule ("warnings" );
189
+
190
+ #if defined(Py_USING_UNICODE ) && defined(HAVE_LANGINFO_H ) && defined(CODESET )
191
+ /* On Unix, set the file system encoding according to the
192
+ user's preference, if the CODESET names a well-known
193
+ Python codec, and Py_FileSystemDefaultEncoding isn't
194
+ initialized by other means. */
195
+ if (!Py_FileSystemDefaultEncoding ) {
196
+ char * saved_locale = setlocale (LC_CTYPE , NULL );
197
+ char * codeset ;
198
+ setlocale (LC_CTYPE , "" );
199
+ codeset = nl_langinfo (CODESET );
200
+ PyObject * enc = NULL ;
201
+ if (* codeset ) {
202
+ enc = PyCodec_Encoder (codeset );
203
+ if (enc ) {
204
+ Py_FileSystemDefaultEncoding = strdup (codeset );
205
+ Py_DECREF (enc );
206
+ } else
207
+ PyErr_Clear ();
208
+ }
209
+ setlocale (LC_CTYPE , saved_locale );
210
+ }
211
+ #endif
184
212
}
185
213
186
214
#ifdef COUNT_ALLOCS
Original file line number Diff line number Diff line change @@ -236,6 +236,22 @@ PyDoc_STRVAR(setdefaultencoding_doc,
236
236
Set the current default string encoding used by the Unicode implementation."
237
237
);
238
238
239
+ static PyObject *
240
+ sys_getfilesystemencoding (PyObject * self )
241
+ {
242
+ if (Py_FileSystemDefaultEncoding )
243
+ return PyString_FromString (Py_FileSystemDefaultEncoding );
244
+ Py_INCREF (Py_None );
245
+ return Py_None ;
246
+ }
247
+
248
+ PyDoc_STRVAR (getfilesystemencoding_doc ,
249
+ "getfilesystemencoding() -> string\n\
250
+ \n\
251
+ Return the encoding used to convert Unicode filenames in\n\
252
+ operating system filenames."
253
+ );
254
+
239
255
#endif
240
256
241
257
/*
@@ -649,6 +665,10 @@ static PyMethodDef sys_methods[] = {
649
665
#ifdef DYNAMIC_EXECUTION_PROFILE
650
666
{"getdxp" , _Py_GetDXProfile , METH_VARARGS },
651
667
#endif
668
+ #ifdef Py_USING_UNICODE
669
+ {"getfilesystemencoding" , (PyCFunction )sys_getfilesystemencoding ,
670
+ METH_NOARGS , getfilesystemencoding_doc },
671
+ #endif
652
672
#ifdef Py_TRACE_REFS
653
673
{"getobjects" , _Py_GetObjects , METH_VARARGS },
654
674
#endif
You can’t perform that action at this time.
0 commit comments