Merge change 54982 from the trunk. This fixes the test_subprocess test in the testsuite for VisualStudio2005 builds, by "sanitizing" the "mode" that is used in the posixmodule's fdopen(). In particular the non-standard "U" mode character is removed.

This commit is contained in:
Kristján Valur Jónsson 2007-05-07 19:25:38 +00:00
parent dffe9a214b
commit a1392d5ace
3 changed files with 26 additions and 14 deletions

View File

@ -57,6 +57,11 @@ PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *); char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *); size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *);
/* A routine to do sanity checking on the file mode string. returns
non-zero on if an exception occurred
*/
int _PyFile_SanitizeMode(char *mode);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -6170,16 +6170,23 @@ static PyObject *
posix_fdopen(PyObject *self, PyObject *args) posix_fdopen(PyObject *self, PyObject *args)
{ {
int fd; int fd;
char *mode = "r"; char *orgmode = "r";
int bufsize = -1; int bufsize = -1;
FILE *fp; FILE *fp;
PyObject *f; PyObject *f;
if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) char *mode;
if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
return NULL; return NULL;
if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { /* Sanitize mode. See fileobject.c */
PyErr_Format(PyExc_ValueError, mode = PyMem_MALLOC(strlen(orgmode)+3);
"invalid file mode '%s'", mode); if (!mode) {
PyErr_NoMemory();
return NULL;
}
strcpy(mode, orgmode);
if (_PyFile_SanitizeMode(mode)) {
PyMem_FREE(mode);
return NULL; return NULL;
} }
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
@ -6200,10 +6207,11 @@ posix_fdopen(PyObject *self, PyObject *args)
#else #else
fp = fdopen(fd, mode); fp = fdopen(fd, mode);
#endif #endif
PyMem_FREE(mode);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (fp == NULL) if (fp == NULL)
return posix_error(); return posix_error();
f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
if (f != NULL) if (f != NULL)
PyFile_SetBufSize(f, bufsize); PyFile_SetBufSize(f, bufsize);
return f; return f;

View File

@ -139,17 +139,16 @@ fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
ignore stuff they don't understand... write or append mode with ignore stuff they don't understand... write or append mode with
universal newline support is expressly forbidden by PEP 278. universal newline support is expressly forbidden by PEP 278.
Additionally, remove the 'U' from the mode string as platforms Additionally, remove the 'U' from the mode string as platforms
won't know what it is. */ won't know what it is. Non-zero return signals an exception */
/* zero return is kewl - one is un-kewl */ int
static int _PyFile_SanitizeMode(char *mode)
sanitize_the_mode(char *mode)
{ {
char *upos; char *upos;
size_t len = strlen(mode); size_t len = strlen(mode);
if (!len) { if (!len) {
PyErr_SetString(PyExc_ValueError, "empty mode string"); PyErr_SetString(PyExc_ValueError, "empty mode string");
return 1; return -1;
} }
upos = strchr(mode, 'U'); upos = strchr(mode, 'U');
@ -160,7 +159,7 @@ sanitize_the_mode(char *mode)
PyErr_Format(PyExc_ValueError, "universal newline " PyErr_Format(PyExc_ValueError, "universal newline "
"mode can only be used with modes " "mode can only be used with modes "
"starting with 'r'"); "starting with 'r'");
return 1; return -1;
} }
if (mode[0] != 'r') { if (mode[0] != 'r') {
@ -175,7 +174,7 @@ sanitize_the_mode(char *mode)
} else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
PyErr_Format(PyExc_ValueError, "mode string must begin with " PyErr_Format(PyExc_ValueError, "mode string must begin with "
"one of 'r', 'w', 'a' or 'U', not '%.200s'", mode); "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode);
return 1; return -1;
} }
return 0; return 0;
@ -204,7 +203,7 @@ open_the_file(PyFileObject *f, char *name, char *mode)
} }
strcpy(newmode, mode); strcpy(newmode, mode);
if (sanitize_the_mode(newmode)) { if (_PyFile_SanitizeMode(newmode)) {
f = NULL; f = NULL;
goto cleanup; goto cleanup;
} }