classification
Title: Consider moving importlib.abc.InspectLoader.source_to_code() to importlib.abc.Loader
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, eric.snow, python-dev
Priority: low Keywords:

Created on 2014-04-04 17:59 by brett.cannon, last changed 2014-05-13 22:39 by berker.peksag. This issue is now closed.

Messages (6)
msg215544 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-04-04 17:59
importlib.abc.InspectLoader.source_to_code exists on InspectLoader because InspectLoader.get_code() uses it. But there is technically no reason to leave it there and not simply move it up to importlib.abc.Loader(). There is also no reason to not make it a staticmethod so that one can use it without worrying about abstractmethod overrides.
msg215628 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-04-05 20:46
source_to_code() seems like a good fit on InspectLoader to me.  Is there
something in particular that motivated the idea of moving it up to Loader?
msg215631 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-04-05 20:54
The inspiration was that I realized there was no technical reason to have it on InspectLoader. Past that there was my thinking of trying to come up with a source_to_module() method on importlib.abc.Loader which would do the right thing with create_module() and init_module_attrs() such that replicating imp.load_module() would be something like::

  loader = importlib.machinery.SourceLoader  # or something
  with open('file.py') as file:
    module = loader.source_to_module(file.read(), location='file.py')
msg215637 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-04-05 22:39
Now that I've thought about it a little more, I'm more open to the idea.  Source is definitely a universal concept in Python, as is code.  So source_to_code() makes some sense on a most-base type like Loader.  On the other hand, source isn't necessarily associated with all loaders.

I guess my initial gut reaction was that it seemed out of place API-wise.  On InspectLoader it's presence is tied to get_code(), so it makes more sense.
msg218174 - (view) Author: Roundup Robot (python-dev) Date: 2014-05-09 16:28
New changeset 9bd844792b32 by Brett Cannon in branch 'default':
Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a
http://hg.python.org/cpython/rev/9bd844792b32
msg218175 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-05-09 16:29
I decided to make it a static method instead of relocating it.
History
Date User Action Args
2014-05-13 22:39:44berker.peksagsetstage: test needed -> resolved
2014-05-09 16:29:21brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg218175
2014-05-09 16:28:29python-devsetnosy: + python-dev
messages: + msg218174
2014-04-05 22:39:03eric.snowsetmessages: + msg215637
2014-04-05 20:54:42brett.cannonsetmessages: + msg215631
2014-04-05 20:46:24eric.snowsetnosy: + eric.snow
messages: + msg215628
2014-04-04 18:00:41brett.cannonsettype: behavior -> enhancement
2014-04-04 17:59:20brett.cannoncreate