cpython/Lib
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
..
__phello__
asyncio GH-90985: Revert "Deprecate passing a message into cancel()" (#97999) 2022-10-06 17:30:27 -07:00
collections
concurrent gh-95166: cancel map waited on future on timeout (GH-95169) 2022-07-28 11:20:10 +02:00
ctypes gh-92869: ctypes: Add c_time_t (#92870) 2022-07-03 11:58:02 -07:00
curses
dbm
distutils gh-95853: Multiple ops and debug for wasm_build.py (#96744) 2022-09-11 09:51:23 +02:00
email gh-95087: Fix IndexError in parsing invalid date in the email module (GH-95201) 2022-07-25 09:17:25 +03:00
encodings
ensurepip gh-95609: update bundled pip to 22.2.2 (gh-95610) 2022-08-03 20:26:51 +01:00
html gh-95813: Improve HTMLParser from the view of inheritance (#95874) 2022-08-18 13:16:33 +02:00
http Document that MozillaCookieJar works for curl's cookie files (#91852) 2022-10-03 15:07:54 -07:00
idlelib bpo-35675: IDLE - separate config_key window and frame (#11427) 2022-09-30 12:44:44 -04:00
importlib gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() (GH-97803) 2022-10-06 19:32:53 -07:00
json gh-96959: Update HTTP links which are redirected to HTTPS (GH-96961) 2022-09-24 14:38:53 +03:00
lib2to3 gh-54781: Move Lib/lib2to3/tests/ to Lib/test/test_lib2to3/ (#94049) 2022-06-21 15:21:22 +02:00
logging gh-90195: Unset logger disabled flag when configuring it. (GH-96530) 2022-09-03 13:38:38 +01:00
msilib
multiprocessing gh-97816: Remove unused variables in mutliprocessing.managers.Server (#97817) 2022-10-03 22:29:17 -07:00
pydoc_data
re gh-91404: Revert "bpo-23689: re module, fix memory leak when a match is terminated by a signal or allocation failure (GH-32283) (#93882) 2022-06-17 01:19:44 -07:00
site-packages
sqlite3 gh-95273: Improve sqlite3.complete_statement docs (#95840) 2022-08-12 01:05:12 +02:00
test gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() (GH-97803) 2022-10-06 19:32:53 -07:00
tkinter gh-73588: Fix generation of the default name of tkinter.Checkbutton. (GH-97547) 2022-09-27 14:05:05 +03:00
tomllib
turtledemo gh-91212: Fixed flickering when the tracer is turned off (#95129) 2022-09-28 23:40:51 -07:00
unittest gh-97837: Change deprecation warning message in unittest (#97838) 2022-10-04 17:29:18 -07:00
urllib gh-91539: improve performance of get_proxies_environment (#91566) 2022-10-05 10:57:52 -07:00
venv gh-96861: Check for unset sys.executable during venv creation. (GH-96887) 2022-09-17 12:58:31 +01:00
wsgiref gh-96959: Update HTTP links which are redirected to HTTPS (GH-96961) 2022-09-24 14:38:53 +03:00
xml gh-96175: add missing self._localName assignment in xml.dom.minidom.Attr (#96176) 2022-08-23 09:16:02 -07:00
xmlrpc
zoneinfo
__future__.py gh-93626: Set the release for __future__.annotations to None (GH-93628) 2022-07-05 10:46:39 +02:00
__hello__.py
_aix_support.py
_collections_abc.py
_compat_pickle.py
_compression.py
_markupbase.py
_osx_support.py
_py_abc.py
_pydecimal.py
_pyio.py gh-94169: Remove deprecated io.OpenWrapper (#94170) 2022-06-24 08:46:53 +02:00
_sitebuiltins.py
_strptime.py
_threading_local.py
_weakrefset.py
abc.py
aifc.py
antigravity.py
argparse.py argparse: Remove unused name variable when handling ArgumentTypeError (#96549) 2022-09-05 14:32:23 -07:00
ast.py gh-95588: Drop the safety claim from ast.literal_eval docs. (#95919) 2022-10-01 17:55:40 -07:00
asynchat.py
asyncore.py
base64.py gh-93096: Remove python -m base64 -t (gh-94230) 2022-07-02 15:53:43 +09:00
bdb.py bpo-30419: DOC: Update missing information in bdb docs (#1687) 2022-09-02 13:22:42 -04:00
bisect.py
bz2.py
calendar.py
cgi.py
cgitb.py
chunk.py
cmd.py
code.py
codecs.py gh-93096: Remove python -m codecs (gh-94233) 2022-07-02 14:45:31 +09:00
codeop.py gh-96052: codeop: fix handling compiler warnings in incomplete input (GH-96132) 2022-09-16 17:37:30 +03:00
colorsys.py
compileall.py
configparser.py gh-89336: Remove configparser APIs that were deprecated for 3.12 (#92503) 2022-06-21 14:31:25 -07:00
contextlib.py gh-96348: Deprecate the 3-arg signature of coroutine.throw and generator.throw (GH-96428) 2022-09-30 09:43:02 +01:00
contextvars.py
copy.py
copyreg.py
cProfile.py gh-65961: Do not rely solely on __cached__ (GH-97990) 2022-10-06 15:40:22 -07:00
crypt.py gh-95231: Disable md5 & crypt modules if FIPS is enabled (GH-94742) 2022-08-15 07:48:07 -07:00
csv.py gh-76728: Coerce DictReader and DictWriter fieldnames argument to a list (GH-32225) 2022-08-25 05:13:24 -05:00
dataclasses.py bpo-35540 dataclasses.asdict now supports defaultdict fields (gh-32056) 2022-10-06 17:11:59 -07:00
datetime.py gh-69142: add %:z strftime format code (gh-95983) 2022-08-28 14:27:42 -07:00
decimal.py
difflib.py
dis.py bpo-40222: Mark exception table function in the dis module as private (#95961) 2022-08-14 15:42:31 +01:00
doctest.py
enum.py gh-96865: [Enum] fix Flag to use CONFORM boundary (GH-97528) 2022-10-05 15:25:55 -07:00
filecmp.py gh-93991: Use boolean instead of 0/1 for condition check (GH-93992) 2022-06-19 07:12:59 -07:00
fileinput.py gh-93157: Fix fileinput didn't support errors in inplace mode (GH-95128) 2022-07-24 11:42:11 +09:00
fnmatch.py
fractions.py GH-96465: Cache hashes for Fraction instances (GH-96483) 2022-09-07 10:31:50 -05:00
ftplib.py
functools.py gh-89828: Do not relay the __class__ attribute in GenericAlias (#93754) 2022-06-18 11:34:57 +03:00
genericpath.py
getopt.py
getpass.py
gettext.py
glob.py
graphlib.py
gzip.py gh-94196: Remove gzip.GzipFile.filename attribute (#94197) 2022-06-24 11:59:32 +02:00
hashlib.py gh-94199: Remove hashlib.pbkdf2_hmac() Python implementation (GH-94200) 2022-06-28 11:51:13 +02:00
heapq.py
hmac.py
imaplib.py
imghdr.py
imp.py
inspect.py gh-65961: Do not rely solely on __cached__ (GH-97990) 2022-10-06 15:40:22 -07:00
io.py gh-94169: Remove deprecated io.OpenWrapper (#94170) 2022-06-24 08:46:53 +02:00
ipaddress.py
keyword.py
linecache.py gh-92336: linecache.getline should not raise exceptions on decoding errors (GH-94410) 2022-06-30 10:18:18 +01:00
locale.py gh-94226: Remove the locale.format() function (#94229) 2022-06-26 12:41:19 +02:00
lzma.py
mailbox.py
mailcap.py
mimetypes.py
modulefinder.py
netrc.py
nntplib.py
ntpath.py gh-89545: Updates platform module to use new internal _wmi module on Windows to directly query OS properties (GH-96289) 2022-09-07 21:09:20 +01:00
nturl2path.py
numbers.py gh-68163: Correct conversion of Rational instances to float (GH-25619) 2022-09-04 13:15:59 +01:00
opcode.py gh-93554: Conditional jump opcodes only jump forward (GH-96318) 2022-09-01 21:36:47 +01:00
operator.py
optparse.py
os.py
pathlib.py gh-94909: fix joining of absolute and relative Windows paths in pathlib (GH-95450) 2022-08-12 14:23:41 -07:00
pdb.py gh-95913: make the new internal classes pdb.ModuleTarget/ScriptTarget private (GH-96053) 2022-08-18 11:16:07 +01:00
pickle.py
pickletools.py
pipes.py
pkgutil.py
platform.py gh-89545: Updates platform module to use new internal _wmi module on Windows to directly query OS properties (GH-96289) 2022-09-07 21:09:20 +01:00
plistlib.py Fix typo in error message in plistlib (GH-97540) 2022-09-28 20:46:09 +03:00
poplib.py
posixpath.py gh-91838: Resolve more HTTP links which redirect to HTTPS (GH-95650) 2022-08-08 14:00:17 +03:00
pprint.py gh-92546: Move pprint benchmark into pyperformance (GH-94613) 2022-07-25 11:30:13 -07:00
profile.py gh-65961: Do not rely solely on __cached__ (GH-97990) 2022-10-06 15:40:22 -07:00
pstats.py Fix type annotation of pstats.FunctionProfile.ncalls (#96741) 2022-09-14 18:33:43 -07:00
pty.py
py_compile.py
pyclbr.py
pydoc.py gh-94318: Strip trailing spaces in pydoc text output (GH-94319) 2022-06-27 13:33:34 +03:00
queue.py
quopri.py
random.py bpo-37000: Remove obsolete comment in _randbelow_with_getrandbits (#95775) 2022-08-08 18:22:26 -05:00
reprlib.py gh-92734: Add indentation feature to reprlib.Repr (GH-92735) 2022-09-08 13:51:44 -05:00
rlcompleter.py
runpy.py
sched.py
secrets.py
selectors.py
shelve.py
shlex.py gh-94352: shlex.split() no longer accepts None (#94353) 2022-07-04 15:29:19 +02:00
shutil.py gh-74696: Pass root_dir to custom archivers which support it (GH-94251) 2022-10-05 12:48:59 +03:00
signal.py
site.py
smtplib.py
sndhdr.py
socket.py gh-96320: WASI socket fixes (#96388) 2022-08-30 06:36:11 +02:00
socketserver.py
sre_compile.py
sre_constants.py
sre_parse.py
ssl.py gh-94199: Remove the ssl.wrap_socket() function (#94203) 2022-07-08 15:20:15 +02:00
stat.py
statistics.py Improve accuracy for Spearman's rank correlation coefficient. (#96392) 2022-08-29 12:19:48 -05:00
string.py
stringprep.py
struct.py
subprocess.py gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with encoding or errors args (#97826) 2022-10-04 17:47:49 -07:00
sunau.py
symtable.py
sysconfig.py gh-92897: Ensure venv --copies respects source build property of the creating interpreter (GH-92899) 2022-07-05 16:08:20 +01:00
tabnanny.py gh-97639: Remove tokenize.NL check from tabnanny (#97640) 2022-10-03 17:41:29 -07:00
tarfile.py bpo-26253: Add compressionlevel to tarfile stream (GH-2962) 2022-06-25 11:43:54 +03:00
telnetlib.py
tempfile.py gh-58451: Add optional delete_on_close parameter to NamedTemporaryFile (GH-97015) 2022-10-04 23:37:33 +01:00
textwrap.py
this.py
threading.py gh-96349: fix minor performance regression initializing threading.Event (gh-96350) 2022-08-30 21:10:02 +09:00
timeit.py
token.py
tokenize.py
trace.py
traceback.py gh-97008: Add a Python implementation of AttributeError and NameError suggestions (#97022) 2022-10-04 15:31:16 -07:00
tracemalloc.py
tty.py
turtle.py gh-91212: Fixed flickering when the tracer is turned off (#95129) 2022-09-28 23:40:51 -07:00
types.py gh-89828: Do not relay the __class__ attribute in GenericAlias (#93754) 2022-06-18 11:34:57 +03:00
typing.py GH-96079 Fix missing field name for _AnnotatedAlias (#96080) 2022-08-31 16:02:24 -07:00
uu.py
uuid.py gh-95174: Handle missing waitpid and gethostbyname in WASI (GH-95181) 2022-07-24 08:04:06 +02:00
warnings.py
wave.py gh-77171: Fixes SubFormat check to compare the entire value. Add docs (GH-97509) 2022-09-23 16:08:21 +01:00
weakref.py
webbrowser.py
xdrlib.py
zipapp.py
zipfile.py gh-45108: Improve docstring and testing of ZipFile.testfile() (GH-96233) 2022-09-03 08:58:25 +03:00
zipimport.py gh-91181: drop support for bytes on sys.path (GH-31934) 2022-07-16 18:07:53 -07:00