File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3838 dev = [
3939 " mypy>=1.16.1" ,
4040 " pytest>=8" ,
41+ " pytest-benchmark>=5" ,
4142 " pytest-cov>=6" ,
4243 " ruff>=0.12.0" ,
4344 ]
4445 test = [
4546 " pytest>=8" ,
47+ " pytest-benchmark>=5" ,
4648 " pytest-cov" ,
4749 ]
4850
Original file line number Diff line number Diff line change 1+ """Round-trip decompile/recompile benchmarks.
2+
3+ Run with: pytest tests/test_bench_roundtrip.py --benchmark-compare
4+ """
5+
6+ import types
7+
8+ import pytest
9+
10+ from bytecode import Bytecode
11+
12+
13+ def _collect_code_objects (root : types .CodeType , depth : int = 1 ) -> list [types .CodeType ]:
14+ result = [root ]
15+ if depth > 0 :
16+ for const in root .co_consts :
17+ if isinstance (const , types .CodeType ):
18+ result .extend (_collect_code_objects (const , depth - 1 ))
19+ return result
20+
21+
22+ def _dis_corpus () -> list [types .CodeType ]:
23+ import importlib .util
24+
25+ spec = importlib .util .find_spec ("dis" )
26+ assert spec and spec .origin
27+ src = open (spec .origin ).read ()
28+ top = compile (src , spec .origin , "exec" )
29+ return _collect_code_objects (top )
30+
31+
32+ _CORPUS = _dis_corpus ()
33+
34+
35+ @pytest .fixture (params = _CORPUS , ids = [c .co_name for c in _CORPUS ])
36+ def code_object (request ):
37+ return request .param
38+
39+
40+ def test_roundtrip (benchmark , code_object ):
41+ def roundtrip ():
42+ Bytecode .from_code (code_object ).to_code ()
43+
44+ benchmark (roundtrip )
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ isolated_build = true
66passenv = BYTECODE_PURE_PYTHON
77deps =
88 pytest
9+ pytest-benchmark
910 pytest-cov
1011 pytest-subtests
1112commands = pytest --cov bytecode --cov-report =xml -v tests
You can’t perform that action at this time.
0 commit comments