From 7bbf5d6b69fb8ee577287242067d8c8b41000c29 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 23 Mar 2025 14:29:14 -0400 Subject: [PATCH 1/7] Support getting source file for frozen modules --- Lib/linecache.py | 21 +++++++++++++++++++-- Lib/test/test_linecache.py | 13 +++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index dc02de19eb62cb..e1c526530cf7e7 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -63,6 +63,16 @@ def _getlines_from_code(code): return [] +def _source_unavailable(filename): + """Return True if the source code is unavailable for such file name""" + return ( + not filename + or (filename.startswith('<') + and filename.endswith('>') + and not filename.startswith('')): + if _source_unavailable(filename): return [] - fullname = filename + if filename.startswith(' Date: Sun, 23 Mar 2025 18:39:11 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst diff --git a/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst b/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst new file mode 100644 index 00000000000000..767a708e901cae --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst @@ -0,0 +1 @@ +Support frozen modules for :func:`linecache.getlines()` From c101cf92092dc56e261eaff5126bcf1f8b4370d1 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 23 Mar 2025 14:59:53 -0400 Subject: [PATCH 3/7] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/linecache.py | 4 ++-- .../Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index e1c526530cf7e7..87d7d6fda657e4 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -64,7 +64,7 @@ def _getlines_from_code(code): def _source_unavailable(filename): - """Return True if the source code is unavailable for such file name""" + """Return True if the source code is unavailable for such file name.""" return ( not filename or (filename.startswith('<') @@ -134,7 +134,7 @@ def updatecache(filename, module_globals=None): if filename.startswith(' Date: Sun, 23 Mar 2025 15:04:34 -0400 Subject: [PATCH 4/7] Update 2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst --- .../next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst b/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst index 9915cc87987e1a..6287e996ae1b01 100644 --- a/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst +++ b/Misc/NEWS.d/next/Library/2025-03-23-18-39-07.gh-issue-60115.AWdcmq.rst @@ -1 +1 @@ -Support frozen modules for :func:`linecache.getlines`. +Support frozen modules for :func:`linecache.getline`. From 381adfb53bf9fc7c30e232f8008e907af84d0426 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Wed, 2 Apr 2025 18:18:01 -0400 Subject: [PATCH 5/7] Add whatsnew entry --- Doc/whatsnew/3.14.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 767cf9a1f08dc2..083664ded9c692 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -674,6 +674,13 @@ json (Contributed by Trey Hunner in :gh:`122873`.) +linecache +--------- + +* :func:`linecache.getline` can retrieve source code for frozen modules. + (Contributed by Tian Gao in :gh:`131638`.) + + mimetypes --------- From 188329c961eabdcae73940eb8075484eaecde2ba Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Wed, 2 Apr 2025 18:42:16 -0400 Subject: [PATCH 6/7] Update docs --- Doc/library/linecache.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/library/linecache.rst b/Doc/library/linecache.rst index 88c6079a05b7fa..7018d99cfffb2b 100644 --- a/Doc/library/linecache.rst +++ b/Doc/library/linecache.rst @@ -30,6 +30,10 @@ The :mod:`linecache` module defines the following functions: .. index:: triple: module; search; path + If *filename* indicates a frozen module (starting with `` Date: Wed, 2 Apr 2025 18:46:41 -0400 Subject: [PATCH 7/7] Fix lint --- Doc/library/linecache.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/linecache.rst b/Doc/library/linecache.rst index 7018d99cfffb2b..e766a9280946d3 100644 --- a/Doc/library/linecache.rst +++ b/Doc/library/linecache.rst @@ -30,7 +30,7 @@ The :mod:`linecache` module defines the following functions: .. index:: triple: module; search; path - If *filename* indicates a frozen module (starting with ``