WINNT/9X patch for errors.c

Christian Tismer tismer at appliedbiometrics.com
Wed Apr 21 06:51:24 EDT 1999


This is a patch to errors.c which gives the correct POSIX
error messages for WinNT/9X.

Reason: os.listdir("nonexistent") gave
OSError: [Errno 3] No such process

Problem caused by:
The standard function strerror is supposed to give an error message for
a system error. Under Windows, these are DOS messages, not POSIX. 
They don't match completely.

Solution:
Instead of strerror, FormatMessage is used. This function
has an option to return standard system messages.

This will now give the correct message.
The messages are those which appear to be natural under
Windows: kernel32.dll defines them all in the default
language of your operating system

Guido van Rossum wrote:
> 
> Christian,
> 
> I just tried your code on Windows, and I noticed a missing feature.
> Python now adds the filename (when it is known) to the error message;
> your code doesn't do this.

Solved this, too. My code was ok, but the filename moved into
the next line. The returned message contains a CR/LF sequence
and also a dot. I'm removing this now, and it looks good.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home
-------------- next part --------------
*** /w/orion/install/cvsroot/python/dist/src/python/errors.c	Mon Dec 21 19:33:30 1998
--- /w/orion/install/python1.5/errors.c	Wed Apr 21 12:13:40 1999
***************
*** 49,54 ****
--- 49,59 ----
  #endif
  #endif
  
+ #ifdef _WIN32
+ #include "windows.h"
+ #include "winbase.h"
+ #endif
+ 
  void
  PyErr_Restore(type, value, traceback)
  	PyObject *type;
***************
*** 291,297 ****
--- 296,319 ----
  	if (i == 0)
  		s = "Error"; /* Sometimes errno didn't get set */
  	else
+ #ifndef _WIN32
  		s = strerror(i);
+ #else
+ 	{ 
+ 		int len = FormatMessage(     
+ 			FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ 			FORMAT_MESSAGE_FROM_SYSTEM |     
+ 			FORMAT_MESSAGE_IGNORE_INSERTS,    
+ 			NULL,	// no message source
+ 			i,
+ 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ 			(LPTSTR) &s,    
+ 			0,      // size not used
+ 			NULL ); // no args
+ 		// remove trailing cr/lf and dots
+ 		while(len && s[len-1] <= '.') s[--len] = '\0' ;
+ 	}
+ #endif
  	if (filename != NULL && Py_UseClassExceptionsFlag)
  		v = Py_BuildValue("(iss)", i, s, filename);
  	else
***************
*** 300,305 ****
--- 322,330 ----
  		PyErr_SetObject(exc, v);
  		Py_DECREF(v);
  	}
+ #ifdef _WIN32
+ 	LocalFree(s);
+ #endif
  	return NULL;
  }
  	


More information about the Python-list mailing list