[Python-checkins] python/dist/src/Python pythonrun.c,2.179,2.180 sysmodule.c,2.115,2.116

loewis@users.sourceforge.net loewis@users.sourceforge.net
Wed, 05 Mar 2003 07:14:21 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv18533/Python

Modified Files:
	pythonrun.c sysmodule.c 
Log Message:
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.


Index: pythonrun.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pythonrun.c,v
retrieving revision 2.179
retrieving revision 2.180
diff -C2 -d -r2.179 -r2.180
*** pythonrun.c	25 Feb 2003 20:25:12 -0000	2.179
--- pythonrun.c	5 Mar 2003 15:13:46 -0000	2.180
***************
*** 18,21 ****
--- 18,26 ----
  #endif
  
+ #ifdef HAVE_LANGINFO_H
+ #include <locale.h>
+ #include <langinfo.h>
+ #endif
+ 
  #ifdef MS_WINDOWS
  #undef BYTE
***************
*** 182,185 ****
--- 187,213 ----
  
  	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
  }
  

Index: sysmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/sysmodule.c,v
retrieving revision 2.115
retrieving revision 2.116
diff -C2 -d -r2.115 -r2.116
*** sysmodule.c	1 Mar 2003 03:20:41 -0000	2.115
--- sysmodule.c	5 Mar 2003 15:13:47 -0000	2.116
***************
*** 237,240 ****
--- 237,256 ----
  );
  
+ 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
  
***************
*** 649,652 ****
--- 665,672 ----
  #ifdef DYNAMIC_EXECUTION_PROFILE
  	{"getdxp",	_Py_GetDXProfile, METH_VARARGS},
+ #endif
+ #ifdef Py_USING_UNICODE
+ 	{"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
+ 	 METH_NOARGS, getfilesystemencoding_doc}, 
  #endif
  #ifdef Py_TRACE_REFS