2009-09-02

Intersection of built-in modules between CPython, Jython and IronPython

[EDIT: updated for IronPython 2.6b2; made it clearer which VMs are missing what modules that importlib relies upon]

It has been a big goal of mine to make importlib the default implementation of import for CPython. But an even bigger goal has been to make it the default implementation for ALL full featured implementations of Python once they implement Python 3. Not only would it make sure that all VMs have consistent semantics when it came to imports, but to also prevent every VM from having to re-implement import themselves.

But using importlib as import imposes a bootstrapping problem. How do you import, well, import? First off, you need to find the source code, compile it into a code object, and create a module object using that code object. That part is actually easy as you can simply look for the file on sys.path since you know what you are looking for, you can compile the source using the built-in compile() function, and then you finally create a module and initialize it with exec(). This is essentially what importlib does at a rudimentary level.

But import obviously goes beyond the rudimentary. There is bytecode to read and write, packages to deal with, warnings to raise, etc. And all of that requires code from some module in the standard library. But if you are trying to bootstrap in import w/o having a full-featured import, what do you do? You rely on built-in modules is what you do.

By using built-in modules you could have the VM inject any built-in module into the created importlib module and have it begin using it. Because of this I was curious as to what built-in modules CPython 3.1, Jython 2.5, and IronPython 2.6b2 had in common. The results are:
  • _codecs
  • _functools
  • _sre
  • _weakref
  • errno
  • gc
  • imp
  • sys
Not a whole lot. Importlib itself relies upon:
errno
Everyone has this.

io
IronPython's _bytesio probably has what I need (importlib only uses io.FileIO). Jython does not cover yet 2.6 so there is hope.

imp
Everyone has this.

marshal
This is actually optional (or at least I will make sure it is) as VMs do not need to implement pyc support.

posix/nt/os2
IronPython has this. Jython plans to have this in 2.6.

sys
Everyone has this.

warnings
Jython does not have a native implementation, but importlib only needs warnings.warn().

There is a partial overlap, but not a complete overlap. Luckily this is for Python 3 and thus there is hope that some of the things I need can be made common between the VMs in terms of what the built-in modules provide. It's possible that IronPython has everything already and Jython could add only what importlib needs (probably) w/o much issue.

Otherwise I am causing myself more pain than I need to and I should just not worry about the bootstrap and simply import code directly. Copying code from the 'os' module does get a little annoying after a while. =)