mirror of
https://github.com/python/cpython.git
synced 2025-07-17 16:25:18 +00:00
Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
and not as a side effect of setlocale. Expose it as sys.getfilesystemencoding. Adjust test case.
This commit is contained in:
parent
620c0837bd
commit
73d538b9c6
6 changed files with 67 additions and 22 deletions
|
@ -211,6 +211,22 @@ It is always available.
|
||||||
\versionadded{2.2}
|
\versionadded{2.2}
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
|
\begin{funcdesc}{getfilesystemencoding}{}
|
||||||
|
Return the name of the encoding used to convert Unicode filenames
|
||||||
|
into system file names, or \code{None} if the system default encoding
|
||||||
|
is used. The result value depends on the operating system:
|
||||||
|
\begin{itemize}
|
||||||
|
\item On Windows 9x, the encoding is ``mbcs''.
|
||||||
|
\item On Mac OS X, the encoding is ``utf-8''.
|
||||||
|
\item On Unix, the encoding is the user's preference
|
||||||
|
according to the result of nl_langinfo(CODESET), or None if
|
||||||
|
the nl_langinfo(CODESET) failed.
|
||||||
|
\item On Windows NT+, file names are Unicode natively, so no conversion
|
||||||
|
is performed.
|
||||||
|
\end{itemize}
|
||||||
|
\versionadded{2.3}
|
||||||
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{getrefcount}{object}
|
\begin{funcdesc}{getrefcount}{object}
|
||||||
Return the reference count of the \var{object}. The count returned
|
Return the reference count of the \var{object}. The count returned
|
||||||
is generally one higher than you might expect, because it includes
|
is generally one higher than you might expect, because it includes
|
||||||
|
|
|
@ -109,8 +109,7 @@ elif os.name != 'riscos':
|
||||||
TESTFN_UNICODE="@test-\xe0\xf2"
|
TESTFN_UNICODE="@test-\xe0\xf2"
|
||||||
else:
|
else:
|
||||||
TESTFN_UNICODE=unicode("@test-\xe0\xf2", "latin-1") # 2 latin characters.
|
TESTFN_UNICODE=unicode("@test-\xe0\xf2", "latin-1") # 2 latin characters.
|
||||||
if os.name=="nt":
|
TESTFN_ENCODING=sys.getfilesystemencoding()
|
||||||
TESTFN_ENCODING="mbcs"
|
|
||||||
else:
|
else:
|
||||||
TESTFN = 'test'
|
TESTFN = 'test'
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ What's New in Python 2.3 beta 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- sys.getfilesystemencoding() was added to expose
|
||||||
|
Py_FileSystemDefaultEncoding.
|
||||||
|
|
||||||
- New function sys.exc_clear() clears the current exception. This is
|
- New function sys.exc_clear() clears the current exception. This is
|
||||||
rarely needed, but can sometimes be useful to release objects
|
rarely needed, but can sometimes be useful to release objects
|
||||||
|
|
|
@ -161,10 +161,6 @@ fixup_ulcase(void)
|
||||||
Py_DECREF(ulo);
|
Py_DECREF(ulo);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
||||||
static int fileencoding_uses_locale = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
PyLocale_setlocale(PyObject* self, PyObject* args)
|
PyLocale_setlocale(PyObject* self, PyObject* args)
|
||||||
{
|
{
|
||||||
|
@ -213,22 +209,6 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
|
||||||
fixup_ulcase();
|
fixup_ulcase();
|
||||||
/* things that got wrong up to here are ignored */
|
/* things that got wrong up to here are ignored */
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
||||||
if (Py_FileSystemDefaultEncoding == NULL)
|
|
||||||
fileencoding_uses_locale = 1;
|
|
||||||
if (fileencoding_uses_locale) {
|
|
||||||
char *codeset = nl_langinfo(CODESET);
|
|
||||||
PyObject *enc = NULL;
|
|
||||||
if (*codeset && (enc = PyCodec_Encoder(codeset))) {
|
|
||||||
/* Release previous file encoding */
|
|
||||||
if (Py_FileSystemDefaultEncoding)
|
|
||||||
free((char *)Py_FileSystemDefaultEncoding);
|
|
||||||
Py_FileSystemDefaultEncoding = strdup(codeset);
|
|
||||||
Py_DECREF(enc);
|
|
||||||
} else
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} else {
|
} else {
|
||||||
/* get locale */
|
/* get locale */
|
||||||
/* restore LC_NUMERIC first, if appropriate */
|
/* restore LC_NUMERIC first, if appropriate */
|
||||||
|
|
|
@ -17,6 +17,11 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_LANGINFO_H
|
||||||
|
#include <locale.h>
|
||||||
|
#include <langinfo.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
#undef BYTE
|
#undef BYTE
|
||||||
#include "windows.h"
|
#include "windows.h"
|
||||||
|
@ -181,6 +186,29 @@ Py_Initialize(void)
|
||||||
initsite(); /* Module site */
|
initsite(); /* Module site */
|
||||||
|
|
||||||
PyModule_WarningsModule = PyImport_ImportModule("warnings");
|
PyModule_WarningsModule = PyImport_ImportModule("warnings");
|
||||||
|
|
||||||
|
#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
|
||||||
|
/* On Unix, set the file system encoding according to the
|
||||||
|
user's preference, if the CODESET names a well-known
|
||||||
|
Python codec, and Py_FileSystemDefaultEncoding isn't
|
||||||
|
initialized by other means. */
|
||||||
|
if (!Py_FileSystemDefaultEncoding) {
|
||||||
|
char *saved_locale = setlocale(LC_CTYPE, NULL);
|
||||||
|
char *codeset;
|
||||||
|
setlocale(LC_CTYPE, "");
|
||||||
|
codeset = nl_langinfo(CODESET);
|
||||||
|
PyObject *enc = NULL;
|
||||||
|
if (*codeset) {
|
||||||
|
enc = PyCodec_Encoder(codeset);
|
||||||
|
if (enc) {
|
||||||
|
Py_FileSystemDefaultEncoding = strdup(codeset);
|
||||||
|
Py_DECREF(enc);
|
||||||
|
} else
|
||||||
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
setlocale(LC_CTYPE, saved_locale);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef COUNT_ALLOCS
|
#ifdef COUNT_ALLOCS
|
||||||
|
|
|
@ -236,6 +236,22 @@ PyDoc_STRVAR(setdefaultencoding_doc,
|
||||||
Set the current default string encoding used by the Unicode implementation."
|
Set the current default string encoding used by the Unicode implementation."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
sys_getfilesystemencoding(PyObject *self)
|
||||||
|
{
|
||||||
|
if (Py_FileSystemDefaultEncoding)
|
||||||
|
return PyString_FromString(Py_FileSystemDefaultEncoding);
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(getfilesystemencoding_doc,
|
||||||
|
"getfilesystemencoding() -> string\n\
|
||||||
|
\n\
|
||||||
|
Return the encoding used to convert Unicode filenames in\n\
|
||||||
|
operating system filenames."
|
||||||
|
);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -649,6 +665,10 @@ static PyMethodDef sys_methods[] = {
|
||||||
#ifdef DYNAMIC_EXECUTION_PROFILE
|
#ifdef DYNAMIC_EXECUTION_PROFILE
|
||||||
{"getdxp", _Py_GetDXProfile, METH_VARARGS},
|
{"getdxp", _Py_GetDXProfile, METH_VARARGS},
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef Py_USING_UNICODE
|
||||||
|
{"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
|
||||||
|
METH_NOARGS, getfilesystemencoding_doc},
|
||||||
|
#endif
|
||||||
#ifdef Py_TRACE_REFS
|
#ifdef Py_TRACE_REFS
|
||||||
{"getobjects", _Py_GetObjects, METH_VARARGS},
|
{"getobjects", _Py_GetObjects, METH_VARARGS},
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue