From 70ad3fc9726a0e667b1edc61984302cc6706263d Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Mon, 27 Apr 2026 15:59:22 +0200 Subject: [PATCH] Update _tokens_filename with the option to not compress user path. This can be desirable in some case, in particular in compex deployment where the filesystem of the server and the kernel don't match to be able to open the file path from the UI. This will later be extended with global IPython configuration options, maybe at runtime. See Quansight/deshaw#259 --- IPython/core/doctb.py | 7 ++++++- IPython/core/tbtools.py | 9 ++++++--- IPython/core/ultratb.py | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/IPython/core/doctb.py b/IPython/core/doctb.py index a4839a7a5f2..b56b19769f0 100644 --- a/IPython/core/doctb.py +++ b/IPython/core/doctb.py @@ -223,7 +223,12 @@ def format_record(self, frame_info: FrameInfo) -> str: assert frame_info._sd is not None result = theme_table[self._theme_name].format( - _tokens_filename(True, frame_info.filename, lineno=frame_info.lineno) + _tokens_filename( + True, + frame_info.filename, + lineno=frame_info.lineno, + compress_user=True, + ) ) result += ", " if call else "" result += f"{call}\n" diff --git a/IPython/core/tbtools.py b/IPython/core/tbtools.py index 32f7bc96911..cf4594057bc 100644 --- a/IPython/core/tbtools.py +++ b/IPython/core/tbtools.py @@ -171,6 +171,7 @@ def _tokens_filename( file: str | None, *, lineno: int | None = None, + compress_user: bool = True, ) -> TokenStream: """ Format filename lines with custom formatting from caching compiler or `File *.py` by default @@ -179,6 +180,8 @@ def _tokens_filename( ---------- em: wether bold or not file : str + compress_user : bool + When True (default), apply ``util_path.compress_user`` to the filename. """ Normal = Token.NormalEm if em else Token.Normal Filename = Token.FilenameEm if em else Token.Filename @@ -202,9 +205,9 @@ def _tokens_filename( (Filename, f", line {lineno}"), ] else: - name = util_path.compress_user( - py3compat.cast_unicode(file or "", util_path.fs_encoding) - ) + name = py3compat.cast_unicode(file or "", util_path.fs_encoding) + if compress_user: + name = util_path.compress_user(name) if lineno is None: return [ (Normal, "File "), diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 355ba8858c6..c9f0703f1b4 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -260,7 +260,7 @@ def _format_list(self, extracted_list: list[Any]) -> list[str]: item = theme_table[self._theme_name].format( [(Token.NormalEm if em else Token.Normal, " ")] - + _tokens_filename(em, filename, lineno=lineno) + + _tokens_filename(em, filename, lineno=lineno, compress_user=True) ) # This seem to be only in xmode plain (%run sinpleer), investigate why not share with verbose. @@ -332,6 +332,7 @@ def _format_exception_only( True, value.filename, lineno=(None if lineno == "unknown" else lineno), + compress_user=True, ) + [(Token, "\n")] ) @@ -615,7 +616,12 @@ def format_record(self, frame_info: FrameInfo) -> str: # fast fallback if file is too long assert frame_info.filename is not None level_tokens = ( - _tokens_filename(True, frame_info.filename, lineno=frame_info.lineno) + _tokens_filename( + True, + frame_info.filename, + lineno=frame_info.lineno, + compress_user=True, + ) + [ (Token, ", " if call else ""), (Token, call), @@ -658,7 +664,12 @@ def format_record(self, frame_info: FrameInfo) -> str: return theme_table[self._theme_name].format(level_tokens + tb_tokens) else: result = theme_table[self._theme_name].format( - _tokens_filename(True, frame_info.filename, lineno=frame_info.lineno) + _tokens_filename( + True, + frame_info.filename, + lineno=frame_info.lineno, + compress_user=True, + ) ) result += ", " if call else "" result += f"{call}\n"