Skip to content

Linecache updatecache can perform many useless os calls #140228

Description

@tconley1428

Bug report

Bug description:

Consider the scenario where updatecache is called with a frozen module and module_globals is None.

In that case, _source_unavailable returns false because it is a frozen module, but because module_globals is None, the code carries on to look for the 'file' <frozen module> everywhere in sys.path. Compare that to the behavior prior to #132662 where that frozen module would simply return unavailable. I would recommend the following change:

    if filename.startswith("<frozen ") and module_globals is not None:
        # This is a frozen module, so we need to use the filename
        # from the module globals.
        fullname = module_globals.get("__file__")
        if fullname is None:
            print("fullname none")
            return []

Into

    if filename.startswith("<frozen "):
        if module_globals is None:
            return []

        # This is a frozen module, so we need to use the filename
        # from the module globals.
        fullname = module_globals.get("__file__")
        if fullname is None:
            print("fullname none")
            return []

For context, this is a problem for us because we run in a sandbox which restricts many OS operations, and this is performing useless os operations where it didn't prior to 3.14.

If there is agreement and it is helpful, happy to contribute a PR for this small change.

CPython versions tested on:

3.14

Operating systems tested on:

macOS, Windows, Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-refactorCode refactoring (with no changes in behavior)
    performancePerformance or resource usage
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Oct 16, 2025
  2. tconley1428 commented on Oct 17, 2025

    @tconley1428
    ContributorAuthor

    I would argue that type-bug is appropriate. While in most cases the impact is limited to performance, for our use case the additional filesystem calls cause a failure.

  3. gaogaotiantian commented on Oct 18, 2025

    @gaogaotiantian
    Member

    I did this PR and I agree this should be changed. Just move the comment to the start of the scope as module_globals check is related. If you want you can make a PR. Otherwise I’ll fix it myself soon.

  4. added a commit that references this issue on Oct 29, 2025
  5. added a commit that references this issue on Oct 29, 2025
  6. added a commit that references this issue on Oct 29, 2025
  7. StanFromIreland commented on Nov 16, 2025

    @StanFromIreland
    Member

    Triage: Can this be closed?

  8. gaogaotiantian commented on Nov 16, 2025

    @gaogaotiantian
    Member

    Thank you for checking it out, closed.

  9. added a commit that references this issue on Dec 6, 2025
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

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-refactorCode refactoring (with no changes in behavior)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions