From a0b698209208ebc484744cf7d16493acc51d5841 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 23 Jan 2021 15:21:53 +0000 Subject: [PATCH] fix traceback chdir issue --- CHANGELOG.md | 1 + rich/__init__.py | 3 +++ rich/_timer.py | 4 +++- rich/traceback.py | 11 +++++++---- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2ec18c1..78100e58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix double line tree guides on Windows - Fixed Tracebacks ignoring initial blank lines +- Partial fix for tracebacks not finding source after chdir ### Added diff --git a/rich/__init__.py b/rich/__init__.py index 98e3b3d5..b0e4c8d9 100644 --- a/rich/__init__.py +++ b/rich/__init__.py @@ -1,5 +1,6 @@ """Rich text and beautiful formatting in the terminal.""" +import os from typing import Any, IO, Optional, TYPE_CHECKING __all__ = ["get_console", "reconfigure", "print", "inspect"] @@ -10,6 +11,8 @@ if TYPE_CHECKING: # Global console used by alternative print _console: Optional["Console"] = None +_IMPORT_CWD = os.path.abspath(os.getcwd()) + def get_console() -> "Console": """Get a global :class:`~rich.console.Console` instance. This function is used when Rich requires a Console, diff --git a/rich/_timer.py b/rich/_timer.py index 360afa7b..0710a02d 100644 --- a/rich/_timer.py +++ b/rich/_timer.py @@ -1,10 +1,12 @@ +# pragma: no cover + from time import time import contextlib @contextlib.contextmanager -def timer(subject: str = "time"): # pragma: no cover +def timer(subject: str = "time"): """print the elapsed time. (only used in debugging)""" start = time() yield diff --git a/rich/traceback.py b/rich/traceback.py index 19adca09..efbd933a 100644 --- a/rich/traceback.py +++ b/rich/traceback.py @@ -1,10 +1,10 @@ from __future__ import absolute_import -import os.path +import os import platform import sys from dataclasses import dataclass, field -from textwrap import indent +import inspect from traceback import walk_tb from types import TracebackType from typing import Callable, Dict, Iterable, List, Optional, Type @@ -257,6 +257,8 @@ class Traceback: stacks: List[Stack] = [] is_cause = False + from rich import _IMPORT_CWD + while True: stack = Stack( exc_type=str(exc_type.__name__), @@ -279,9 +281,10 @@ class Traceback: for frame_summary, line_no in walk_tb(traceback): filename = frame_summary.f_code.co_filename if filename and not filename.startswith("<"): - filename = os.path.abspath(filename) if filename else "?" + if not os.path.isabs(filename): + filename = os.path.join(_IMPORT_CWD, filename) frame = Frame( - filename=filename, + filename=filename or "?", lineno=line_no, name=frame_summary.f_code.co_name, locals={