This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Decorater, jbfzs, ncoghlan, r.david.murray, rhettinger, serhiy.storchaka, vstinner, Александр Карпинский
Date 2017-11-22.12:23:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1511353414.42.0.213398074469.issue27535@psf.upfronthosting.co.za>
In-reply-to
Content
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().
History
Date User Action Args
2017-11-22 12:23:34vstinnersetrecipients: + vstinner, rhettinger, ncoghlan, r.david.murray, serhiy.storchaka, Decorater, Александр Карпинский, jbfzs
2017-11-22 12:23:34vstinnersetmessageid: <1511353414.42.0.213398074469.issue27535@psf.upfronthosting.co.za>
2017-11-22 12:23:34vstinnerlinkissue27535 messages
2017-11-22 12:23:34vstinnercreate