You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Refactor the dis module to provide better building blocks for bytecode analysis #56025
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/ncoghlan'closed_at=<Date2013-05-07.09:55:08.281>created_at=<Date2011-04-10.00:20:08.483>labels= ['type-feature', 'library']
title='Refactor the dis module to provide better building blocks for bytecode analysis'updated_at=<Date2016-01-19.07:51:22.304>user='https://github.com/eltoder'
As discussed in bpo-11549 a couple of tests need to inspect disassembly of some code. Currently they have to override sys.stdout, run dis and restore stdout back. It would be much nicer if dis module provided functions that return disassembly as a string.
Provided is a patch that adds file argument to most dis functions, defaulting to sys.stdout. On top of that there are 2 new functions: dis_to_str and disassembly_to_str that return disassembly as a string instead of writing it to a file.
Inspecting the text disassembly is a bit fragile for testing. It would be better to scan a list of (opcode, oparg) pairs for given pattern (i.e. (LOAD_CONST, 3) where consts[3] --> some target value).
eltoder commented on Apr 10, 2011
eltodermannequin
MannequinAuthor
More actions
Agreed, but that would require rewriting of all tests in test_peepholer.
I really like the idea of adding some lower level infrastructure to dis to make it generator based, making the disassembly more amenable to programmatic manipulation.
Consider if, for each line disassemble() currently prints, we had an underlying iterator that yielded a named tuple consisting of (index, opcode, oparg, linestart, details). I've created a proof-of-concept for that in my sandbox (http://hg.python.org/sandbox/ncoghlan/file/get_opinfo/Lib/dis.py) which adds a get_opinfo() function that does exactly. With disassemble() rewritten to use that, test_dis and test_peepholer still pass as currently written.
Near-term, test_peepholer could easily continue to do what it does now (i.e. use the higher level dis() function and redirect sys.stdout). Longer term, it could be written to analyse the opcode stream instead of doing string comparisons.
changed the title [-]Add functions to return disassembly as string[/-][+]Refactor the dis module to provide better building blocks for bytecode analysis[/+]on Apr 10, 2011
Re-running test 'test_dis' in verbose mode
test test_dis crashed -- Traceback (most recent call last):
File "/opt/python/3.x.langa-ubuntu/build/Lib/test/regrtest.py", line 1294, in runtest_inner
the_module = importlib.import_module(abstest)
File "/opt/python/3.x.langa-ubuntu/build/Lib/importlib/__init__.py", line 92, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1603, in _gcd_import
File "<frozen importlib._bootstrap>", line 1584, in _find_and_load
File "<frozen importlib._bootstrap>", line 1551, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 591, in _check_name_wrapper
File "<frozen importlib._bootstrap>", line 1053, in load_module
File "<frozen importlib._bootstrap>", line 1034, in load_module
File "<frozen importlib._bootstrap>", line 567, in module_for_loader_wrapper
File "<frozen importlib._bootstrap>", line 901, in _load_module
File "<frozen importlib._bootstrap>", line 297, in _call_with_frames_removed
File "/opt/python/3.x.langa-ubuntu/build/Lib/test/test_dis.py", line 4, in <module>from test.bytecode_helper import BytecodeTestCase
ImportError: No module named 'test.bytecode_helper'
Activity
eltoder commentedon Apr 10, 2011
As discussed in bpo-11549 a couple of tests need to inspect disassembly of some code. Currently they have to override sys.stdout, run dis and restore stdout back. It would be much nicer if dis module provided functions that return disassembly as a string.
Provided is a patch that adds file argument to most dis functions, defaulting to sys.stdout. On top of that there are 2 new functions: dis_to_str and disassembly_to_str that return disassembly as a string instead of writing it to a file.
rhettinger commentedon Apr 10, 2011
Inspecting the text disassembly is a bit fragile for testing. It would be better to scan a list of (opcode, oparg) pairs for given pattern (i.e. (LOAD_CONST, 3) where consts[3] --> some target value).
eltoder commentedon Apr 10, 2011
Agreed, but that would require rewriting of all tests in test_peepholer.
rhettinger commentedon Apr 10, 2011
Yep!
ncoghlan commentedon Apr 10, 2011
I really like the idea of adding some lower level infrastructure to dis to make it generator based, making the disassembly more amenable to programmatic manipulation.
Consider if, for each line disassemble() currently prints, we had an underlying iterator that yielded a named tuple consisting of (index, opcode, oparg, linestart, details). I've created a proof-of-concept for that in my sandbox (http://hg.python.org/sandbox/ncoghlan/file/get_opinfo/Lib/dis.py) which adds a get_opinfo() function that does exactly. With disassemble() rewritten to use that, test_dis and test_peepholer still pass as currently written.
Near-term, test_peepholer could easily continue to do what it does now (i.e. use the higher level dis() function and redirect sys.stdout). Longer term, it could be written to analyse the opcode stream instead of doing string comparisons.
ncoghlan commentedon Apr 10, 2011
Changed issue title to cover ideas like get_opinfo().
[-]Add functions to return disassembly as string[/-][+]Refactor the dis module to provide better building blocks for bytecode analysis[/+]ncoghlan commentedon Apr 10, 2011
Oops, I forgot to edit my comment to match the OpInfo definition I used in the proof-of-concept:
eltoder commentedon Apr 10, 2011
So in the near term, dis-based tests should continue to copy/paste sys.stdout redirection code?
ncoghlan commentedon Apr 10, 2011
If we decide our long term goal is the use of the opcode stream for programmatic access, then yes.
alex commentedon Apr 10, 2011
FWIW in PyPy we have https://bitbucket.org/pypy/pypy/src/default/lib_pypy/disassembler.py which we use for some of our tools.
jcea commentedon Apr 11, 2011
Do not forget to update docs too.
34 remaining items
python-dev commentedon May 6, 2013
New changeset f65b867ce817 by Nick Coghlan in branch 'default':
Issue bpo-11816: multiple improvements to the dis module
http://hg.python.org/cpython/rev/f65b867ce817
python-dev commentedon May 6, 2013
New changeset d3fee4c64654 by Nick Coghlan in branch 'default':
Issue bpo-11816: switch test_peepholer to bytecode_helper
http://hg.python.org/cpython/rev/d3fee4c64654
ncoghlan commentedon May 6, 2013
And two-and-a-bit years later, we're done - thanks all, any further feedback or problems can be filed as a new issue :)
neologix commentedon May 6, 2013
test_dis is failing on some buildbots:
http://buildbot.python.org/all/builders/AMD64 Ubuntu LTS 3.x/builds/1674/steps/test/logs/stdio
pitrou commentedon May 6, 2013
Yes, this is bytecode_helper hasn't been added to the repository.
pitrou commentedon May 6, 2013
(this is *because*, sorry)
vstinner commentedon May 6, 2013
Ping! The test is still failing.
takluyver commentedon May 6, 2013
bytecode_helper is there in dis_api3.diff - anyone with commit rights should be able to add it to the repository.
python-dev commentedon May 6, 2013
New changeset 84d1a0e32d3b by Nick Coghlan in branch 'default':
Issue bpo-11816: Add missing test helper
http://hg.python.org/cpython/rev/84d1a0e32d3b
ncoghlan commentedon May 7, 2013
I checked in the missing file after I woke up this morning. Maybe I'll learn to use hg import instead of patch some day...
Sorry for the noise.
python-dev commentedon Jan 19, 2016
New changeset bf997b22df06 by Victor Stinner in branch '3.5':
Fix BytecodeTestCase.assertNotInBytecode()
https://hg.python.org/cpython/rev/bf997b22df06