Author Oren Milman
Recipients Oren Milman
Date 2017-08-26.18:48:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1503773302.07.0.816913451611.issue31285@psf.upfronthosting.co.za>
In-reply-to
Content
1.
the following causes an assertion failure in Python/_warnings.c in
show_warning():

import warnings

class BadLoader:
    def get_source(self, fullname):
        class BadSource:
            def splitlines(self):
                return [42]
        return BadSource()

del warnings._showwarnmsg
warnings.warn_explicit(message='foo', category=ArithmeticError, filename='bar',
                       lineno=1, module_globals={'__loader__': BadLoader(),
                                                 '__name__': 'foobar'})

in short, the assertion failure would happen in warnings.warn_explicit() in case
module_globals['__loader__'].get_source(module_globals['__name__']).splitlines()[lineno-1]
is not a str.


2.
the following raises a SystemError:

import warnings

class BadLoader:
    def get_source(self, fullname):
        class BadSource:
            def splitlines(self):
                return 42
        return BadSource()

warnings.warn_explicit(message='foo', category=UserWarning, filename='bar',
                       lineno=42, module_globals={'__loader__': BadLoader(),
                                                  '__name__': 'foobar'})

in short, warnings.warn_explicit() raises the SystemError in case
module_globals['__loader__'].get_source(module_globals['__name__']).splitlines()
is not a list.



ISTM that adding a check in warnings_warn_explicit() (in Python/_warnings.c),
to check whether
module_globals['__loader__'].get_source(module_globals['__name__'])
is a str (after existing code found out that it isn't None) would prevent both
the assertion failure and the SystemError.

What do you think? Is it OK to permit get_source() to return only None or a str?
History
Date User Action Args
2017-08-26 18:48:22Oren Milmansetrecipients: + Oren Milman
2017-08-26 18:48:22Oren Milmansetmessageid: <1503773302.07.0.816913451611.issue31285@psf.upfronthosting.co.za>
2017-08-26 18:48:22Oren Milmanlinkissue31285 messages
2017-08-26 18:48:21Oren Milmancreate