GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [subinterpreters] Eliminate PyInterpreterState.modules. · Issue #72597 · python/cpython

[subinterpreters] Eliminate PyInterpreterState.modules. #72597

New issue
Copy link
New issue
Copy link
Open
Open
[subinterpreters] Eliminate PyInterpreterState.modules.#72597
Copy link
Labels
3.13only security fixesonly security fixestopic-subinterpreters

Description

@ericsnowcurrently
ericsnowcurrently
opened on Oct 10, 2016
Issue body actions
BPO 28411
Nosy @brettcannon, @ncoghlan, @ericsnowcurrently
PRs
  • bpo-28411: Remove "modules" field from Py_InterpreterState. #1638
  • bpo-31404: Revert "remove modules from Py_InterpreterState (#1638)" #3565
  • bpo-28411: Isolate PyInterpreterState.modules #3575
  • bpo-28411: Support other mappings in PyInterpreterState.modules. #3593
  • bpo-28411: Remove PyInterpreterState.modules. #3606
  • bpo-28411: Fix redundant declaration of _PyImport_AddModuleObject #7992
  • [3.7] bpo-28411: Fix redundant declaration of _PyImport_AddModuleObject (GH-7992) #8017
  • Files
  • drop-interp-modules.diff
  • 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/ericsnowcurrently'
    closed_at = None
    created_at = <Date 2016-10-10.22:33:03.757>
    labels = ['expert-subinterpreters', 'type-bug', '3.7']
    title = '[subinterpreters] Eliminate PyInterpreterState.modules.'
    updated_at = <Date 2020-05-15.01:02:08.283>
    user = 'https://github.com/ericsnowcurrently'

    bugs.python.org fields:

    activity = <Date 2020-05-15.01:02:08.283>
    actor = 'vstinner'
    assignee = 'eric.snow'
    closed = False
    closed_date = None
    closer = None
    components = ['Subinterpreters']
    creation = <Date 2016-10-10.22:33:03.757>
    creator = 'eric.snow'
    dependencies = []
    files = ['45052']
    hgrepos = []
    issue_num = 28411
    keywords = ['patch']
    message_count = 15.0
    messages = ['278445', '278456', '278457', '278507', '278508', '278509', '278514', '278664', '301286', '302127', '302141', '302148', '302193', '302303', '335026']
    nosy_count = 4.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'grahamd', 'eric.snow']
    pr_nums = ['1638', '3565', '3575', '3593', '3606', '7992', '8017']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28411'
    versions = ['Python 3.7']

    Activity

    Next
    1. ericsnowcurrently commented on Oct 10, 2016

      @ericsnowcurrently
      ericsnowcurrently
      on Oct 10, 2016
      MemberAuthor
      More actions

      tl;dr PyInterpreterState does not need a "modules" field. Attached is a patch that removes it.

      During interpreter startup [1] the sys module is imported using the same C API [2] as any other builtin module. That API only requires one bit of import state, sys.modules. Obviously, while the sys module is being imported, sys.modules does not exist yet. To accommodate this situation, PyInterpreterState has a "modules" field. The problem is that PyInterpreterState.modules remains significant in the C-API long after it is actually needed during startup. This creates the potential for sys.modules and PyInterpreterState.modules to be out of sync.

      Currently I'm working on an encapsulation of the import state. PyInterpreterState.modules complicates the scene enough that I'd like to see it go away. The attached patch does so by adding private C-API functions that take a modules arg, rather than getting it from the interpreter state. These are used during interpreter startup, rendering PyInterpreterState.modules unnecessary and allowing sys.modules to become the single source of truth.

      If this patch lands, we can close bpo-12633.

      [1] see _Py_InitializeEx_Private() and Py_NewInterpreter() in Python/pylifecycle.c
      [2] see PyModule_Create2() and _PyImport_FindBuiltin() in Python/import.c

    2. ericsnowcurrently
      added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
      3.7 (EOL)end of lifeend of life
      on Oct 10, 2016
    3. ncoghlan commented on Oct 11, 2016

      @ncoghlan
      ncoghlan
      on Oct 11, 2016
      Contributor
      More actions

      (added Graham Dumpleton to the nosy list to ask if this change may impact mod_wsgi for 3.7)

      +1 on the general idea, but given that the current field is a public part of the interpreter state, the replacement access API should really be public as well - we can't be sure folks will always be going through the "PyImport_GetModuleDict()" API.

      If you replace the addition of the _PyImport_GetModuleDict API with a public PyInterpreterState_GetModuleDict API, I think that will cover it - the new calls would just be "PyInterpreterState_GetModuleCache(tstate->interp)" rather than _PyImport_GetModuleDict(tstate)

      Folks accessing this field directly can then define their own shim function if PyInterpreterState_GetModuleCache isn't defined.

      (The rationale for the GetModuleDict -> GetModuleCache change is that "ModuleDict" is ambiguous - every module has a dict. For PyImport_* we're stuck with it, but the "PyImport" prefix at least gives a hint that the reference might be to the sys.modules cache. That affordance doesn't exist for the "PyInterpeterState" prefix.

    4. grahamd commented on Oct 11, 2016

      @grahamd
      grahamdmannequin
      on Oct 11, 2016
      Mannequin
      More actions

      I always use PyImport_GetModuleDict(). So long as that isn't going away I should be good.

    5. ericsnowcurrently commented on Oct 11, 2016

      @ericsnowcurrently
      ericsnowcurrently
      on Oct 11, 2016
      MemberAuthor
      More actions

      What's the benefit to adding PyInterpreterState_GetModuleCache()? TBH, it should only be needed in this short period during startup when the import system hasn't been bootstrapped yet. After that code can import sys and access sys.modules from there. (For that matter, PyImport_GetModuleDict() doesn't buy all that much either...) I think all this would be clearer in a world with PEP-432. :)

      FWIW, I'm inclined to encourage new APIs where it makes sense that take an explicit interp arg. I just don't think a new public API is warranted here. If we didn't already have PyImport_GetModuleDict(), I'd probably just move the code to Python/pylifecycle.c, inlined or in a small static function.

      And +1 to ModuleCache vs. ModuleDict. :)

    6. ericsnowcurrently commented on Oct 11, 2016

      @ericsnowcurrently
      ericsnowcurrently
      on Oct 11, 2016
      MemberAuthor
      More actions

      Hmm, actually _PyImport_GetModuleDict() isn't needed to solve the startup issue. It's still rather internally focused but the same could be said for PyImport_GetModuleDict(). I guess I'm still not sold on adding a new public API function for what amounts to a rename of PyImport_GetModuleDict().

      Furthermore, wouldn't it make more sense to keep it in the PyImport_* namespace? Perhaps we could set the precedent that explicitly-arg'ed functions should be in the PyInterpreterState_* (or PyInterpreter_*) namespace?

    7. ericsnowcurrently commented on Oct 11, 2016

      @ericsnowcurrently
      ericsnowcurrently
      on Oct 11, 2016
      MemberAuthor
      More actions

      Meh, there really isn't any need for _PyImport_GetModuleDict(). I'll drop it. Problem solved! :)

    8. ncoghlan commented on Oct 12, 2016

      @ncoghlan
      ncoghlan
      on Oct 12, 2016
      Contributor
      More actions

      I just checked the docs, and it turns out I'm wrong about this being a previously public API: "There are no public members in this structure."

      From https://docs.python.org/3/c-api/init.html#c.PyInterpreterState

      That means the only externally supported API that needs to be preserved is PyImport_GetModuleDict() to get the current thread's module cache, and your original patch already did that.

    9. brettcannon commented on Oct 14, 2016

      @brettcannon
      brettcannon
      on Oct 14, 2016
      Member
      More actions

      As Nick pointed out, PyInterpreterState's fields are private so do what you want. :)

    10. ericsnowcurrently commented on Sep 4, 2017

      @ericsnowcurrently
      ericsnowcurrently
      on Sep 4, 2017
      MemberAuthor
      More actions

      New changeset 86b7afd by Eric Snow in branch 'master':
      bpo-28411: Remove "modules" field from Py_InterpreterState. (bpo-1638)
      86b7afd

    11. ericsnowcurrently
      closed this as completedon Sep 4, 2017
    12. ericsnowcurrently commented on Sep 13, 2017

      @ericsnowcurrently
      ericsnowcurrently
      on Sep 13, 2017
      MemberAuthor
      More actions

      FYI, this broke some (very) corner cases. See issue bpo-31404.

    13. ericsnowcurrently commented on Sep 14, 2017

      @ericsnowcurrently
      ericsnowcurrently
      on Sep 14, 2017
      MemberAuthor
      More actions

      We're reverting this (see bpo-31404), so back to the drawing board...

    14. ericsnowcurrently
      reopened this on Sep 14, 2017
    15. 12 remaining items

    16. erlend-aasland commented on Jul 24, 2022

      @erlend-aasland
      erlend-aasland
      on Jul 24, 2022
      Contributor
      More actions

      @ericsnowcurrently: is this issue and the linked PR still something you are working on, or has it been superseded by other more recent issues?

    17. ericsnowcurrently commented on Jul 25, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jul 25, 2022
      MemberAuthor
      More actions

      It is still alive, just way back on my TODO list. 🙂

      Reacted by Erlend E. Aasland
    18. kumaraditya303 commented on Jul 26, 2022

      @kumaraditya303
      kumaraditya303
      on Jul 26, 2022
      Contributor
      More actions

      @ericsnowcurrently I'll pick this one up.

      Reacted by Erlend E. Aasland
    19. kumaraditya303
      assigned
      kumaraditya303
      and unassigned
      ericsnowcurrently
      on Jul 26, 2022
    20. kumaraditya303
      added
      3.12only security fixesonly security fixes
      and removed
      3.7 (EOL)end of lifeend of life
      on Jul 26, 2022
    21. kumaraditya303 commented on Jul 26, 2022

      @kumaraditya303
      kumaraditya303
      on Jul 26, 2022
      Contributor
      More actions

      I have a WIP branch 1, it passes all the tests but has refleak. I'll investigate more once I get some time till then you can try it.

      Footnotes

      1. https://github.com/kumaraditya303/cpython/pull/6 ↩

    22. ericsnowcurrently commented on Jul 26, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jul 26, 2022
      MemberAuthor
      More actions

      Be cautious. There are some potentially tricky bits to this (as evidenced by #72597 (comment)).

      FWIW, there is no urgency with this at the moment.

    23. kumaraditya303 commented on Jul 26, 2022

      @kumaraditya303
      kumaraditya303
      on Jul 26, 2022
      Contributor
      More actions

      Be cautious. There are some potentially tricky bits to this (as evidenced by #72597 (comment)).

      FWIW, there is no urgency with this at the moment.

      Okay, it was on low priority anyways but we now have a updated branch against main to work with (if we want to).

      My top priority is Per Interpreter GIL for 3.12~3.13 and it's precursors which is more urgent and interesting than this ;)

      Reacted by Eric Snow
    24. ericsnowcurrently commented on Aug 31, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Aug 31, 2022
      MemberAuthor
      More actions

      I've closed my PR (gh-3606) but will probably get back to it some time down the road.

    25. kumaraditya303
      removed their assignment
      on Oct 22, 2022
    26. arhadthedev
      added
      3.13only security fixesonly security fixes
      and removed
      3.12only security fixesonly security fixes
      on May 15, 2023
    27. ericsnowcurrently
      removed
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Jul 8, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.13only security fixesonly security fixestopic-subinterpreters

      Projects

      • Subinterpreters
        Status
        Todo
        Show more project fields

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.