GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `trace`: line tracing output lacks newlines if source lines are not available · Issue #103956 · python/cpython

trace: line tracing output lacks newlines if source lines are not available #103956

New issue
Copy link
New issue
Copy link
Closed
#103958
Closed
trace: line tracing output lacks newlines if source lines are not available#103956
#103958
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@chgnrdv
chgnrdv
opened on Apr 27, 2023
Issue body actions

trace doesn't print newline character when handling line event if it can't fetch source line that corresponds to a current frame, e. g. in case of frozen modules (or in any other case when frame.f_code.co_filename points to a non-existing file).

Example:

import numpy as np
x = np.array([1, 2, 3])
./python -m trace --trace example.py
--- modulename: example, funcname: <module>
example.py(2): import signal
 --- modulename: _bootstrap, funcname: _find_and_load
<frozen importlib._bootstrap>(1299): <frozen importlib._bootstrap>(1300): <frozen importlib._bootstrap>(1302):  --- modulename: _bootstrap, funcname: __init__
<frozen importlib._bootstrap>(325): <frozen importlib._bootstrap>(326):  --- modulename: _bootstrap, funcname: __enter__
<frozen importlib._bootstrap>(329):  --- modulename: _bootstrap, funcname: _get_module_lock
<frozen importlib._bootstrap>(344): <frozen importlib._bootstrap>(345): <frozen importlib._bootstrap>(346): <frozen importlib._bootstrap>(347): <frozen importlib._bootstrap>(348): <frozen importlib._bootstrap>(349): <frozen importlib._bootstrap>(351): <frozen importlib._bootstrap>(352): <frozen importlib._bootstrap>(355):  --- modulename: _bootstrap, funcname: __init__
<frozen importlib._bootstrap>(165): <frozen importlib._bootstrap>(166): <frozen importlib._bootstrap>(169): <frozen importlib._bootstrap>(173): <frozen importlib._bootstrap>(184): <frozen importlib._bootstrap>(198): <frozen importlib._bootstrap>(357): <frozen importlib._bootstrap>(368): <frozen importlib._bootstrap>(370): <frozen importlib._bootstrap>(372): <frozen importlib._bootstrap>(330):  --- modulename: _bootstrap, funcname: acquire
<frozen importlib._bootstrap>(222): <frozen importlib._bootstrap>(223):  --- modulename: _bootstrap, funcname: __init__
<frozen importlib._bootstrap>(71): <frozen importlib._bootstrap>(72):  --- modulename: _bootstrap, funcname: __enter__
<frozen importlib._bootstrap>(82): <frozen importlib._bootstrap>(83): <frozen importlib._bootstrap>(224): <frozen importlib._bootstrap>(228): <frozen importlib._bootstrap>(229): <frozen importlib._bootstrap>(236): <frozen importlib._bootstrap>(237): <frozen importlib._bootstrap>(238): <frozen importlib._bootstrap>(228): <frozen importlib._bootstrap>(223):  --- modulename: _bootstrap, funcname: __exit__
<frozen importlib._bootstrap>(87): <frozen importlib._bootstrap>(1303): <frozen importlib._bootstrap>(1304): <frozen importlib._bootstrap>(1305):  --- modulename: _bootstrap, funcname: _find_and_load_unlocked
<frozen importlib._bootstrap>(1250): <frozen importlib._bootstrap>(1251): <frozen importlib._bootstrap>(1252): <frozen importlib._bootstrap>(1253): <frozen importlib._bootstrap>(1267):  --- modulename: _bootstrap, funcname: _find_spec
<frozen importlib._bootstrap>(1185): <frozen importlib._bootstrap>(1186): <frozen importlib._bootstrap>(1191): <frozen importlib._bootstrap>(1197): <frozen importlib._bootstrap>(1198): <frozen importlib._bootstrap>(1199):  --- modulename: _bootstrap, funcname: __enter__
...
numeric.py(205):     a = empty(shape, dtype, order)
numeric.py(206):     multiarray.copyto(a, 1, casting='unsafe')
 --- modulename: overrides, funcname: copyto
<__array_function__ internals>(179): <__array_function__ internals>(180):  --- modulename: multiarray, funcname: copyto
multiarray.py(1127):     return (dst, src, where)
<__array_function__ internals>(200): <__array_function__ internals>(201): <__array_function__ internals>(200): numeric.py(207):     return a
__init__.py(348):             if not abs(x.dot(x) - float32(2.0)) < 1e-5:
__init__.py(359):     del _sanity_check
__init__.py(361):     def _mac_os_check():
...

It happens because Trace object's localtrace_trace and localtrace_trace_and_count methods expect the call to linecache.getline to always return source line with newline at the end of it.

cpython/Lib/trace.py

Lines 566 to 577 in bf0b8a9

def localtrace_trace(self, frame, why, arg):
if why == "line":
# record the file name and line number of every trace
filename = frame.f_code.co_filename
lineno = frame.f_lineno
if self.start_time:
print('%.2f' % (_time() - self.start_time), end=' ')
bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno,
linecache.getline(filename, lineno)), end='')
return self.localtrace

Confirmed on 3.11.1 and 3.12.0a7+(63842bd).
I'll do a PR with possible fix.

Linked PRs

  • gh-103956: fix trace output in case of missing source line #103958
  • [3.12] gh-103956: Fix trace output in case of missing source line (GH-103958) #118832
  • [3.11] gh-103956: Fix trace output in case of missing source line (GH-103958) #118833
  • [3.13] gh-103956: Fix trace output in case of missing source line (GH-103958) #118834

Activity

  1. chgnrdv
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 27, 2023
  2. iritkatriel
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 28, 2023
  3. serhiy-storchaka
    closed this as completedin #103958on May 9, 2024
  4. serhiy-storchaka
    added a commit that references this issue on May 9, 2024

    gh-103956: Fix `trace` output in case of missing source line (GH-103958)

    7c87ce7
  5. miss-islington
    added 3 commits that reference this issue on May 9, 2024

    pythongh-103956: Fix `trace` output in case of missing source line (p…

    83f2830

    pythongh-103956: Fix `trace` output in case of missing source line (p…

    b1b724a

    pythongh-103956: Fix `trace` output in case of missing source line (p…

    46dd2a7
  6. serhiy-storchaka
    added 2 commits that reference this issue on May 9, 2024

    [3.12] gh-103956: Fix `trace` output in case of missing source line (G…

    0d62676

    [3.13] gh-103956: Fix `trace` output in case of missing source line (G…

    c4b853f
  7. SonicField
    added a commit that references this issue on May 9, 2024

    pythongh-103956: Fix `trace` output in case of missing source line (p…

    e570920
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

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.