[Python-3000] Extending warnings for Py3K - any interest?

Walter Dörwald walter at livinglogic.de
Mon Nov 20 16:46:20 CET 2006


Andrew Clover wrote:

> I've always felt slightly disappointed by the Python warnings interface. 
> It's great for managing language feature deprecation, but I've often 
> wanted something I could also use for recoverable errors in general.
> 
> I've written multiple application-specific mechanisms for allowing a 
> caller to customise handling of potentially recoverable conditions in 
> the past, so I think it's probably a fairly common use case, and maybe, 
> if others how found the same, worth standardising as an extension of the 
> existing Python warnings system.
> 
> Here's a first sketch of the sort of thing I'm thinking of:
> 
>    http://doxdesk.com/file/software/py/v/warnings2-0.1.py
> 
> Features:
> 
>    - warnings as instances with arbitrary properties, rather than being
>      limited to a string message;

That's already possible:

class MyWarning(Warning):
   ...

warnings.warn(MyWarning(...))

>    - different severities with different default actions, so trivial
>      warnings can be ignored by default and things which are by default
>      errors can be recovered if a filter says so;
>
>    - option to store warning instances that occur for later handling;
> 
>    - switchable warnings-handling contexts (like eg. the decimal module)
>      for temporary changes to filters;
> 
>    - context manager helpers so you can say things like:
> 
>      with ignoring(DeprecationWarning):
>          import oldmodule
> 
>      with storing(InvalidDataWarning) as problems:
>          reghive.scanKeys()
>      for problem in problems: ...

+1

> Anyone agree/disagree with the idea of adding such functionality to 
> warnings, or what features are worthwhile?

Another idea: Actions might be callables, so that they can be used
directly in handleWarning() instead of dispatching on the action name.

BTW, your source code is not PEP 8 compatible.

Servus,
   Walter


More information about the Python-3000 mailing list