[Python-3000-checkins] r59682 - python/branches/py3k/Python/dynload_win.c
Guido van Rossum
guido at python.org
Fri Jan 4 00:44:05 CET 2008
Is this Py3k specific?
On Jan 3, 2008 3:42 PM, amaury.forgeotdarc
<python-3000-checkins at python.org> wrote:
> Author: amaury.forgeotdarc
> Date: Fri Jan 4 00:42:13 2008
> New Revision: 59682
>
> Modified:
> python/branches/py3k/Python/dynload_win.c
> Log:
> On Windows, when import fails to load a dll module, the message says
> "error code 193" instead of a more informative text.
>
> It turns out that FormatMessage needs additional parameters for some error codes.
> For example: 193 means "%1 is not a valid Win32 application".
> Since it is impossible to know which parameter to pass, we use
> FORMAT_MESSAGE_IGNORE_INSERTS to get the raw message, which is still better
> than the number.
>
> Also use the Unicode version of the API, to deal with accented letters.
>
>
>
> Modified: python/branches/py3k/Python/dynload_win.c
> ==============================================================================
> --- python/branches/py3k/Python/dynload_win.c (original)
> +++ python/branches/py3k/Python/dynload_win.c Fri Jan 4 00:42:13 2008
> @@ -183,33 +183,35 @@
> hDLL = LoadLibraryEx(pathname, NULL,
> LOAD_WITH_ALTERED_SEARCH_PATH);
> if (hDLL==NULL){
> - char errBuf[256];
> + PyObject *message;
> unsigned int errorCode;
>
> /* Get an error string from Win32 error code */
> - char theInfo[256]; /* Pointer to error text
> + wchar_t theInfo[256]; /* Pointer to error text
> from system */
> int theLength; /* Length of error text */
>
> errorCode = GetLastError();
>
> - theLength = FormatMessage(
> - FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
> + theLength = FormatMessageW(
> + FORMAT_MESSAGE_FROM_SYSTEM |
> + FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */
> NULL, /* message source */
> errorCode, /* the message (error) ID */
> - 0, /* default language environment */
> - (LPTSTR) theInfo, /* the buffer */
> + MAKELANGID(LANG_NEUTRAL,
> + SUBLANG_DEFAULT),
> + /* Default language */
> + theInfo, /* the buffer */
> sizeof(theInfo), /* the buffer size */
> NULL); /* no additional format args. */
>
> /* Problem: could not get the error message.
> This should not happen if called correctly. */
> if (theLength == 0) {
> - PyOS_snprintf(errBuf, sizeof(errBuf),
> - "DLL load failed with error code %d",
> - errorCode);
> + message = PyUnicode_FromFormat(
> + "DLL load failed with error code %d",
> + errorCode);
> } else {
> - size_t len;
> /* For some reason a \r\n
> is appended to the text */
> if (theLength >= 2 &&
> @@ -218,13 +220,16 @@
> theLength -= 2;
> theInfo[theLength] = '\0';
> }
> - strcpy(errBuf, "DLL load failed: ");
> - len = strlen(errBuf);
> - strncpy(errBuf+len, theInfo,
> - sizeof(errBuf)-len);
> - errBuf[sizeof(errBuf)-1] = '\0';
> + message = PyUnicode_FromString(
> + "DLL load failed: ");
> +
> + PyUnicode_AppendAndDel(&message,
> + PyUnicode_FromUnicode(
> + theInfo,
> + theLength));
> }
> - PyErr_SetString(PyExc_ImportError, errBuf);
> + PyErr_SetObject(PyExc_ImportError, message);
> + Py_XDECREF(message);
> return NULL;
> } else {
> char buffer[256];
> _______________________________________________
> Python-3000-checkins mailing list
> Python-3000-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-3000-checkins
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000-checkins
mailing list