diff options
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r-- | Objects/fileobject.c | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index a4a43ed..fb8a542 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -4,9 +4,9 @@ #include "Python.h" #include "structmember.h" -#ifndef DONT_HAVE_SYS_TYPES_H +#ifdef HAVE_SYS_TYPES_H #include <sys/types.h> -#endif /* DONT_HAVE_SYS_TYPES_H */ +#endif /* HAVE_SYS_TYPES_H */ #ifdef MS_WINDOWS #define fileno _fileno @@ -103,6 +103,7 @@ static PyObject * fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode, int (*close)(FILE *)) { + assert(name != NULL); assert(f != NULL); assert(PyFile_Check(f)); assert(f->f_fp == NULL); @@ -111,7 +112,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode, Py_DECREF(f->f_mode); Py_DECREF(f->f_encoding); - Py_INCREF (name); + Py_INCREF(name); f->f_name = name; f->f_mode = PyString_FromString(mode); @@ -126,7 +127,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode, Py_INCREF(Py_None); f->f_encoding = Py_None; - if (f->f_name == NULL || f->f_mode == NULL) + if (f->f_mode == NULL) return NULL; f->f_fp = fp; f = dircheck(f); @@ -241,13 +242,15 @@ open_the_file(PyFileObject *f, char *name, char *mode) } if (f->f_fp == NULL) { -#ifdef _MSC_VER +#if defined _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__)) /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings, * across all Windows flavors. When it sets EINVAL varies * across Windows flavors, the exact conditions aren't * documented, and the answer lies in the OS's implementation * of Win32's CreateFile function (whose source is secret). * Seems the best we can do is map EINVAL to ENOENT. + * Starting with Visual Studio .NET 2005, EINVAL is correctly + * set by our CRT error handler (set in exceptions.c.) */ if (errno == 0) /* bad mode string */ errno = EINVAL; @@ -276,7 +279,9 @@ PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, NULL, NULL); if (f != NULL) { - PyObject *o_name = PyString_FromString(name); + PyObject *o_name = PyString_FromString(name); + if (o_name == NULL) + return NULL; if (fill_file_fields(f, fp, o_name, mode, close) == NULL) { Py_DECREF(f); f = NULL; @@ -409,11 +414,11 @@ file_repr(PyFileObject *f) if (PyUnicode_Check(f->f_name)) { #ifdef Py_USING_UNICODE PyObject *ret = NULL; - PyObject *name; - name = PyUnicode_AsUnicodeEscapeString(f->f_name); + PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name); + const char *name_str = name ? PyString_AsString(name) : "?"; ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>", f->f_fp == NULL ? "closed" : "open", - PyString_AsString(name), + name_str, PyString_AsString(f->f_mode), f); Py_XDECREF(name); |