-
Notifications
You must be signed in to change notification settings - Fork 536
Linear scanning in CompileUnit.iter_DIEs() #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #------------------------------------------------------------------------------ | ||
| # elftools tests | ||
| # | ||
| # Seva Alekseyev ([email protected]) | ||
| # This code is in the public domain | ||
| # | ||
| # Making sure the two ways of iterating through DIEs (linear and by tree) | ||
| # return an identically built tree (parents, children, terminators). | ||
| # | ||
| # TODO: find a siblingless file. None in the corpus so far. | ||
| #------------------------------------------------------------------------------ | ||
|
|
||
| import unittest | ||
| import os | ||
| from elftools.elf.elffile import ELFFile | ||
|
|
||
| class TestTree(unittest.TestCase): | ||
| def test_tree(self): | ||
| self.run_test_on('dwarf_llpair.elf', 0, True) | ||
| self.run_test_on('test_debugsup1.debug', 2, False) | ||
|
|
||
| def run_test_on(self, file_name, cu_index, test_cached): | ||
| def die_summary(die): | ||
| return (die.offset, die.tag, die._terminator.offset if die._terminator else None, die.get_parent().offset if die.get_parent() else None) | ||
|
|
||
| with ELFFile.load_from_path(os.path.join('test', 'testfiles_for_unittests', file_name)) as elf: | ||
| di = elf.get_dwarf_info() | ||
| cu = next(c for i,c in enumerate(di.iter_CUs()) if i == cu_index) | ||
| #_terminator is only set on a DIE *after* that DIE is yielded during enumeration | ||
| DIEs = [d for d in cu.iter_DIEs()] | ||
| seq_DIEs = [die_summary(d) for d in DIEs] | ||
|
|
||
| if test_cached: | ||
| sample_offset = DIEs[len(DIEs) // 2].offset | ||
| # Offset of a random DIE from the middle for later | ||
|
|
||
| # Deliberately erase the CU/DIE cache to force a repeat parse - this time using an explicit tree traversal | ||
| di = elf.get_dwarf_info() | ||
| cu = next(c for i,c in enumerate(di.iter_CUs()) if i == cu_index) | ||
| DIEs = [d for d in cu._iter_DIE_subtree(cu.get_top_DIE())] | ||
|
|
||
| tree_DIEs = [die_summary(d) for d in DIEs] | ||
| self.assertSequenceEqual(seq_DIEs, tree_DIEs) | ||
|
|
||
| if test_cached: | ||
| # Another repeat parse, with a nonblank cache | ||
| di = elf.get_dwarf_info() | ||
| cu = next(c for i,c in enumerate(di.iter_CUs()) if i == cu_index) | ||
| cu.get_DIE_from_refaddr(sample_offset) # Cache this random DIE in the middle | ||
| DIEs = [d for d in cu.iter_DIEs()] | ||
| seq_DIEs_x = [die_summary(d) for d in DIEs] | ||
| self.assertSequenceEqual(seq_DIEs, seq_DIEs_x) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what's happening here. Aren't we supposed to yield from _iter_die_subtree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean instead of
supp_die.cu._iter_DIE_subtree(supp_die)it should have been
yield supp_die.cu._iter_DIE_subtree(supp_die)? That was my first stab too, but apparently when you do that (at least in Python 3.13), a generator object is yielded to the caller, instead of its contents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yield from?Otherwise, I don't see what this call is even doing.
_iter_DIE_subtreeyields stuff, it's not usually called for side effects.If you remove this call, do tests still pass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yield fromis right. And the autotest didn't catch that. It does now.