mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #9979: Use PyUnicode_AsWideCharString() in import.c
Don't truncate path if it is too long anymore, and allocate fewer memory (but allocate it on the heap, not on the stack).
This commit is contained in:
parent
137c34c027
commit
255dfdb5ce
1 changed files with 16 additions and 18 deletions
|
@ -1961,21 +1961,21 @@ FILE*
|
||||||
_Py_fopen(PyObject *unicode, const char *mode)
|
_Py_fopen(PyObject *unicode, const char *mode)
|
||||||
{
|
{
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
wchar_t path[MAXPATHLEN+1];
|
wchar_t *path;
|
||||||
wchar_t wmode[10];
|
wchar_t wmode[10];
|
||||||
Py_ssize_t len;
|
|
||||||
int usize;
|
int usize;
|
||||||
|
FILE *f;
|
||||||
len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, MAXPATHLEN);
|
|
||||||
if (len == -1)
|
|
||||||
return NULL;
|
|
||||||
path[len] = L'\0';
|
|
||||||
|
|
||||||
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
|
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
|
||||||
if (usize == 0)
|
if (usize == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return _wfopen(path, wmode);
|
path = PyUnicode_AsWideCharString((PyUnicodeObject*)unicode, NULL);
|
||||||
|
if (path == NULL)
|
||||||
|
return NULL;
|
||||||
|
f = _wfopen(path, wmode);
|
||||||
|
PyMem_Free(path);
|
||||||
|
return f;
|
||||||
#else
|
#else
|
||||||
FILE *f;
|
FILE *f;
|
||||||
PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
|
PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
|
||||||
|
@ -1997,17 +1997,15 @@ int
|
||||||
_Py_stat(PyObject *unicode, struct stat *statbuf)
|
_Py_stat(PyObject *unicode, struct stat *statbuf)
|
||||||
{
|
{
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
wchar_t path[MAXPATHLEN+1];
|
wchar_t *path;
|
||||||
Py_ssize_t len;
|
|
||||||
int err;
|
int err;
|
||||||
struct _stat wstatbuf;
|
struct _stat wstatbuf;
|
||||||
|
|
||||||
len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, MAXPATHLEN);
|
path = PyUnicode_AsWideCharString((PyUnicodeObject*)unicode, NULL);
|
||||||
if (len == -1)
|
if (path == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
path[len] = L'\0';
|
|
||||||
|
|
||||||
err = _wstat(path, &wstatbuf);
|
err = _wstat(path, &wstatbuf);
|
||||||
|
PyMem_Free(path);
|
||||||
if (!err)
|
if (!err)
|
||||||
statbuf->st_mode = wstatbuf.st_mode;
|
statbuf->st_mode = wstatbuf.st_mode;
|
||||||
return err;
|
return err;
|
||||||
|
@ -3724,7 +3722,7 @@ NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
|
||||||
#else /* MS_WINDOWS */
|
#else /* MS_WINDOWS */
|
||||||
PyObject *pathobj;
|
PyObject *pathobj;
|
||||||
DWORD rv;
|
DWORD rv;
|
||||||
wchar_t path[MAXPATHLEN+1];
|
wchar_t *path;
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
|
|
||||||
if (!_PyArg_NoKeywords("NullImporter()", kwds))
|
if (!_PyArg_NoKeywords("NullImporter()", kwds))
|
||||||
|
@ -3739,15 +3737,15 @@ NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = PyUnicode_AsWideChar((PyUnicodeObject*)pathobj,
|
path = PyUnicode_AsWideCharString((PyUnicodeObject*)pathobj, NULL);
|
||||||
path, sizeof(path) / sizeof(path[0]));
|
if (path == NULL)
|
||||||
if (len == -1)
|
|
||||||
return -1;
|
return -1;
|
||||||
/* see issue1293 and issue3677:
|
/* see issue1293 and issue3677:
|
||||||
* stat() on Windows doesn't recognise paths like
|
* stat() on Windows doesn't recognise paths like
|
||||||
* "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
|
* "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
|
||||||
*/
|
*/
|
||||||
rv = GetFileAttributesW(path);
|
rv = GetFileAttributesW(path);
|
||||||
|
PyMem_Free(path);
|
||||||
if (rv != INVALID_FILE_ATTRIBUTES) {
|
if (rv != INVALID_FILE_ATTRIBUTES) {
|
||||||
/* it exists */
|
/* it exists */
|
||||||
if (rv & FILE_ATTRIBUTE_DIRECTORY) {
|
if (rv & FILE_ATTRIBUTE_DIRECTORY) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue