diff options
author | Kristján Valur Jónsson <kristjan@ccpgames.com> | 2007-04-26 09:15:08 (GMT) |
---|---|---|
committer | Kristján Valur Jónsson <kristjan@ccpgames.com> | 2007-04-26 09:15:08 (GMT) |
commit | 0a440d41844aac25178400d33d714d8e479b86d4 (patch) | |
tree | 85932752a2f3ec1070e5cf231717a50fb603630a | |
parent | 452f5df64a09e6e50a2d861fa1d18e0bbea7ca4a (diff) | |
download | cpython-0a440d41844aac25178400d33d714d8e479b86d4.zip cpython-0a440d41844aac25178400d33d714d8e479b86d4.tar.gz cpython-0a440d41844aac25178400d33d714d8e479b86d4.tar.bz2 |
Export function sanitize_the_mode from fileobject.c as _PyFile_SanitizeMode(). Use this function in posixmodule.c when implementing fdopen(). This fixes test_subprocess.py for a VisualStudio 2005 compile.
-rw-r--r-- | Include/fileobject.h | 5 | ||||
-rw-r--r-- | Modules/posixmodule.c | 20 | ||||
-rw-r--r-- | Objects/fileobject.c | 15 |
3 files changed, 26 insertions, 14 deletions
diff --git a/Include/fileobject.h b/Include/fileobject.h index ebbb521..9fd0646 100644 --- a/Include/fileobject.h +++ b/Include/fileobject.h @@ -57,6 +57,11 @@ PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding; char *Py_UniversalNewlineFgets(char *, int, 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 } #endif diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index bb1dc4f..29ef1dc 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6259,16 +6259,23 @@ static PyObject * posix_fdopen(PyObject *self, PyObject *args) { int fd; - char *mode = "r"; + char *orgmode = "r"; int bufsize = -1; FILE *fp; PyObject *f; - if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) + char *mode; + if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize)) return NULL; - if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { - PyErr_Format(PyExc_ValueError, - "invalid file mode '%s'", mode); + /* Sanitize mode. See fileobject.c */ + mode = PyMem_MALLOC(strlen(orgmode)+3); + if (!mode) { + PyErr_NoMemory(); + return NULL; + } + strcpy(mode, orgmode); + if (_PyFile_SanitizeMode(mode)) { + PyMem_FREE(mode); return NULL; } Py_BEGIN_ALLOW_THREADS @@ -6289,10 +6296,11 @@ posix_fdopen(PyObject *self, PyObject *args) #else fp = fdopen(fd, mode); #endif + PyMem_FREE(mode); Py_END_ALLOW_THREADS if (fp == NULL) return posix_error(); - f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); + f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose); if (f != NULL) PyFile_SetBufSize(f, bufsize); return f; diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 1880187..ac093ce 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -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 universal newline support is expressly forbidden by PEP 278. Additionally, remove the 'U' from the mode string as platforms - won't know what it is. */ -/* zero return is kewl - one is un-kewl */ -static int -sanitize_the_mode(char *mode) + won't know what it is. Non-zero return signals an exception */ +int +_PyFile_SanitizeMode(char *mode) { char *upos; size_t len = strlen(mode); if (!len) { PyErr_SetString(PyExc_ValueError, "empty mode string"); - return 1; + return -1; } upos = strchr(mode, 'U'); @@ -160,7 +159,7 @@ sanitize_the_mode(char *mode) PyErr_Format(PyExc_ValueError, "universal newline " "mode can only be used with modes " "starting with 'r'"); - return 1; + return -1; } if (mode[0] != 'r') { @@ -175,7 +174,7 @@ sanitize_the_mode(char *mode) } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { PyErr_Format(PyExc_ValueError, "mode string must begin with " "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode); - return 1; + return -1; } return 0; @@ -204,7 +203,7 @@ open_the_file(PyFileObject *f, char *name, char *mode) } strcpy(newmode, mode); - if (sanitize_the_mode(newmode)) { + if (_PyFile_SanitizeMode(newmode)) { f = NULL; goto cleanup; } |