Remove deprecated py3compat module and inline its utilities#15292
Open
Carreau wants to merge 3 commits into
Open
Remove deprecated py3compat module and inline its utilities#15292Carreau wants to merge 3 commits into
Carreau wants to merge 3 commits into
Conversation
43d7ab7 to
da56255
Compare
Carreau
commented
Jul 7, 2026
Comment on lines
+204
to
208
| file_str = file or "" | ||
| if isinstance(file_str, bytes): | ||
| file_str = file_str.decode(util_path.fs_encoding, "replace") | ||
| name = util_path.compress_user(file_str) | ||
| if lineno is None: |
Member
Author
There was a problem hiding this comment.
@claude it look like file can never be bytes; remove the conditional; add an assertion at the start of the function.
- Delete IPython/utils/py3compat.py which was marked as deprecated - Replace py3compat.decode() with direct .decode() calls - Replace py3compat.encode() with direct .encode() calls - Replace py3compat.cast_unicode() with inline type checks and decoding - Replace py3compat.input() with builtin input() - Replace py3compat.safe_unicode() with try/except str() fallback - Replace py3compat.execfile() with inline exec() or local helpers - Replace py3compat.PYPY with platform.python_implementation() == 'PyPy' - Import DEFAULT_ENCODING directly where needed for encoding operations Files updated: - IPython/utils/_process_win32.py - IPython/utils/_process_common.py - IPython/lib/editorhooks.py - IPython/lib/demo.py - IPython/lib/clipboard.py - IPython/lib/pretty.py - IPython/core/page.py - IPython/core/tbtools.py - IPython/core/interactiveshell.py - IPython/terminal/magics.py - IPython/testing/tools.py - tests/test_debugger.py Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NyXpP8SsNDxTCEKnZ5MpF8
The py3compat removal flattened safe_execfile's exec() call, dropping the intermediate stack frame that tracebacks (and the doctest_tb_plain /doctest_tb_sysexit tests) expect to see as "in execfile", and which tb_offset=2 relies on to skip the right number of frames.
Every call site passes a str filename (co_filename or a traceback record's filename), matching the str | None type annotation, so the bytes-decoding branch was dead code left over from cast_unicode().
ae3c5df to
10101d1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR removes the deprecated
IPython/utils/py3compat.pymodule and inlines its utility functions throughout the codebase. The py3compat module was primarily designed for Python 2/3 compatibility, which is no longer needed as IPython now requires Python 3.Summary
The
py3compatmodule contained several compatibility utilities that are no longer necessary in a Python 3-only codebase. Rather than maintaining this deprecated module, its functionality has been directly integrated into the modules that use it.Key Changes
IPython/utils/py3compat.pyentirelysafe_unicode()logic inIPython/core/interactiveshell.py- replaced with direct try/except handling ofstr(),repr(), and fallback stringexecfile()logic inIPython/core/interactiveshell.pyandtests/test_debugger.py- replaced with direct file reading andexec()callscast_unicode()calls inIPython/lib/clipboard.pyandIPython/core/tbtools.pywith inline conditional checks and.decode()callsdecode()calls inIPython/lib/clipboard.py,IPython/testing/tools.py, andIPython/utils/_process_common.pywith direct.decode(DEFAULT_ENCODING, "replace")callsencode()calls inIPython/testing/tools.pywith direct.encode(DEFAULT_ENCODING, "replace")callsinput()wrapper inIPython/lib/demo.py,IPython/core/page.py,IPython/lib/editorhooks.py, andIPython/terminal/magics.pywith builtininput()PYPYconstant inIPython/lib/pretty.pywith inlineplatform.python_implementation() == "PyPy"checkpy3compatreferences and importDEFAULT_ENCODINGfromIPython.utils.encodingwhere neededImplementation Details
All inlined code maintains the same error handling and fallback behavior as the original utility functions. The
DEFAULT_ENCODINGconstant is imported fromIPython.utils.encodingwhere needed for encoding/decoding operations.https://claude.ai/code/session_01NyXpP8SsNDxTCEKnZ5MpF8