cpython/Lib/test/test_importlib
Barry Warsaw 13d4489142
gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() (GH-97803)
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 https://github.com/python/cpython/issues/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
# /tmp/foo.py
import warnings

import bar

warnings.warn_explicit(
    'warning!',
    RuntimeWarning,
    'bar.py', 2,
    module='bar knee',
    module_globals=bar.__dict__,
    )
```

```python
# /tmp/bar.py
import sys
import os
import pathlib

# __loader__ = pathlib.Path()
```

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
```

Automerge-Triggered-By: GH:warsaw
2022-10-06 19:32:53 -07:00
..
builtin bpo-46425: fix direct invocation of test_importlib (GH-30682) 2022-01-22 18:05:05 +02:00
data bpo-44893: Implement EntryPoint as simple class with attributes. (GH-30150) 2021-12-16 15:49:42 -05:00
extension gh-94217: Skip import tests when _testcapi is a builtin (GH-94218) 2022-06-24 11:28:04 -07:00
frozen gh-97850: Remove all known instances of module_repr() (#97876) 2022-10-05 11:42:26 -07:00
import_ gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() (GH-97803) 2022-10-06 19:32:53 -07:00
namespace_pkgs
partial
resources gh-93963: Remove usage of deprecated interface from importlib.abc. (#95217) 2022-07-27 04:54:31 -04:00
source gh-94619: Remove long deprecated methods module_repr() and load_module() (#94624) 2022-08-04 17:24:26 -07:00
__init__.py
__main__.py
abc.py
fixtures.py gh-93259: Validate arg to `Distribution.from_name`. (GH-94270) 2022-06-25 21:04:28 -04:00
stubs.py
test_abc.py gh-97850: Remove deprecated functions from importlib.utils (#97898) 2022-10-06 17:57:10 -07:00
test_api.py gh-84623: Remove unused imports in tests (#93772) 2022-06-13 16:56:03 +02:00
test_lazy.py bpo-46425: fix direct invocation of test_importlib (GH-30682) 2022-01-22 18:05:05 +02:00
test_locks.py bpo-40280: Detect missing threading on WASM platforms (GH-32352) 2022-04-07 09:22:47 +02:00
test_main.py gh-97781: Apply changes from importlib_metadata 5. (GH-97785) 2022-10-06 15:25:24 -04:00
test_metadata_api.py gh-97781: Apply changes from importlib_metadata 5. (GH-97785) 2022-10-06 15:25:24 -04:00
test_namespace_pkgs.py gh-94619: Remove long deprecated methods module_repr() and load_module() (#94624) 2022-08-04 17:24:26 -07:00
test_pkg_import.py
test_spec.py gh-97850: Remove deprecated functions from importlib.utils (#97898) 2022-10-06 17:57:10 -07:00
test_threaded_import.py test_threaded_import: Fix unittest.main spelling (GH-93114) 2022-05-25 10:59:32 +03:00
test_util.py gh-97850: Remove deprecated functions from importlib.utils (#97898) 2022-10-06 17:57:10 -07:00
test_windows.py bpo-46616: Ensures test_importlib.test_windows cleans up registry keys after completion (GH-31086) 2022-02-02 19:54:27 +00:00
test_zip.py bpo-46425: Partially revert "bpo-46425: fix direct invocation of test_importlib (GH-30682)" (GH-30799) 2022-01-22 21:38:26 -05:00
threaded_import_hangers.py
util.py