Skip to content

Re-implement parts of imp in pure Python #58167

Closed

Description

@brettcannon
BPO 13959
Nosy @brettcannon, @birkenfeld, @pitrou, @vstinner, @benjaminp, @merwok, @meadori, @ericsnowcurrently, @berkerpeksag
PRs
  • bpo-32030: pass interp to _PyImport_Init() #4736
  • Dependencies
  • bpo-2377: Replace import w/ importlib.import
  • bpo-14605: Make import machinery explicit
  • bpo-14657: Avoid two importlib copies
  • bpo-15166: Implement imp.get_tag() using sys.implementation
  • bpo-15167: Re-implement imp.get_magic() in pure Python
  • Files
  • imp.diff
  • imp.diff
  • use_of_imp.txt: stdlib modules that use imp (could be refactored to use importlib)
  • imp_find_module.diff: sketch of find_module() implementation
  • issue13959_reload.diff: port of _imp.reload() to imp.py
  • issue13959_magic_and_tag.diff: one approach to moving the magic and tag code out of import.c
  • explicit_meta_path.diff
  • explicit_path_hooks.diff
  • issue13959_magic.diff: porting MAGIC to importlib
  • issue13959_tag.diff: porting TAG to importlib
  • imptest.py
  • issue13959_get_tag.diff: new TAG implementation using sys.implementation
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/brettcannon'
    closed_at = <Date 2012-07-09.20:23:48.007>
    created_at = <Date 2012-02-07.00:51:29.679>
    labels = ['library', 'release-blocker']
    title = 'Re-implement parts of imp in pure Python'
    updated_at = <Date 2017-12-06.16:25:57.617>
    user = 'https://github.com/brettcannon'

    bugs.python.org fields:

    activity = <Date 2017-12-06.16:25:57.617>
    actor = 'vstinner'
    assignee = 'brett.cannon'
    closed = True
    closed_date = <Date 2012-07-09.20:23:48.007>
    closer = 'brett.cannon'
    components = ['Library (Lib)']
    creation = <Date 2012-02-07.00:51:29.679>
    creator = 'brett.cannon'
    dependencies = ['2377', '14605', '14657', '15166', '15167']
    files = ['24846', '24979', '24980', '25250', '25302', '25303', '25308', '25309', '25335', '25336', '25834', '26058']
    hgrepos = []
    issue_num = 13959
    keywords = ['patch']
    message_count = 76.0
    messages = ['152800', '155790', '156473', '156474', '156475', '156527', '156530', '156964', '158269', '158362', '158364', '158365', '158370', '158371', '158377', '158383', '158398', '158474', '158522', '158531', '158555', '158575', '158593', '158594', '158622', '158628', '158704', '158895', '158908', '158930', '158931', '158939', '158941', '158951', '158955', '158968', '158971', '158985', '158986', '158995', '158998', '158999', '159118', '159119', '159628', '159630', '159631', '159641', '159644', '159757', '159758', '159960', '159962', '159969', '160424', '160433', '160434', '160439', '160493', '160495', '160524', '162276', '162356', '162357', '162364', '162384', '162940', '163212', '163232', '163569', '163798', '164102', '165116', '165120', '165148', '307747']
    nosy_count = 12.0
    nosy_names = ['brett.cannon', 'georg.brandl', 'jpe', 'pitrou', 'vstinner', 'benjamin.peterson', 'eric.araujo', 'Arfrever', 'meador.inge', 'python-dev', 'eric.snow', 'berker.peksag']
    pr_nums = ['4736']
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'needs patch'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue13959'
    versions = ['Python 3.3']

    Activity

    1. brettcannon commented on Feb 7, 2012

      @brettcannon
      MemberAuthor

      A bunch of code in Python/import.c exists purely for the imp module, but a large chunk of it does not need to be implemented in C but instead can be implemented in Python code.

      Once importlib is bootstrapped in then an attempt to scale back the C code should be done by re-implementing parts of imp in pure Python (either in some _imp module or directly in _importlib itself).

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Feb 7, 2012
    3. brettcannon commented on Mar 14, 2012

      @brettcannon
      MemberAuthor

      Attached is a start for re-implementing imp. Still need code for cache_from_source, source_from_cache, find_module, load_module, reload, load_compiled, load_source, and load_package.

    4. ericsnowcurrently commented on Mar 21, 2012

      @ericsnowcurrently
      Member

      Here's an incomplete (though essentially test-passing) patch that flips more of imp into importlib. It builds on Brett's patch. The gist is that imp.py is a minimal wrapper around importlib, which uses _imp directly.

      Ultimately the things implemented in Lib/importlib/_bootstrap.py should be yanked out of Python/import.c (and Python/dynload_*.c). I expect that this will wait for the bootstrap merge (bpo-2377) to land.

    5. ericsnowcurrently commented on Mar 21, 2012

      @ericsnowcurrently
      Member

      Here's a listing of the places where import-ish modules are used in CPython. Once things all the import stuff currently on the table is wrapped up, I'll probably work on switching things over to using importlib directly.

    6. ericsnowcurrently commented on Mar 21, 2012

      @ericsnowcurrently
      Member

      Of note for my patch is the addition of SUFFIXES and MODE to the 3 main "file loader" classes. I did this to reverse the dependence on imp.get_suffixes().

      As well, a couple of extra functions are added to Python/importlib/_bootstrap.py for a similar reason, but should also be meaningful externally in their own right.

      load_module_from_file(), is one of these, though the current incarnation relies on a mythical new method on loaders that may not pass muster.

      (FWIW, I accidently included the use_of_imp.txt file in the patch and don't intend it to get included)

    7. merwok commented on Mar 22, 2012

      @merwok
      Member

      Once things all the import stuff currently on the table is wrapped up,
      I'll probably work on switching things over to using importlib directly.

      Please leave distutils unchanged; we don’t clean it up or improve it in any way, it’s bugfixes only. Changing packaging is fine; I’ll just keep using the imp module in the distutils2-python3 backport.

    8. ericsnowcurrently commented on Mar 22, 2012

      @ericsnowcurrently
      Member

      Sounds good. It will be a while before we get there, but at that point I was already planning on getting in touch with the maintainers of relevant modules before messing with them.

    9. ericsnowcurrently commented on Mar 28, 2012

      @ericsnowcurrently
      Member

      (Éric, I'd reply in the review if reitveld recognized my current username -- see http://psf.upfronthosting.co.za/roundup/meta/issue402. FYI, I also did not get an email for your review, which is likely related to that issue.)

      Regarding __all__, I didn't realize that about being a tuple. I'll fix it.

      As to the "dm" in the SOABI value, that is definitely something we'll have to factor in. There are actually a few such places in the current patch that rely on problematic data. It's either hidden in C or not available during bootstrapping. That's something we'll have to iron out before the solution for this issue gets committed.

    10. pitrou commented on Apr 14, 2012

      @pitrou
      Member

      I think this should be a blocker for 3.3.

    11. brettcannon commented on Apr 15, 2012

      @brettcannon
      MemberAuthor

      Just because I was thinking about it, I wonder if necessarily all the frozen stuff really needs to stay in import.c. I mean a frozen module is really just an entry in an array of structs that has a name of an char*[]. I don't see why one couldn't simply have a get_frozen_bytes() method to convert that char*[] into a bytes object and use that to construct a module in pure Python code.

    12. python-dev commented on Apr 15, 2012

      python-devmannequin
      Mannequin

      New changeset d777f854a66e by Brett Cannon in branch 'default':
      Issue bpo-13959: Rename imp to _imp and add Lib/imp.py and begin
      http://hg.python.org/cpython/rev/d777f854a66e

    13. brettcannon commented on Apr 15, 2012

      @brettcannon
      MemberAuthor

      OK, so I have started to check this stuff in, but I think it's best to do it piecemeal. Going forward I would like to commit in units of functions being replaced, and prioritize stuff that cuts out C code (e.g. the load_*() methods, find_module(), etc.). That way it's clear that progress is being made. Obviously the best way to tell if code is hanging on just because of imp is to comment out the interface code and see what your compiler complains about in terms of dead code (or at least clang is good at this).

    14. brettcannon commented on Apr 15, 2012

      @brettcannon
      MemberAuthor

      It looks like in order to get a clear sign of what it will take to remove various parts of import.c, imp.load_module() needs to go along with imp.load_package() (since they call each other in the C code). You also have to take care of imp.reload(), but I am simplifying the C code greatly and will take care of referencing other code in the module.

    15. 55 remaining items

    16. birkenfeld commented on Jun 19, 2012

      @birkenfeld
      Member

      Which parts are still missing here?

    17. ericsnowcurrently commented on Jun 19, 2012

      @ericsnowcurrently
      Member

      Here are the things I'm aware of:

      • implement imp.get_tag() using sys.implementation
      • *maybe* implement imp.get_magic() in pure Python (patch already attached here)
      • (low priority) find a way to rip out the bulk of PyImport_ExecCodeModuleObject() from imp.c, if practical

      From my discussions with Brett, there was a laundry list he was aiming to get in for 3.3 (for import stuff in general). I know much of it got done, but not all of it. Of the remainder, I'm not sure how much of that he still wants to see get in. Some of it pertains to this issue, though not all.

    18. birkenfeld commented on Jun 23, 2012

      @birkenfeld
      Member

      OK, sounds like none of it would block beta1.

    19. brettcannon commented on Jun 24, 2012

      @brettcannon
      MemberAuthor

      Sorry, been on vacation the past week.

      Georg is right, nothing left is a b1 blocker, just stuff I want to get in before 3.3 ships.

    20. birkenfeld commented on Jun 26, 2012

      @birkenfeld
      Member

      Moving back to blocker for beta2.

    21. python-dev commented on Jul 9, 2012

      python-devmannequin
      Mannequin

      New changeset efb5e6ab10f4 by Brett Cannon in branch 'default':
      Issue bpo-15167 (as part of bpo-13959): imp.get_magic() is no implemented in
      http://hg.python.org/cpython/rev/efb5e6ab10f4

    22. brettcannon commented on Jul 9, 2012

      @brettcannon
      MemberAuthor

      Since the final issue is just a nicety, I am considering this issue closed.

    23. ericsnowcurrently commented on Jul 10, 2012

      @ericsnowcurrently
      Member

      hurray!

    24. vstinner commented on Dec 6, 2017

      @vstinner
      Member

      New changeset 672b6ba by Victor Stinner in branch 'master':
      bpo-32030: pass interp to _PyImport_Init() (bpo-4736)
      672b6ba

    25. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    release-blockerstdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions