From 2967d4ef158cca9a434324e6341405857df0123d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 17 Nov 2011 00:51:38 +0100 Subject: Catch PyUnicode_AS_UNICODE() errors in fileutils.c --- Python/fileutils.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index c563eaa5fb..0afa415d59 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -244,8 +244,12 @@ _Py_stat(PyObject *path, struct stat *statbuf) #ifdef MS_WINDOWS int err; struct _stat wstatbuf; + wchar_t *wpath; - err = _wstat(PyUnicode_AS_UNICODE(path), &wstatbuf); + wpath = PyUnicode_AsUnicode(path); + if (wpath == NULL) + return -1; + err = _wstat(wpath, &wstatbuf); if (!err) statbuf->st_mode = wstatbuf.st_mode; return err; @@ -297,14 +301,19 @@ FILE* _Py_fopen(PyObject *path, const char *mode) { #ifdef MS_WINDOWS + wchar_t *wpath; wchar_t wmode[10]; int usize; + wpath = PyUnicode_AsUnicode(path); + if (wpath == NULL) + return NULL; + usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); if (usize == 0) return NULL; - return _wfopen(PyUnicode_AS_UNICODE(path), wmode); + return _wfopen(wpath, wmode); #else FILE *f; PyObject *bytes = PyUnicode_EncodeFSDefault(path); -- cgit v1.2.1 From ab895ebc75ae51642db6392c58a0f7addb2c96cf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 16 Dec 2011 23:56:01 +0100 Subject: Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale() * PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale() decode a string from the current locale encoding * _Py_char2wchar() writes an "error code" in the size argument to indicate if the function failed because of memory allocation failure or because of a decoding error. The function doesn't write the error message directly to stderr. * Fix time.strftime() (if wcsftime() is missing): decode strftime() result from the current locale encoding, not from the filesystem encoding. --- Python/fileutils.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 0afa415d59..0aad2200fb 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -16,7 +16,9 @@ Return a pointer to a newly allocated wide character string (use PyMem_Free() to free the memory) and write the number of written wide characters excluding the null character into *size if size is not NULL, or - NULL on error (conversion or memory allocation error). + NULL on error (decoding or memory allocation error). If size is not NULL, + *size is set to (size_t)-1 on memory error and (size_t)-2 on decoding + error. Conversion errors should never happen, unless there is a bug in the C library. */ @@ -82,8 +84,9 @@ _Py_char2wchar(const char* arg, size_t *size) since we provide everything that we have - unless there is a bug in the C library, or I misunderstood how mbrtowc works. */ - fprintf(stderr, "unexpected mbrtowc result -2\n"); PyMem_Free(res); + if (size != NULL) + *size = (size_t)-2; return NULL; } if (converted == (size_t)-1) { @@ -112,7 +115,8 @@ _Py_char2wchar(const char* arg, size_t *size) is ASCII (i.e. escape all bytes > 128. This will still roundtrip correctly in the locale's charset, which must be an ASCII superset. */ res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t)); - if (!res) goto oom; + if (!res) + goto oom; in = (unsigned char*)arg; out = res; while(*in) @@ -126,7 +130,8 @@ _Py_char2wchar(const char* arg, size_t *size) *size = out - res; return res; oom: - fprintf(stderr, "out of memory\n"); + if (size != NULL) + *size = (size_t)-1; return NULL; } @@ -137,10 +142,10 @@ oom: This function is the reverse of _Py_char2wchar(). Return a pointer to a newly allocated byte string (use PyMem_Free() to free - the memory), or NULL on conversion or memory allocation error. + the memory), or NULL on encoding or memory allocation error. If error_pos is not NULL: *error_pos is the index of the invalid character - on conversion error, or (size_t)-1 otherwise. */ + on encoding error, or (size_t)-1 otherwise. */ char* _Py_wchar2char(const wchar_t *text, size_t *error_pos) { @@ -328,7 +333,7 @@ _Py_fopen(PyObject *path, const char *mode) #ifdef HAVE_READLINK /* Read value of symbolic link. Encode the path to the locale encoding, decode - the result from the locale encoding. */ + the result from the locale encoding. Return -1 on error. */ int _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) @@ -372,7 +377,8 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) #ifdef HAVE_REALPATH /* Return the canonicalized absolute pathname. Encode path to the locale - encoding, decode the result from the locale encoding. */ + encoding, decode the result from the locale encoding. + Return NULL on error. */ wchar_t* _Py_wrealpath(const wchar_t *path, @@ -410,7 +416,8 @@ _Py_wrealpath(const wchar_t *path, #endif /* Get the current directory. size is the buffer size in wide characters - including the null character. Decode the path from the locale encoding. */ + including the null character. Decode the path from the locale encoding. + Return NULL on error. */ wchar_t* _Py_wgetcwd(wchar_t *buf, size_t size) -- cgit v1.2.1 From 219d85b51cf4eed90b0cd74706d6b55b3753c604 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 18 Dec 2011 20:47:30 +0100 Subject: import.c now catchs _Py_stat() exceptions _Py_stat() now returns -2 if an exception was raised. --- Python/fileutils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 0aad2200fb..8c049e02af 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -240,8 +240,8 @@ _Py_wstat(const wchar_t* path, struct stat *buf) /* Call _wstat() on Windows, or encode the path to the filesystem encoding and call stat() otherwise. Only fill st_mode attribute on Windows. - Return 0 on success, -1 on _wstat() / stat() error or (if PyErr_Occurred()) - unicode error. */ + Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was + raised. */ int _Py_stat(PyObject *path, struct stat *statbuf) @@ -253,7 +253,7 @@ _Py_stat(PyObject *path, struct stat *statbuf) wpath = PyUnicode_AsUnicode(path); if (wpath == NULL) - return -1; + return -2; err = _wstat(wpath, &wstatbuf); if (!err) statbuf->st_mode = wstatbuf.st_mode; @@ -262,7 +262,7 @@ _Py_stat(PyObject *path, struct stat *statbuf) int ret; PyObject *bytes = PyUnicode_EncodeFSDefault(path); if (bytes == NULL) - return -1; + return -2; ret = stat(PyBytes_AS_STRING(bytes), statbuf); Py_DECREF(bytes); return ret; -- cgit v1.2.1 From 65f4c8fb8a67c300410ad2835b7850a2bedef51c Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 19 Dec 2011 18:19:06 +0100 Subject: _Py_fopen now allows bytes filenames under non-Windows platforms. --- Python/fileutils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 8c049e02af..1e71431c00 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -321,8 +321,8 @@ _Py_fopen(PyObject *path, const char *mode) return _wfopen(wpath, wmode); #else FILE *f; - PyObject *bytes = PyUnicode_EncodeFSDefault(path); - if (bytes == NULL) + PyObject *bytes; + if (!PyUnicode_FSConverter(path, &bytes)) return NULL; f = fopen(PyBytes_AS_STRING(bytes), mode); Py_DECREF(bytes); -- cgit v1.2.1 From 5b02c29edfe71ff031b4bfa5e07063018d552f11 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 22 Dec 2011 10:03:38 +0100 Subject: Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the SSLContext.load_dh_params() method and the ssl.OP_SINGLE_DH_USE option. --- Python/fileutils.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 1e71431c00..8993c8c497 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -310,6 +310,12 @@ _Py_fopen(PyObject *path, const char *mode) wchar_t wmode[10]; int usize; + if (!PyUnicode_Check(path)) { + PyErr_Format(PyExc_TypeError, + "str file path expected under Windows, got %R", + Py_TYPE(path)); + return NULL; + } wpath = PyUnicode_AsUnicode(path); if (wpath == NULL) return NULL; -- cgit v1.2.1