From f1e1189d7a26801235331422a6129cb47f5ad78e Mon Sep 17 00:00:00 2001 From: lhish Date: Mon, 22 Jul 2024 21:47:00 +0800 Subject: [PATCH 01/14] Fixed traceback leaks global code when exec --- Lib/linecache.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/linecache.py b/Lib/linecache.py index 3462f1c451ba29..187f72a588c73d 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -180,6 +180,9 @@ def lazycache(filename, module_globals): loader = getattr(spec, 'loader', None) if loader is None: loader = module_globals.get('__loader__') + wrong_name = hasattr(loader, "get_filename") and loader.get_filename() != filename + if wrong_name: + return False get_source = getattr(loader, 'get_source', None) if name and get_source: From f510454495b20c3a7ab5985012c2996ceec3e606 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:58:17 +0000 Subject: [PATCH 02/14] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst b/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst new file mode 100644 index 00000000000000..1e20a8a6b3e922 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst @@ -0,0 +1 @@ +Fixed traceback leaks global code when exec. From b40bc2876140ef9f3b1126ffbd991e0610c1ea78 Mon Sep 17 00:00:00 2001 From: lhish Date: Tue, 23 Jul 2024 00:04:24 +0800 Subject: [PATCH 03/14] adapt for zipimporter --- Lib/linecache.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 187f72a588c73d..09558ed8b8753a 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -180,9 +180,12 @@ def lazycache(filename, module_globals): loader = getattr(spec, 'loader', None) if loader is None: loader = module_globals.get('__loader__') - wrong_name = hasattr(loader, "get_filename") and loader.get_filename() != filename - if wrong_name: - return False + try: + wrong_name = hasattr(loader, "get_filename") and loader.get_filename() != filename + if wrong_name: + return False + except: + pass get_source = getattr(loader, 'get_source', None) if name and get_source: From a9f8458f0bce4927a7690a509032ca5cb4620e3d Mon Sep 17 00:00:00 2001 From: lhish Date: Tue, 23 Jul 2024 01:30:20 +0800 Subject: [PATCH 04/14] Fixed missing file's lazycache --- Lib/linecache.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 09558ed8b8753a..793e74ff15de6b 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -183,7 +183,10 @@ def lazycache(filename, module_globals): try: wrong_name = hasattr(loader, "get_filename") and loader.get_filename() != filename if wrong_name: - return False + def get_lines(name=name, *args, **kwargs): + return "" + cache[filename] = (get_lines,) + return True except: pass get_source = getattr(loader, 'get_source', None) From 0fab6296fc87349097e7be9513a4604733f35dfa Mon Sep 17 00:00:00 2001 From: lhish Date: Tue, 23 Jul 2024 01:30:48 +0800 Subject: [PATCH 05/14] Update trace_back test --- Lib/test/test_traceback.py | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 1895c88d23b70d..0fc13969dc7524 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4,6 +4,7 @@ from io import StringIO import linecache import sys +import os import types import inspect import builtins @@ -36,7 +37,7 @@ test_code.co_positions = lambda _: iter([(6, 6, 0, 0)]) test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals']) test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next', 'tb_lasti']) - +test_path = os.path.abspath(__file__) LEVENSHTEIN_DATA_FILE = Path(__file__).parent / 'levenshtein_examples.json' @@ -3100,13 +3101,13 @@ class TestFrame(unittest.TestCase): def test_basics(self): linecache.clearcache() - linecache.lazycache("f", globals()) - f = traceback.FrameSummary("f", 1, "dummy") + linecache.lazycache(test_path, globals()) + f = traceback.FrameSummary(test_path, 1, "dummy") self.assertEqual(f, - ("f", 1, "dummy", '"""Test cases for traceback module"""')) + (test_path, 1, "dummy", '"""Test cases for traceback module"""')) self.assertEqual(tuple(f), - ("f", 1, "dummy", '"""Test cases for traceback module"""')) - self.assertEqual(f, traceback.FrameSummary("f", 1, "dummy")) + (test_path, 1, "dummy", '"""Test cases for traceback module"""')) + self.assertEqual(f, traceback.FrameSummary(test_path, 1, "dummy")) self.assertEqual(f, tuple(f)) # Since tuple.__eq__ doesn't support FrameSummary, the equality # operator fallbacks to FrameSummary.__eq__. @@ -3117,9 +3118,9 @@ def test_basics(self): def test_lazy_lines(self): linecache.clearcache() - f = traceback.FrameSummary("f", 1, "dummy", lookup_line=False) + f = traceback.FrameSummary(test_path, 1, "dummy", lookup_line=False) self.assertEqual(None, f._lines) - linecache.lazycache("f", globals()) + linecache.lazycache(test_path, globals()) self.assertEqual( '"""Test cases for traceback module"""', f.line) @@ -3165,8 +3166,8 @@ def test_extract_stack_limit(self): def test_extract_stack_lookup_lines(self): linecache.clearcache() - linecache.updatecache('/foo.py', globals()) - c = test_code('/foo.py', 'method') + linecache.updatecache(test_path, globals()) + c = test_code(test_path, 'method') f = test_frame(c, None, None) s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=True) linecache.clearcache() @@ -3174,11 +3175,11 @@ def test_extract_stack_lookup_lines(self): def test_extract_stackup_deferred_lookup_lines(self): linecache.clearcache() - c = test_code('/foo.py', 'method') + c = test_code(test_path, 'method') f = test_frame(c, None, None) s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=False) self.assertEqual({}, linecache.cache) - linecache.updatecache('/foo.py', globals()) + linecache.updatecache(test_path, globals()) self.assertEqual(s[0].line, "import sys") def test_from_list(self): @@ -3204,15 +3205,15 @@ def test_format_smoke(self): s.format()) def test_locals(self): - linecache.updatecache('/foo.py', globals()) - c = test_code('/foo.py', 'method') + linecache.updatecache(test_path, globals()) + c = test_code(test_path, 'method') f = test_frame(c, globals(), {'something': 1}) s = traceback.StackSummary.extract(iter([(f, 6)]), capture_locals=True) self.assertEqual(s[0].locals, {'something': '1'}) def test_no_locals(self): - linecache.updatecache('/foo.py', globals()) - c = test_code('/foo.py', 'method') + linecache.updatecache(test_path, globals()) + c = test_code(test_path, 'method') f = test_frame(c, globals(), {'something': 1}) s = traceback.StackSummary.extract(iter([(f, 6)])) self.assertEqual(s[0].locals, None) @@ -3570,18 +3571,18 @@ def recurse(n): def test_lookup_lines(self): linecache.clearcache() e = Exception("uh oh") - c = test_code('/foo.py', 'method') + c = test_code(test_path, 'method') f = test_frame(c, None, None) tb = test_tb(f, 6, None, 0) exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False) self.assertEqual(linecache.cache, {}) - linecache.updatecache('/foo.py', globals()) + linecache.updatecache(test_path, globals()) self.assertEqual(exc.stack[0].line, "import sys") def test_locals(self): - linecache.updatecache('/foo.py', globals()) + linecache.updatecache(test_path, globals()) e = Exception("uh oh") - c = test_code('/foo.py', 'method') + c = test_code(test_path, 'method') f = test_frame(c, globals(), {'something': 1, 'other': 'string', 'unrepresentable': Unrepresentable()}) tb = test_tb(f, 6, None, 0) exc = traceback.TracebackException( @@ -3591,9 +3592,9 @@ def test_locals(self): {'something': '1', 'other': "'string'", 'unrepresentable': ''}) def test_no_locals(self): - linecache.updatecache('/foo.py', globals()) + linecache.updatecache(test_path, globals()) e = Exception("uh oh") - c = test_code('/foo.py', 'method') + c = test_code(test_path, 'method') f = test_frame(c, globals(), {'something': 1}) tb = test_tb(f, 6, None, 0) exc = traceback.TracebackException(Exception, e, tb) From 44715f252dbd006c690046422eb52fe8fc8d100e Mon Sep 17 00:00:00 2001 From: lhish Date: Sun, 25 Aug 2024 01:35:26 +0800 Subject: [PATCH 06/14] Update the method to check the existence of the file --- Lib/linecache.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index de4ca4b6989b88..a1698f8efcff77 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -182,15 +182,12 @@ def lazycache(filename, module_globals): loader = getattr(spec, 'loader', None) if loader is None: loader = module_globals.get('__loader__') - try: - wrong_name = hasattr(loader, "get_filename") and loader.get_filename() != filename - if wrong_name: - def get_lines(name=name, *args, **kwargs): - return "" - cache[filename] = (get_lines,) - return True - except: - pass + if not ((mod_file := module_globals.get('__file__', None)) and mod_file.endswith(filename)): + def get_lines(name=name, *args, **kwargs): + return "" + + cache[filename] = (get_lines,) + return True get_source = getattr(loader, 'get_source', None) if name and get_source: From 78d1a147a5a150ad65dbde93a336edbd6a49f457 Mon Sep 17 00:00:00 2001 From: lhish <59965910+lhish@users.noreply.github.com> Date: Wed, 28 Aug 2024 00:44:13 +0800 Subject: [PATCH 07/14] change description 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> --- .../2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst b/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst index 1e20a8a6b3e922..798777d33851ef 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-07-22-13-58-16.gh-issue-122071.c5EQZL.rst @@ -1 +1 @@ -Fixed traceback leaks global code when exec. +Fix global code leaks when reporting tracebacks in the REPL and :func:`exec`. From 363758a8347e69bf37f4f8c5f90a40a72f0fe9ff Mon Sep 17 00:00:00 2001 From: lhish <59965910+lhish@users.noreply.github.com> Date: Wed, 28 Aug 2024 00:45:12 +0800 Subject: [PATCH 08/14] 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 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index a1698f8efcff77..7b4c0736e89f1b 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -182,10 +182,10 @@ def lazycache(filename, module_globals): loader = getattr(spec, 'loader', None) if loader is None: loader = module_globals.get('__loader__') - if not ((mod_file := module_globals.get('__file__', None)) and mod_file.endswith(filename)): + mod_file = module_globals.get('__file__') + if not mod_file or not mod_file.endswith(filename): def get_lines(name=name, *args, **kwargs): return "" - cache[filename] = (get_lines,) return True get_source = getattr(loader, 'get_source', None) From fa2d76ba175ba6fbaa54d38b62ad44e5d9912dc3 Mon Sep 17 00:00:00 2001 From: lhish Date: Wed, 28 Aug 2024 00:51:07 +0800 Subject: [PATCH 09/14] Revert "Update trace_back test" This reverts commit 0fab6296fc87349097e7be9513a4604733f35dfa. --- Lib/test/test_traceback.py | 45 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index bb47a62bcc1cbc..b568221212d71f 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4,7 +4,6 @@ from io import StringIO import linecache import sys -import os import types import inspect import builtins @@ -37,7 +36,7 @@ test_code.co_positions = lambda _: iter([(6, 6, 0, 0)]) test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals']) test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next', 'tb_lasti']) -test_path = os.path.abspath(__file__) + LEVENSHTEIN_DATA_FILE = Path(__file__).parent / 'levenshtein_examples.json' @@ -3133,13 +3132,13 @@ class TestFrame(unittest.TestCase): def test_basics(self): linecache.clearcache() - linecache.lazycache(test_path, globals()) - f = traceback.FrameSummary(test_path, 1, "dummy") + linecache.lazycache("f", globals()) + f = traceback.FrameSummary("f", 1, "dummy") self.assertEqual(f, - (test_path, 1, "dummy", '"""Test cases for traceback module"""')) + ("f", 1, "dummy", '"""Test cases for traceback module"""')) self.assertEqual(tuple(f), - (test_path, 1, "dummy", '"""Test cases for traceback module"""')) - self.assertEqual(f, traceback.FrameSummary(test_path, 1, "dummy")) + ("f", 1, "dummy", '"""Test cases for traceback module"""')) + self.assertEqual(f, traceback.FrameSummary("f", 1, "dummy")) self.assertEqual(f, tuple(f)) # Since tuple.__eq__ doesn't support FrameSummary, the equality # operator fallbacks to FrameSummary.__eq__. @@ -3150,9 +3149,9 @@ def test_basics(self): def test_lazy_lines(self): linecache.clearcache() - f = traceback.FrameSummary(test_path, 1, "dummy", lookup_line=False) + f = traceback.FrameSummary("f", 1, "dummy", lookup_line=False) self.assertEqual(None, f._lines) - linecache.lazycache(test_path, globals()) + linecache.lazycache("f", globals()) self.assertEqual( '"""Test cases for traceback module"""', f.line) @@ -3198,8 +3197,8 @@ def test_extract_stack_limit(self): def test_extract_stack_lookup_lines(self): linecache.clearcache() - linecache.updatecache(test_path, globals()) - c = test_code(test_path, 'method') + linecache.updatecache('/foo.py', globals()) + c = test_code('/foo.py', 'method') f = test_frame(c, None, None) s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=True) linecache.clearcache() @@ -3207,11 +3206,11 @@ def test_extract_stack_lookup_lines(self): def test_extract_stackup_deferred_lookup_lines(self): linecache.clearcache() - c = test_code(test_path, 'method') + c = test_code('/foo.py', 'method') f = test_frame(c, None, None) s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=False) self.assertEqual({}, linecache.cache) - linecache.updatecache(test_path, globals()) + linecache.updatecache('/foo.py', globals()) self.assertEqual(s[0].line, "import sys") def test_from_list(self): @@ -3237,15 +3236,15 @@ def test_format_smoke(self): s.format()) def test_locals(self): - linecache.updatecache(test_path, globals()) - c = test_code(test_path, 'method') + linecache.updatecache('/foo.py', globals()) + c = test_code('/foo.py', 'method') f = test_frame(c, globals(), {'something': 1}) s = traceback.StackSummary.extract(iter([(f, 6)]), capture_locals=True) self.assertEqual(s[0].locals, {'something': '1'}) def test_no_locals(self): - linecache.updatecache(test_path, globals()) - c = test_code(test_path, 'method') + linecache.updatecache('/foo.py', globals()) + c = test_code('/foo.py', 'method') f = test_frame(c, globals(), {'something': 1}) s = traceback.StackSummary.extract(iter([(f, 6)])) self.assertEqual(s[0].locals, None) @@ -3603,18 +3602,18 @@ def recurse(n): def test_lookup_lines(self): linecache.clearcache() e = Exception("uh oh") - c = test_code(test_path, 'method') + c = test_code('/foo.py', 'method') f = test_frame(c, None, None) tb = test_tb(f, 6, None, 0) exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False) self.assertEqual(linecache.cache, {}) - linecache.updatecache(test_path, globals()) + linecache.updatecache('/foo.py', globals()) self.assertEqual(exc.stack[0].line, "import sys") def test_locals(self): - linecache.updatecache(test_path, globals()) + linecache.updatecache('/foo.py', globals()) e = Exception("uh oh") - c = test_code(test_path, 'method') + c = test_code('/foo.py', 'method') f = test_frame(c, globals(), {'something': 1, 'other': 'string', 'unrepresentable': Unrepresentable()}) tb = test_tb(f, 6, None, 0) exc = traceback.TracebackException( @@ -3624,9 +3623,9 @@ def test_locals(self): {'something': '1', 'other': "'string'", 'unrepresentable': ''}) def test_no_locals(self): - linecache.updatecache(test_path, globals()) + linecache.updatecache('/foo.py', globals()) e = Exception("uh oh") - c = test_code(test_path, 'method') + c = test_code('/foo.py', 'method') f = test_frame(c, globals(), {'something': 1}) tb = test_tb(f, 6, None, 0) exc = traceback.TracebackException(Exception, e, tb) From 7f7308a76feebfb824b4a63f1c1a1eb9345bcbcb Mon Sep 17 00:00:00 2001 From: lhish Date: Wed, 28 Aug 2024 00:54:01 +0800 Subject: [PATCH 10/14] change the implementation of get_lines --- Lib/linecache.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 7b4c0736e89f1b..8ada9dadc1a240 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -177,6 +177,7 @@ def lazycache(filename, module_globals): return False # Try for a __loader__, if available if module_globals and '__name__' in module_globals: + get_lines = lambda *_, **__: "" spec = module_globals.get('__spec__') name = getattr(spec, 'name', None) or module_globals['__name__'] loader = getattr(spec, 'loader', None) @@ -184,8 +185,6 @@ def lazycache(filename, module_globals): loader = module_globals.get('__loader__') mod_file = module_globals.get('__file__') if not mod_file or not mod_file.endswith(filename): - def get_lines(name=name, *args, **kwargs): - return "" cache[filename] = (get_lines,) return True get_source = getattr(loader, 'get_source', None) From 250f02693b1d439ca578d6065fc9744113a60983 Mon Sep 17 00:00:00 2001 From: lhish Date: Wed, 28 Aug 2024 12:33:30 +0800 Subject: [PATCH 11/14] make restriction stronger --- Lib/linecache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 8ada9dadc1a240..168d75b9faccbc 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -1,10 +1,10 @@ +import importlib._bootstrap_external """Cache lines from Python source files. This is intended to read lines from modules imported -- hence if a filename is not found, it will look down the module search path for a file by that name. """ - __all__ = ["getline", "clearcache", "checkcache", "lazycache"] @@ -184,7 +184,7 @@ def lazycache(filename, module_globals): if loader is None: loader = module_globals.get('__loader__') mod_file = module_globals.get('__file__') - if not mod_file or not mod_file.endswith(filename): + if isinstance(loader, importlib._bootstrap_external.SourceFileLoader) and (not mod_file or (not mod_file.endswith(filename) and not mod_file.endswith('.pyc'))): cache[filename] = (get_lines,) return True get_source = getattr(loader, 'get_source', None) From e3796bb88f7c2f8d3e3ebb1f48f4c23c9c3e6d13 Mon Sep 17 00:00:00 2001 From: lhish Date: Sun, 6 Oct 2024 19:59:44 +0800 Subject: [PATCH 12/14] Apply suggestions from code review --- Lib/linecache.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 168d75b9faccbc..a376d940a07878 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -1,10 +1,10 @@ -import importlib._bootstrap_external """Cache lines from Python source files. This is intended to read lines from modules imported -- hence if a filename is not found, it will look down the module search path for a file by that name. """ + __all__ = ["getline", "clearcache", "checkcache", "lazycache"] @@ -184,6 +184,7 @@ def lazycache(filename, module_globals): if loader is None: loader = module_globals.get('__loader__') mod_file = module_globals.get('__file__') + import importlib._bootstrap_external if isinstance(loader, importlib._bootstrap_external.SourceFileLoader) and (not mod_file or (not mod_file.endswith(filename) and not mod_file.endswith('.pyc'))): cache[filename] = (get_lines,) return True From 3005636ace3adc891c4458768cbe1835a9b0d756 Mon Sep 17 00:00:00 2001 From: lhish Date: Tue, 8 Oct 2024 12:50:08 +0800 Subject: [PATCH 13/14] change return value from true to false --- Lib/linecache.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index a376d940a07878..34f56d10b1d234 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -177,7 +177,6 @@ def lazycache(filename, module_globals): return False # Try for a __loader__, if available if module_globals and '__name__' in module_globals: - get_lines = lambda *_, **__: "" spec = module_globals.get('__spec__') name = getattr(spec, 'name', None) or module_globals['__name__'] loader = getattr(spec, 'loader', None) @@ -186,8 +185,7 @@ def lazycache(filename, module_globals): mod_file = module_globals.get('__file__') import importlib._bootstrap_external if isinstance(loader, importlib._bootstrap_external.SourceFileLoader) and (not mod_file or (not mod_file.endswith(filename) and not mod_file.endswith('.pyc'))): - cache[filename] = (get_lines,) - return True + return False get_source = getattr(loader, 'get_source', None) if name and get_source: From 787ea13462cc82b169f35226e3c4107136a24e4f Mon Sep 17 00:00:00 2001 From: lhish Date: Tue, 8 Oct 2024 12:50:26 +0800 Subject: [PATCH 14/14] change tests to make it correct --- Lib/test/test_linecache.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py index 6f5955791407ea..4a38379a2f749a 100644 --- a/Lib/test/test_linecache.py +++ b/Lib/test/test_linecache.py @@ -206,8 +206,8 @@ def test_lazycache_smoke(self): lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() self.assertEqual( - True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) - self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) + False, linecache.lazycache(NONEXISTENT_FILENAME, globals())) + self.assertEqual(False, NONEXISTENT_FILENAME in linecache.cache) # Note here that we're looking up a nonexistent filename with no # globals: this would error if the lazy value wasn't resolved. self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME)) @@ -232,11 +232,11 @@ def test_lazycache_bad_filename(self): def test_lazycache_already_cached(self): linecache.clearcache() - lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) + lines = linecache.getlines(FILENAME, globals()) self.assertEqual( False, - linecache.lazycache(NONEXISTENT_FILENAME, globals())) - self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME])) + linecache.lazycache(FILENAME, globals())) + self.assertEqual(4, len(linecache.cache[FILENAME])) def test_memoryerror(self): lines = linecache.getlines(FILENAME)