Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() #97803

Merged
merged 17 commits into from
Oct 7, 2022

Commits on Oct 3, 2022

  1. PoC for checking both __loader__ and __spec__.loader

    In _warnings.c, in the C equivalent of warnings.warn_explicit(), if the module
    globals are given (and not None), the warning will attempt to get the source
    line for the issued warning.  To do this, it needs the module's loader.
    
    Previously, it would only look up `__loader__` in the module globals.  In
    python#86298 we want to defer to the
    `__spec__.loader` if available.
    
    The first step on this journey is to check that `loader` == `__spec__.loader`
    and issue another warning if it is not.  This commit does that.
    
    Since this is a PoC, only manual testing for now.
    
    ```python
    import warnings
    
    import bar
    
    warnings.warn_explicit(
        'warning!',
        RuntimeWarning,
        'bar.py', 2,
        module='bar knee',
        module_globals=bar.__dict__,
        )
    ```
    
    ```python
    import sys
    import os
    import pathlib
    
    ```
    
    Then running this: `./python.exe -Wdefault /tmp/foo.py`
    
    Produces:
    
    ```
    bar.py:2: RuntimeWarning: warning!
      import os
    ```
    
    Uncomment the `__loader__ = ` line in `bar.py` and try it again:
    
    ```
    sys:1: ImportWarning: Module bar; __loader__ != __spec__.loader (<_frozen_importlib_external.SourceFileLoader object at 0x109f7dfa0> != PosixPath('.'))
    bar.py:2: RuntimeWarning: warning!
      import os
    ```
    warsaw committed Oct 3, 2022
    Configuration menu
    Copy the full SHA
    37b581a View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    049910b View commit details
    Browse the repository at this point in the history

Commits on Oct 4, 2022

  1. Configuration menu
    Copy the full SHA
    317e253 View commit details
    Browse the repository at this point in the history
  2. Add news

    warsaw committed Oct 4, 2022
    Configuration menu
    Copy the full SHA
    428d863 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    06d686d View commit details
    Browse the repository at this point in the history
  4. Update some documentation

    warsaw committed Oct 4, 2022
    Configuration menu
    Copy the full SHA
    fc50fe1 View commit details
    Browse the repository at this point in the history

Commits on Oct 6, 2022

  1. Use a helper function

    warsaw committed Oct 6, 2022
    Configuration menu
    Copy the full SHA
    1066b9f View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    4602366 View commit details
    Browse the repository at this point in the history
  3. Pass warnings tests

    Note however that the semantics of the helper has changed, and can't be full
    on loader blessing... yet.
    warsaw committed Oct 6, 2022
    Configuration menu
    Copy the full SHA
    831f832 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    37254cf View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    fe9d62e View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    3bcdba9 View commit details
    Browse the repository at this point in the history

Commits on Oct 7, 2022

  1. Configuration menu
    Copy the full SHA
    f5cef9c View commit details
    Browse the repository at this point in the history
  2. Update Lib/importlib/_bootstrap_external.py

    Co-authored-by: Brett Cannon <[email protected]>
    warsaw and brettcannon authored Oct 7, 2022
    Configuration menu
    Copy the full SHA
    5600c9c View commit details
    Browse the repository at this point in the history
  3. Update Lib/test/test_importlib/import_/test_helpers.py

    Co-authored-by: Brett Cannon <[email protected]>
    warsaw and brettcannon authored Oct 7, 2022
    Configuration menu
    Copy the full SHA
    8631e53 View commit details
    Browse the repository at this point in the history
  4. Update Lib/test/test_importlib/import_/test_helpers.py

    Co-authored-by: Brett Cannon <[email protected]>
    warsaw and brettcannon authored Oct 7, 2022
    Configuration menu
    Copy the full SHA
    b1b0d86 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    823400c View commit details
    Browse the repository at this point in the history