Warnings killing my performance

Peter Otten __peter__ at web.de
Sun Feb 8 06:07:11 EST 2004


Kylotan wrote:

> Peter Otten <__peter__ at web.de> wrote in message
> news:<c02ubl$qqt$07$1 at news.t-online.com>...
> 
>> def disableWarnings():
>>     def _theevilunwarner(*args):
>>         pass
>>     import warnings
>>     warnings.warn = _theevilunwarner
>>     warnings.warn_explicit = _theevilunwarner
> 
> Heh, that's great. However, I don't think it really scales well to a
> full application, does it? :)

Well, you could disable warnings in critical parts only, but that wouldn't
play well with threads. Enter the not so evil unwarner:

def disableWarnings():
    originalWarn = warnings.warn
    disabled = sets.Set([
        id(exceptions.OverflowWarning),
        id(exceptions.FutureWarning)])

    def _thenotsoevilunwarner(message, category=None, stacklevel=1):
        if id(category) in disabled:
            #print "DISMISS", message, category
            pass
        else:
            #print "PROPAGATE", message, category
            originalWarn(message, category, stacklevel)
    warnings.warn = _thenotsoevilunwarner

It's still pretty fast (10.8 vs 27.1 usec on my machine), but the duplicate
test will slow down warnings that are actually propagated.

And if you are really ambitious, you could devise a patch to speed up warn()
in the library, maybe using a similar technique as shown above.

Peter




More information about the Python-list mailing list