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

Jeroen Demeyer J.Demeyer at UGent.be
Sat Apr 28 12:09:10 EDT 2018

The documentation of __loader__.get_source() says

"Returns None if no source is available (e.g. a built-in module)."

But what does "no source is available" mean precisely? It could mean 
either of two things:

(A) I am absolutely certain that there is no source anywhere to be found.

(B) I don't know where to find the source, but if you look hard enough, 
you may find it.

Currently, linecache interprets it as (A). Is there any chance that we 
can either change the interpretation for returning None to (B) or to 
provide an officially documented way to answer (B)? This could be using 
a new return value (say, NotImplemented) or by not implementing 
get_source at all (such that __loader__.get_source raises 
AttributeError). The latter is probably how things already work in 
practice, but it isn't really documented that way.

The context for this question/proposal is https://bugs.python.org/issue32797

When the linecache module is asked for the source code of a certain 
file, it queries the __loader__.get_source() for the source code. If 
this returns None, that's the end: no source is returned.

However, linecache is also passed the filename! So even if it could find 
the source by filename, it won't even try that if get_source() returns None.


More information about the Python-ideas mailing list