Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How I would (Like To) Change Import

How I would (Like To) Change Import

PyCon 2008 lightning talk

Brett Cannon

March 15, 2008
Tweet

More Decks by Brett Cannon

Other Decks in Programming

Transcript

  1. __import__()’s API sucks • local argument is not used. •

    Only two keys out of globals matters. • fromlist only there for opcodes. • Going to complain about this later. • Parser can split on dots, so why make the function do it?
  2. How I would change it • name • Tuple of

    names, with each item a package level. • Empty strings/None represent a dot. • Removes need for level. • Still not sure if I like its removal, though.
  3. If I can’t change it ... • A saner API

    wrapper in the imp module. • Do people like having control over whether the top-level or specified module is returned?
  4. sys.meta_path under-utilized • Nothing put here by default. • Yet

    really handy to use for control/ customization.
  5. sys.meta_path defaults • Theoretically, sys.modules. • But probably too much

    of a special-case. • Built-in modules. • Frozen modules. • Extension modules. • .py/.pyc modules. • Ditch sys.path_hooks, sys.path_importer_cache.
  6. Unfortunately ... • Changes what takes precedence in choosing what

    to import. • Currently searched by location on sys.meta_path/sys.path, then type of module. • Proposal would reverse this.
  7. I don’t like sys.meta_path/sys.path • I would rather have a

    single, unified approach to having importers. • Stuff on sys.meta_path already knows whether it cares about its path argument or not.
  8. How I would change it • sys.importers • Almost exactly

    like sys.meta_path. • path argument always given. • __path__ of parent or sys.path. • Treat path as a “suggestion” of where to look, with sys.path the default suggestion.
  9. loader.load_module() over-burdened • Has to deal with sys.modules for reloading.

    • But that’s there only to support reload(). • ... which is no longer a built-in in Py3k. • Has to set various values on module that were already known to import.
  10. Not sure how I would change it • Pass in

    a module to load_module()? • With PEP 3121 this might make creating a new module blindly a waste.
  11. What __import__() returns is complicated • Depends on the value

    of fromlist. • Mostly there for the benefit of import- specific opcodes. • But shouldn’t the opcodes just deal with their own stuff?
  12. Always return the same thing • Opcode to import a

    module. • Opcode to return the top-level module. • For ``import monty``. • Opcode to handle a list of specifc things to import. • For ``from monty import eels, hovercraft``.