[Python-ideas] __loader__.get_source(): semantics of returning None

Paul Moore p.f.moore at gmail.com
Sat Apr 28 14:29:44 EDT 2018


On 28 April 2018 at 18:20, Jeroen Demeyer <J.Demeyer at ugent.be> wrote:
> On 2018-04-28 18:26, Paul Moore wrote:
>>
>> """
>> If a file named filename is not found, the function will look for it
>> in the module search path, sys.path, after first checking for a PEP
>> 302 __loader__ in module_globals, in case the module was imported from
>> a zipfile or other non-filesystem import source.
>> """
>
>
> Sorry, I should have been more precise. linecache does the following (which
> is what is in the doc you quoted above):
>
> (1) Look for the filename exactly as given.
> (2) Look for a loader and call its get_source() method.
> (3) Look for the filename under all sys.path entries.
>
> The difference between (1) and (3) is when a relative filename is given,
> typically relative to site-packages. And I'm actually interested in that
> third case.
>
> So the question is really: should (3) still be tried if
> __loader__.get_source() returns None?

Well, the docs say it is, so I'd say yes. I guess the docs could be
interpreted as saying "if there isn't a loader, go on to (3) otherwise
call the loader and stop". But I'd say that if __loader__.get_source()
returns None, that should be treated the same as no loader being
found.

Paul


More information about the Python-ideas mailing list