示例工程 01-orientation

01-orientation/02-pyc-and-bytecode/test_pyc_and_bytecode.py

python · 0.6 KB

"""test_pyc_and_bytecode.py"""
 
from __future__ import annotations
 
import importlib.util
from pathlib import Path
 
from pyc_and_bytecode import compile_to_pyc, function_opcodes
 
 
def test_function_bytecode_contains_return() -> None:
    ops = function_opcodes()
    assert "RESUME" in ops
    assert "RETURN_VALUE" in ops
    assert "BINARY_OP" in ops or "BINARY_ADD" in ops
 
 
def test_pyc_is_written_with_magic(tmp_path: Path) -> None:
    pyc, magic = compile_to_pyc(tmp_path)
    assert pyc.exists()
    assert "__pycache__" in str(pyc)
    assert magic == importlib.util.MAGIC_NUMBER
    assert pyc.stat().st_size > 0