Skip to content

bpo-43381: Add frozen module co_lines() test #24712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Lib/test/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@
#
# 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):
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case/cause

# 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a small nitpick: cpython arg is not needed and other uses of @impl_detail in tests leave it out (with just one exception).

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()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a small test for frozen code co_lines().