From 45421095bcee7b61134bf657682f8390e7e5305b Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Tue, 2 Mar 2021 16:24:11 -0800 Subject: [PATCH 1/2] Add a small test for frozen code co_lines(). --- Lib/test/test_frozen.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_frozen.py b/Lib/test/test_frozen.py index 142f17d518e783..993e3ebe974372 100644 --- a/Lib/test/test_frozen.py +++ b/Lib/test/test_frozen.py @@ -7,12 +7,10 @@ # # The test_importlib also tests this module but because those tests # are much more complicated, it might be unclear why they are failing. -# Invalid marshalled data in frozen.c could case the interpreter to -# crash when __hello__ is imported. import sys import unittest -from test.support import captured_stdout +from test.support import captured_stdout, impl_detail class TestFrozen(unittest.TestCase): @@ -20,10 +18,25 @@ def test_frozen(self): name = '__hello__' if name in sys.modules: del sys.modules[name] + # Invalid marshalled data in frozen.c could case the interpreter to + # crash when __hello__ is imported. Ensure we can import the module + # and that it generates the correct output. with captured_stdout() as out: import __hello__ self.assertEqual(out.getvalue(), 'Hello world!\n') + @impl_detail('code object line table', cpython=True) + def test_frozen_linetab(self): + with captured_stdout(): + import __hello__ + # get the code object for the module + co = __hello__.__spec__.loader.get_code('__hello__') + # verify that line number table is as expected. This is intended to + # catch bugs like bpo-43372. The __hello__ module would import + # successfully but the frozen code was out-of-date and co_lines() + # would cause a crash. + self.assertEqual(list(co.co_lines()), [(0, 4, 1), (4, 16, 2)]) + if __name__ == '__main__': unittest.main() From f3c31a55ce9ee32ac0c4c42419c8ca6df1134443 Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Tue, 2 Mar 2021 16:56:57 -0800 Subject: [PATCH 2/2] Add NEWS file. --- Misc/NEWS.d/next/Tests/2021-03-02-16-55-37.bpo-43381.45RrT9.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2021-03-02-16-55-37.bpo-43381.45RrT9.rst diff --git a/Misc/NEWS.d/next/Tests/2021-03-02-16-55-37.bpo-43381.45RrT9.rst b/Misc/NEWS.d/next/Tests/2021-03-02-16-55-37.bpo-43381.45RrT9.rst new file mode 100644 index 00000000000000..e5f9af27f33bc6 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2021-03-02-16-55-37.bpo-43381.45RrT9.rst @@ -0,0 +1 @@ +Add a small test for frozen code co_lines().