diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-01-07 19:42:09 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-01-07 19:42:09 -0500 |
commit | fcc18ffcb11bca315c6c0690414e817d5c22b4dd (patch) | |
tree | bfc24ee3b407193fdc7528844be90ec597c03a44 /lab/parser.py | |
parent | 152dd7d6e4b9a53e89cb7ec0cacf0f01be4abc73 (diff) | |
download | python-coveragepy-git-fcc18ffcb11bca315c6c0690414e817d5c22b4dd.tar.gz |
Make lab/parser.py usable on snippets within larger Python files.
--HG--
branch : ast-branch
Diffstat (limited to 'lab/parser.py')
-rw-r--r-- | lab/parser.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/lab/parser.py b/lab/parser.py index 717fbbf9..08b50921 100644 --- a/lab/parser.py +++ b/lab/parser.py @@ -5,9 +5,13 @@ from __future__ import division -import glob, os, sys import collections -from optparse import OptionParser +import glob +import optparse +import os +import re +import sys +import textwrap import disgen @@ -24,7 +28,7 @@ class ParserMain(object): def main(self, args): """A main function for trying the code from the command line.""" - parser = OptionParser() + parser = optparse.OptionParser() parser.add_option( "-c", action="store_true", dest="chunks", help="Show basic block chunks" @@ -72,8 +76,20 @@ class ParserMain(object): def one_file(self, options, filename): """Process just one file.""" + # `filename` can have a line number suffix. In that case, extract those + # lines, dedent them, and use that. + match = re.search(r"^(.*):(\d+)-(\d+)$", filename) + if match: + filename, start, end = match.groups() + start, end = int(start), int(end) + else: + start = end = None + try: text = get_python_source(filename) + if start is not None: + lines = text.splitlines(True) + text = textwrap.dedent("".join(lines[start-1:end])) bp = ByteParser(text, filename=filename) except Exception as err: print("%s" % (err,)) |