bytecode.py 723 B

1234567891011121314151617181920212223
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Bytecode manipulation for coverage.py"""
  4. import types
  5. class CodeObjects(object):
  6. """Iterate over all the code objects in `code`."""
  7. def __init__(self, code):
  8. self.stack = [code]
  9. def __iter__(self):
  10. while self.stack:
  11. # We're going to return the code object on the stack, but first
  12. # push its children for later returning.
  13. code = self.stack.pop()
  14. for c in code.co_consts:
  15. if isinstance(c, types.CodeType):
  16. self.stack.append(c)
  17. yield code