diff -r 4d89d03690ef Modules/posixmodule.c --- a/Modules/posixmodule.c Sun Aug 31 15:48:55 2014 +0200 +++ b/Modules/posixmodule.c Tue Sep 02 00:52:30 2014 +0200 @@ -784,6 +784,7 @@ typedef struct { static void path_cleanup(path_t *path) { + PyMem_Free(path->wide); if (path->cleanup) { Py_CLEAR(path->cleanup); } @@ -809,6 +810,7 @@ path_converter(PyObject *o, void *p) { } /* ensure it's always safe to call path_cleanup() */ + path->wide = NULL; path->cleanup = NULL; if (o == Py_None) { @@ -830,13 +832,14 @@ path_converter(PyObject *o, void *p) { #ifdef MS_WINDOWS wchar_t *wide; - wide = PyUnicode_AsUnicodeAndSize(unicode, &length); + wide = PyUnicode_AsWideCharString(unicode, &length); if (!wide) { Py_DECREF(unicode); return 0; } if (length > 32767) { FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); + PyMem_Free(wide); Py_DECREF(unicode); return 0; } @@ -4754,7 +4757,7 @@ posix__getfullpathname(PyObject *self, P DWORD result; PyObject *v; - wpath = PyUnicode_AsUnicode(po); + wpath = PyUnicode_AsWideCharString(po, NULL); if (wpath == NULL) return NULL; result = GetFullPathNameW(wpath, @@ -4762,10 +4765,13 @@ posix__getfullpathname(PyObject *self, P woutbuf, &wtemp); if (result > Py_ARRAY_LENGTH(woutbuf)) { woutbufp = PyMem_Malloc(result * sizeof(wchar_t)); - if (!woutbufp) + if (!woutbufp) { + PyMem_Free(wpath); return PyErr_NoMemory(); + } result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); } + PyMem_Free(wpath); if (result) v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); else @@ -4844,10 +4850,6 @@ os__getfinalpathname_impl(PyModuleDef *m PyObject *result; wchar_t *path_wchar; - path_wchar = PyUnicode_AsUnicode(path); - if (path_wchar == NULL) - return NULL; - if(!check_GetFinalPathNameByHandle()) { /* If the OS doesn't have GetFinalPathNameByHandle, return a NotImplementedError. */ @@ -4855,6 +4857,10 @@ os__getfinalpathname_impl(PyModuleDef *m "GetFinalPathNameByHandle not available on this platform"); } + path_wchar = PyUnicode_AsWideCharString(path, NULL); + if (path_wchar == NULL) + return NULL; + hFile = CreateFileW( path_wchar, 0, /* desired access */ @@ -4865,6 +4871,8 @@ os__getfinalpathname_impl(PyModuleDef *m FILE_FLAG_BACKUP_SEMANTICS, NULL); + PyMem_Free(path_wchar); + if(hFile == INVALID_HANDLE_VALUE) return win32_error_object("CreateFileW", path); @@ -4905,11 +4913,12 @@ posix__isdir(PyObject *self, PyObject *a DWORD attributes; if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { - wchar_t *wpath = PyUnicode_AsUnicode(po); + wchar_t *wpath = PyUnicode_AsWideCharString(po, NULL); if (wpath == NULL) return NULL; attributes = GetFileAttributesW(wpath); + PyMem_Free(wpath); if (attributes == INVALID_FILE_ATTRIBUTES) Py_RETURN_FALSE; goto check; @@ -4980,7 +4989,7 @@ os__getvolumepathname_impl(PyModuleDef * size_t buflen; BOOL ret; - path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); + path_wchar = PyUnicode_AsWideCharString(path, &buflen); if (path_wchar == NULL) return NULL; buflen += 1; @@ -4990,12 +4999,14 @@ os__getvolumepathname_impl(PyModuleDef * if (buflen > DWORD_MAX) { PyErr_SetString(PyExc_OverflowError, "path too long"); - return NULL; + goto exit; } mountpath = (wchar_t *)PyMem_Malloc(buflen * sizeof(wchar_t)); - if (mountpath == NULL) - return PyErr_NoMemory(); + if (mountpath == NULL) { + PyErr_NoMemory(); + goto exit; + } Py_BEGIN_ALLOW_THREADS ret = GetVolumePathNameW(path_wchar, mountpath, @@ -5010,6 +5021,7 @@ os__getvolumepathname_impl(PyModuleDef * exit: PyMem_Free(mountpath); + PyMem_Free(path_wchar); return result; } @@ -10002,7 +10014,7 @@ win_readlink(PyObject *self, PyObject *a )) return NULL; - path = PyUnicode_AsUnicode(po); + path = PyUnicode_AsWideCharString(po, NULL); if (path == NULL) return NULL; @@ -10018,6 +10030,8 @@ win_readlink(PyObject *self, PyObject *a 0); Py_END_ALLOW_THREADS + PyMem_Free(path); + if (reparse_point_handle==INVALID_HANDLE_VALUE) return win32_error_object("readlink", po); @@ -12930,13 +12944,15 @@ os_putenv_impl(PyModuleDef *module, PyOb goto error; } - env = PyUnicode_AsUnicode(unicode); + env = PyUnicode_AsWideCharString(unicode, NULL); if (env == NULL) goto error; if (_wputenv(env)) { + PyMem_Free(env); posix_error(); goto error; } + PyMem_Free(env); posix_putenv_garbage_setitem(name, unicode); Py_RETURN_NONE; @@ -15108,13 +15124,17 @@ win32_startfile(PyObject *self, PyObject } } - wpath = PyUnicode_AsUnicode(unipath); - if (wpath == NULL) + wpath = PyUnicode_AsWideCharString(unipath, NULL); + if (wpath == NULL) { + PyErr_Clear(); goto normal; + } if (uoperation) { - woperation = PyUnicode_AsUnicode(uoperation); - if (woperation == NULL) + woperation = PyUnicode_AsWideCharString(uoperation, NULL); + if (woperation == NULL) { + PyMem_Free(wpath); goto normal; + } } else woperation = NULL; @@ -15124,6 +15144,8 @@ win32_startfile(PyObject *self, PyObject NULL, NULL, SW_SHOWNORMAL); Py_END_ALLOW_THREADS + PyMem_Free(wpath); + Py_XDECREF(uoperation); if (rc <= (HINSTANCE)32) { win32_error_object("startfile", unipath);