Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Improve magic number test execution and message
Improve the execution of the magic number test by
using skipUnless for alpha and beta releases, and
directly inheriting from unittest.TestCase rather than
using the machinery for the other tests. Also improve
the error message to explain the reason for caution in
changing the magic number.

BPO: 29514
  • Loading branch information
appeltel committed Mar 12, 2017
commit 1e32a1be4a1705e34011770026cb64ada2d340b5
20 changes: 10 additions & 10 deletions Lib/test/test_importlib/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
machinery = util.import_importlib('importlib.machinery')
importlib_util = util.import_importlib('importlib.util')

import importlib.util
import os
import pathlib
import string
Expand Down Expand Up @@ -757,10 +758,14 @@ def test_source_from_cache_path_like_arg(self):
) = util.test_both(PEP3147Tests, util=importlib_util)


class MagicNumberTests:
class MagicNumberTests(unittest.TestCase):
"""
Test release compatibility issues relating to importlib
"""
@unittest.skipUnless(
sys.version_info.releaselevel in ('final', 'release'),
'only applies to candidate or final python release levels'
)
def test_magic_number(self):
"""
Each python minor release should generally have a MAGIC_NUMBER

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it would be better to write this as a comment rather than a docstring. If you want, add a one-line docstring, but most test methods don't have docstrings.

Expand All @@ -778,13 +783,13 @@ def test_magic_number(self):
in advance. Such exceptional releases will then require an
adjustment to this test case.
"""
if sys.version_info.releaselevel not in ('final', 'candidate'):
return
EXPECTED_MAGIC_NUMBER = 3379
actual = int.from_bytes(self.util.MAGIC_NUMBER[:2], 'little')
actual = int.from_bytes(importlib.util.MAGIC_NUMBER[:2], 'little')

msg = (
"Candidate and final releases require the current "
"To avoid breaking backwards compatibility with cached bytecode "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't be better to use a multiline string literal if you want such verbose error message?

But I think that "broken *.pyc files compatibility" would be enough.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Then you'd have to dedent it, etc. This message is perfectly legible to me.

"files that can't be automatically regenerated by the current "
"user, candidate and final releases require the current "
"importlib.util.MAGIC_NUMBER to match the expected "
"magic number in this test. Set the expected "
"magic number in this test to the current MAGIC_NUMBER to "
Expand All @@ -796,10 +801,5 @@ def test_magic_number(self):
self.assertEqual(EXPECTED_MAGIC_NUMBER, actual, msg)


Source_MagicNumberTests = util.specialize_class(
MagicNumberTests, 'Source', None, util=importlib_util
)


if __name__ == '__main__':
unittest.main()