STINNER Victor <victor.stin...@gmail.com> added the comment:

I implemented a cache for warnings.filters in C. With my WIP patch which 
doesn't touch the registry for the ignore action, warnings.warn() is now faster 
than the current code ;-)

haypo@selma$ ./python -m perf compare_to ref.json patch.json 
Mean +- std dev: [ref] 938 ns +- 72 ns -> [patch] 843 ns +- 57 ns: 1.11x faster 
(-10%)

There is a single test in test_warnings which modifies directly 
warnings.filters:

    @support.cpython_only
    def test_issue31416(self):
        # warn_explicit() shouldn't cause an assertion failure in case of a
        # bad warnings.filters or warnings.defaultaction.
        wmod = self.module
        with original_warnings.catch_warnings(module=wmod):
            wmod.filters = [(None, None, Warning, None, 0)]
            with self.assertRaises(TypeError):
                wmod.warn_explicit('foo', Warning, 'bar', 1)

            wmod.filters = []
            with support.swap_attr(wmod, 'defaultaction', None), \
                 self.assertRaises(TypeError):
                wmod.warn_explicit('foo', Warning, 'bar', 1)

I don't think that it's common to modify warnings.filters directly. Maybe we 
can make this sequence immutable in the public API to prevent misuse of the 
warnings API? To force users to use warnings.simplefilter() and 
warnings.filterwarnings()?

IMHO the main usage of modifying directyl warnings.filters it to save/restore 
filters. But there is already an helper for that: warnings.catch_warnings().

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue27535>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to