Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

A possible test case to trigger this code path based on 
testUncheckedHashBasedPyc would be as below. check_hash_based_pycs can be 
patched to be "always" so that the hash is validated using 
_bootstrap_external._validate_hash_pyc. The difference between "state = old" 
for pyc and "state = new" in py would ensure the hashes are different to raise 
ImportError expected and the test picks up state = new as the updated source 
code. Without fixing the typo trying to make hash validation would throw 
NameError.


@unittest.mock.patch('_imp.check_hash_based_pycs', 'always')
def test_checked_hash_based_change_pyc(self):
    source = b"state = 'old'"
    source_hash = importlib.util.source_hash(source)
    bytecode = importlib._bootstrap_external._code_to_hash_pyc(
        compile(source, "???", "exec"),
        source_hash,
        False,
    )
    files = {TESTMOD + ".py": (NOW, "state = 'new'"),
             TESTMOD + ".pyc": (NOW - 20, bytecode)}
    def check(mod):
        self.assertEqual(mod.state, 'new')
    self.doTest(None, files, TESTMOD, call=check)

----------
nosy: +xtreak

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39033>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to