Catch PyUnicode_AS_UNICODE() errors

This commit is contained in:
Victor Stinner 2011-11-17 00:45:54 +01:00
parent 6f9568bb1f
commit 1f7951711c
2 changed files with 55 additions and 25 deletions

View File

@ -3051,9 +3051,13 @@ PyObject *
PyUnicode_EncodeFSDefault(PyObject *unicode) PyUnicode_EncodeFSDefault(PyObject *unicode)
{ {
#ifdef HAVE_MBCS #ifdef HAVE_MBCS
return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), const Py_UNICODE *wstr;
PyUnicode_GET_SIZE(unicode), Py_ssize_t wlen;
NULL);
wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen);
if (wstr == NULL)
return NULL;
return PyUnicode_EncodeMBCS(wstr, wlen, NULL);
#elif defined(__APPLE__) #elif defined(__APPLE__)
return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); return _PyUnicode_AsUTF8String(unicode, "surrogateescape");
#else #else
@ -3137,10 +3141,15 @@ PyUnicode_AsEncodedString(PyObject *unicode,
(strcmp(lower, "iso-8859-1") == 0)) (strcmp(lower, "iso-8859-1") == 0))
return _PyUnicode_AsLatin1String(unicode, errors); return _PyUnicode_AsLatin1String(unicode, errors);
#ifdef HAVE_MBCS #ifdef HAVE_MBCS
else if (strcmp(lower, "mbcs") == 0) else if (strcmp(lower, "mbcs") == 0) {
return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), const Py_UNICODE *wstr;
PyUnicode_GET_SIZE(unicode), Py_ssize_t wlen;
errors);
wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen);
if (wstr == NULL)
return NULL;
return PyUnicode_EncodeMBCS(wstr, wlen, errors);
}
#endif #endif
else if (strcmp(lower, "ascii") == 0) else if (strcmp(lower, "ascii") == 0)
return _PyUnicode_AsASCIIString(unicode, errors); return _PyUnicode_AsASCIIString(unicode, errors);
@ -5148,10 +5157,12 @@ PyUnicode_EncodeUTF32(const Py_UNICODE *s,
PyObject * PyObject *
PyUnicode_AsUTF32String(PyObject *unicode) PyUnicode_AsUTF32String(PyObject *unicode)
{ {
return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), const Py_UNICODE *wstr;
PyUnicode_GET_SIZE(unicode), Py_ssize_t wlen;
NULL, wstr = PyUnicode_AsUnicodeAndSize(unicode, &wlen);
0); if (wstr == NULL)
return NULL;
return PyUnicode_EncodeUTF32(wstr, wlen, NULL, 0);
} }
/* --- UTF-16 Codec ------------------------------------------------------- */ /* --- UTF-16 Codec ------------------------------------------------------- */

View File

@ -1198,6 +1198,7 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
PyObject *cpathname_tmp; PyObject *cpathname_tmp;
#ifdef MS_WINDOWS /* since Windows uses different permissions */ #ifdef MS_WINDOWS /* since Windows uses different permissions */
mode_t mode = srcstat->st_mode & ~S_IEXEC; mode_t mode = srcstat->st_mode & ~S_IEXEC;
wchar_t *wdirname, *wpathname, *wpathname_tmp;
#else #else
mode_t dirmode = (srcstat->st_mode | mode_t dirmode = (srcstat->st_mode |
S_IXUSR | S_IXGRP | S_IXOTH | S_IXUSR | S_IXGRP | S_IXOTH |
@ -1230,7 +1231,12 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
} }
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
res = CreateDirectoryW(PyUnicode_AS_UNICODE(dirname), NULL); wdirname = PyUnicode_AsUnicode(dirname);
if (wdirname == NULL) {
PyErr_Clear();
return;
}
res = CreateDirectoryW(wdirname, NULL);
ok = (res != 0); ok = (res != 0);
if (!ok && GetLastError() == ERROR_ALREADY_EXISTS) if (!ok && GetLastError() == ERROR_ALREADY_EXISTS)
ok = 1; ok = 1;
@ -1268,8 +1274,19 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
return; return;
} }
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
(void)DeleteFileW(PyUnicode_AS_UNICODE(cpathname_tmp)); wpathname = PyUnicode_AsUnicode(cpathname);
fd = _wopen(PyUnicode_AS_UNICODE(cpathname_tmp), if (wpathname == NULL) {
PyErr_Clear();
return;
}
wpathname_tmp = PyUnicode_AsUnicode(cpathname_tmp);
if (wpathname_tmp == NULL) {
PyErr_Clear();
return;
}
(void)DeleteFileW(wpathname_tmp);
fd = _wopen(wpathname_tmp,
O_EXCL | O_CREAT | O_WRONLY | O_BINARY, O_EXCL | O_CREAT | O_WRONLY | O_BINARY,
mode); mode);
if (0 <= fd) if (0 <= fd)
@ -1322,7 +1339,7 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
/* Don't keep partial file */ /* Don't keep partial file */
fclose(fp); fclose(fp);
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
(void)DeleteFileW(PyUnicode_AS_UNICODE(cpathname_tmp)); (void)DeleteFileW(wpathname_tmp);
Py_DECREF(cpathname_tmp); Py_DECREF(cpathname_tmp);
#else #else
(void) unlink(PyBytes_AS_STRING(cpathbytes_tmp)); (void) unlink(PyBytes_AS_STRING(cpathbytes_tmp));
@ -1334,13 +1351,11 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
fclose(fp); fclose(fp);
/* Do a (hopefully) atomic rename */ /* Do a (hopefully) atomic rename */
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
if (!MoveFileExW(PyUnicode_AS_UNICODE(cpathname_tmp), if (!MoveFileExW(wpathname_tmp, wpathname, MOVEFILE_REPLACE_EXISTING)) {
PyUnicode_AS_UNICODE(cpathname),
MOVEFILE_REPLACE_EXISTING)) {
if (Py_VerboseFlag) if (Py_VerboseFlag)
PySys_FormatStderr("# can't write %R\n", cpathname); PySys_FormatStderr("# can't write %R\n", cpathname);
/* Don't keep tmp file */ /* Don't keep tmp file */
(void) DeleteFileW(PyUnicode_AS_UNICODE(cpathname_tmp)); (void) DeleteFileW(wpathname_tmp);
Py_DECREF(cpathname_tmp); Py_DECREF(cpathname_tmp);
return; return;
} }
@ -2268,18 +2283,22 @@ case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
WIN32_FIND_DATAW data; WIN32_FIND_DATAW data;
HANDLE h; HANDLE h;
int cmp; int cmp;
wchar_t *wname; wchar_t *wfilename, *wname;
Py_ssize_t wname_len; Py_ssize_t wname_len;
if (Py_GETENV("PYTHONCASEOK") != NULL) if (Py_GETENV("PYTHONCASEOK") != NULL)
return 1; return 1;
h = FindFirstFileW(PyUnicode_AS_UNICODE(filename), &data); wfilename = PyUnicode_AsUnicode(filename);
if (wfilename == NULL)
return -1;
h = FindFirstFileW(wfilename, &data);
if (h == INVALID_HANDLE_VALUE) { if (h == INVALID_HANDLE_VALUE) {
PyErr_Format(PyExc_NameError, PyErr_Format(PyExc_NameError,
"Can't find file for module %R\n(filename %R)", "Can't find file for module %R\n(filename %R)",
name, filename); name, filename);
return 0; return -1;
} }
FindClose(h); FindClose(h);