Flexible warning system

Simon Forman sajmikins at gmail.com
Mon Jun 29 03:21:16 EDT 2009

On Sun, Jun 28, 2009 at 2:31 PM, Shrutarshi Basu<shr at basushr.net> wrote:
> I'm writing a Python package where I have an underlying object model that is
> manipulated by a runtime control layer and clients that interface with this
> runtime. As I'm developing this i'm realizing that there are going to be a
> number of places where the runtime might affect the object model in ways
> that might not be immediately obvious to the user. I would like to have some
> sort of warning system where the runtime can raise a warning and then the
> clients can 'catch' those warnings and display them as they want to. Is
> there some sort of a system that will operates like that or will I have to
> roll my own? If I do need to roll my own, any ideas on how I should go about
> it? I know that there is a warning module, but it seems to that all outputs
> go to standard out which isn't what I want.
> Thanks,
> Basu
> Shrutarshi Basu
> Computer Science,
> Electrical and Computer Engineering,
> Lafayette College,
> The ByteBaker -- http://bytebaker.com

I just glanced at the docs for the warnings module and it seems like
exactly what you're asking for, and you can change the default writing
to stdout:

"Warning messages are normally written to sys.stderr, but their
disposition can be changed flexibly, from ignoring all warnings to
turning them into exceptions. The disposition of warnings can vary
based on the warning category (see below), the text of the warning
message, and the source location where it is issued. Repetitions of a
particular warning for the same source location are typically
suppressed." - http://docs.python.org/library/warnings.html

That said, why not just use exceptions?


More information about the Python-list mailing list