[New-bugs-announce] [issue46911] Early tracing has lineno=None for modules

Ned Batchelder report at bugs.python.org
Thu Mar 3 08:36:59 EST 2022

New submission from Ned Batchelder <ned at nedbatchelder.com>:

Coverage.py has a trick to measure the early execution of stdlib modules. It has an "encodings.py" file that sets a trace function, then gets out of the way to let the real encodings.py execute.  In 3.11.0a5, that early trace function gets None values for the line numbers when modules were first executed.

To reproduce, create two files, encodings.py and prog.py:

--- 8< ------------------------------------------
# encodings.py
import sys

class FullCoverageTracer:
    def __init__(self):
        self.traces = []

    def fullcoverage_trace(self, *args):
        frame, event, arg = args
        if frame.f_lineno is None:
            self.traces.append((frame.f_code.co_name, frame.f_code.co_filename))
        return self.fullcoverage_trace


parentdir = max(filter(__file__.startswith, sys.path), key=len)
del sys.modules['encodings']
import encodings

--- 8< ------------------------------------------
# prog.py
import sys

trace = sys.gettrace()
print("None traces:", trace.__self__.traces)

Then run:

% PYTHONPATH=$(pwd) python3.10 prog.py
3.10.2 (main, Jan 15 2022, 05:51:59) [Clang 12.0.0 (clang-1200.0.32.29)]
None traces: []

% PYTHONPATH=$(pwd) python3.11 prog.py
3.11.0a5 (main, Feb  3 2022, 19:11:58) [Clang 12.0.0 (clang-1200.0.32.29)]
None traces: [('<module>', '/usr/local/pyenv/pyenv/versions/3.11.0a5/lib/python3.11/encodings/__init__.py'), ('<module>', '<frozen codecs>'), ('<module>', '/usr/local/pyenv/pyenv/versions/3.11.0a5/lib/python3.11/encodings/aliases.py'), ('<module>', '/usr/local/pyenv/pyenv/versions/3.11.0a5/lib/python3.11/encodings/utf_8.py'), ('<module>', '<frozen io>'), ('<module>', '<frozen abc>'), ('<module>', '<frozen site>'), ('<module>', '<frozen os>'), ('<module>', '<frozen stat>'), ('<module>', '<frozen _collections_abc>'), ('<module>', '<frozen posixpath>'), ('<module>', '<frozen genericpath>'), ('<module>', '<frozen _sitebuiltins>'), ('<module>', '<string>'), ('<module>', '/System/Volumes/Data/root/src/bugs/fullcov/prog.py')]

components: Interpreter Core
keywords: 3.11regression
messages: 414438
nosy: Mark.Shannon, nedbat
priority: normal
severity: normal
status: open
title: Early tracing has lineno=None for modules
versions: Python 3.11

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list