[Python-checkins] python/dist/src/Objects fileobject.c,2.168,2.169

mhammond@users.sourceforge.net mhammond@users.sourceforge.net
Wed, 02 Oct 2002 22:10:41 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv29768/Objects

Modified Files:
	fileobject.c 
Log Message:
Patch 594001: PEP 277 - Unicode file name support for Windows NT.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.168
retrieving revision 2.169
diff -C2 -d -r2.168 -r2.169
*** fileobject.c	14 Aug 2002 21:01:41 -0000	2.168
--- fileobject.c	3 Oct 2002 05:10:39 -0000	2.169
***************
*** 16,19 ****
--- 16,25 ----
  #endif
  
+ #ifdef _MSC_VER
+ /* Need GetVersion to see if on NT so safe to use _wfopen */
+ #define WIN32_LEAN_AND_MEAN
+ #include <windows.h>
+ #endif /* _MSC_VER */
+ 
  #ifdef macintosh
  #ifdef USE_GUSI
***************
*** 103,107 ****
  static PyObject *
  fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
! 		 int (*close)(FILE *))
  {
  	assert(f != NULL);
--- 109,113 ----
  static PyObject *
  fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
! 		 int (*close)(FILE *), PyObject *wname)
  {
  	assert(f != NULL);
***************
*** 111,115 ****
  	Py_DECREF(f->f_name);
  	Py_DECREF(f->f_mode);
! 	f->f_name = PyString_FromString(name);
  	f->f_mode = PyString_FromString(mode);
  
--- 117,124 ----
  	Py_DECREF(f->f_name);
  	Py_DECREF(f->f_mode);
! 	if (wname)
! 		f->f_name = PyUnicode_FromObject(wname);
! 	else
! 		f->f_name = PyString_FromString(name);
  	f->f_mode = PyString_FromString(mode);
  
***************
*** 136,140 ****
--- 145,154 ----
  	assert(f != NULL);
  	assert(PyFile_Check(f));
+ #ifdef MS_WINDOWS
+ 	/* windows ignores the passed name in order to support Unicode */
+ 	assert(f->f_name != NULL);
+ #else
  	assert(name != NULL);
+ #endif
  	assert(mode != NULL);
  	assert(f->f_fp == NULL);
***************
*** 157,161 ****
  #endif
  	{
- 		Py_BEGIN_ALLOW_THREADS
  #ifdef WITH_UNIVERSAL_NEWLINES
  		if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
--- 171,174 ----
***************
*** 169,174 ****
  			mode = "r";
  #endif
! 		f->f_fp = fopen(name, mode);
! 		Py_END_ALLOW_THREADS
  	}
  	if (f->f_fp == NULL) {
--- 182,205 ----
  			mode = "r";
  #endif
! #ifdef MS_WINDOWS
! 		if (PyUnicode_Check(f->f_name)) {
! 			PyObject *wmode; 
! 			wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL); 
! 			if (f->f_name && wmode) {
! 				Py_BEGIN_ALLOW_THREADS
! 				/* PyUnicode_AS_UNICODE OK without thread
! 				   lock as it is a simple dereference. */
! 				f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
! 						  PyUnicode_AS_UNICODE(wmode));
! 				Py_END_ALLOW_THREADS
! 			}
! 			Py_XDECREF(wmode);
! 		}
! #endif
! 		if (NULL == f->f_fp && NULL != name) {
! 			Py_BEGIN_ALLOW_THREADS
! 			f->f_fp = fopen(name, mode);
! 			Py_END_ALLOW_THREADS
! 		}
  	}
  	if (f->f_fp == NULL) {
***************
*** 202,206 ****
--- 233,241 ----
  				     mode);
  		else
+ #ifdef MS_WINDOWS
+ 			PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
+ #else
  			PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
+ #endif /* MS_WINDOWS */
  		f = NULL;
  	}
***************
*** 216,220 ****
  							     NULL, NULL);
  	if (f != NULL) {
! 		if (fill_file_fields(f, fp, name, mode, close) == NULL) {
  			Py_DECREF(f);
  			f = NULL;
--- 251,255 ----
  							     NULL, NULL);
  	if (f != NULL) {
! 		if (fill_file_fields(f, fp, name, mode, close, NULL) == NULL) {
  			Py_DECREF(f);
  			f = NULL;
***************
*** 294,302 ****
  file_repr(PyFileObject *f)
  {
! 	return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
  				   f->f_fp == NULL ? "closed" : "open",
  				   PyString_AsString(f->f_name),
  				   PyString_AsString(f->f_mode),
  				   f);
  }
  
--- 329,350 ----
  file_repr(PyFileObject *f)
  {
! 	if (PyUnicode_Check(f->f_name)) {
! 		PyObject *ret = NULL;
! 		PyObject *name;
! 		name = PyUnicode_AsUnicodeEscapeString(f->f_name);
! 		ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
! 				   f->f_fp == NULL ? "closed" : "open",
! 				   PyString_AsString(name),
! 				   PyString_AsString(f->f_mode),
! 				   f);
! 		Py_XDECREF(name);
! 		return ret;
! 	} else {
! 		return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
  				   f->f_fp == NULL ? "closed" : "open",
  				   PyString_AsString(f->f_name),
  				   PyString_AsString(f->f_mode),
  				   f);
+ 	}
  }
  
***************
*** 1767,1770 ****
--- 1815,1819 ----
  	char *mode = "r";
  	int bufsize = -1;
+ 	int wideargument = 0;
  
  	assert(PyFile_Check(self));
***************
*** 1777,1786 ****
  	}
  
! 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
! 					 Py_FileSystemDefaultEncoding, &name,
! 					 &mode, &bufsize))
! 		return -1;
! 	if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL)
! 		goto Error;
  	if (open_the_file(foself, name, mode) == NULL)
  		goto Error;
--- 1826,1856 ----
  	}
  
! #ifdef Py_WIN_WIDE_FILENAMES
! 	if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
! 		PyObject *po;
! 		if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
! 						kwlist, &po, &mode, &bufsize)) {
! 			wideargument = 1;
! 			if (fill_file_fields(foself, NULL, name, mode,
! 					     fclose, po) == NULL)
! 				goto Error;
! 		} else {
! 			/* Drop the argument parsing error as narrow
! 			   strings are also valid. */
! 			PyErr_Clear();
! 		}
! 	}
! #endif
! 
! 	if (!wideargument) {
! 		if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
! 						 Py_FileSystemDefaultEncoding,
! 						 &name,
! 						 &mode, &bufsize))
! 			return -1;
! 		if (fill_file_fields(foself, NULL, name, mode,
! 				     fclose, NULL) == NULL)
! 			goto Error;
! 	}
  	if (open_the_file(foself, name, mode) == NULL)
  		goto Error;