summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2014-05-04 09:59:39 -0400
committerNed Batchelder <ned@nedbatchelder.com>2014-05-04 09:59:39 -0400
commit94cfa23bc1c22587979ac2343dcd553789584472 (patch)
tree0b7a0037cb7ca21ee211755def99a41ac4f55aae
parent846a0de82c2a943d6011d8a0d44a0c928a4695f1 (diff)
downloadpython-coveragepy-git-94cfa23bc1c22587979ac2343dcd553789584472.tar.gz
Refactoring, asymptotically approaching multi-language
-rw-r--r--coverage/backward.py6
-rw-r--r--coverage/codeunit.py4
-rw-r--r--coverage/execfile.py4
-rw-r--r--coverage/parser.py35
4 files changed, 19 insertions, 30 deletions
diff --git a/coverage/backward.py b/coverage/backward.py
index 03fa6512..e81dd199 100644
--- a/coverage/backward.py
+++ b/coverage/backward.py
@@ -50,12 +50,12 @@ if sys.version_info >= (3, 0):
# Python 3.2 provides `tokenize.open`, the best way to open source files.
import tokenize
try:
- open_source = tokenize.open # pylint: disable=E1101
+ open_python_source = tokenize.open # pylint: disable=E1101
except AttributeError:
from io import TextIOWrapper
detect_encoding = tokenize.detect_encoding # pylint: disable=E1101
# Copied from the 3.2 stdlib:
- def open_source(fname):
+ def open_python_source(fname):
"""Open a file in read only mode using the encoding detected by
detect_encoding().
"""
@@ -66,7 +66,7 @@ if sys.version_info >= (3, 0):
text.mode = 'r'
return text
else:
- def open_source(fname):
+ def open_python_source(fname):
"""Open a source file the best way."""
return open(fname, "rU")
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index d9cd5e44..88858801 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -2,7 +2,7 @@
import glob, os, re
-from coverage.backward import open_source, string_class, StringIO
+from coverage.backward import open_python_source, string_class, StringIO
from coverage.misc import CoverageException, NoSource
from coverage.parser import CodeParser, PythonParser
from coverage.phystokens import source_token_lines, source_encoding
@@ -123,7 +123,7 @@ class CodeUnit(object):
"""Return an open file for reading the source of the code unit."""
if os.path.exists(self.filename):
# A regular text file: open it.
- return open_source(self.filename)
+ return open_python_source(self.filename)
# Maybe it's in a zip file?
source = self.file_locator.get_zip_data(self.filename)
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 7b90137a..10c7b917 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -2,7 +2,7 @@
import imp, marshal, os, sys
-from coverage.backward import open_source
+from coverage.backward import open_python_source
from coverage.misc import ExceptionDuringRun, NoCode, NoSource
@@ -123,7 +123,7 @@ def make_code_from_py(filename):
"""Get source from `filename` and make a code object of it."""
# Open the source file.
try:
- source_file = open_source(filename)
+ source_file = open_python_source(filename)
except IOError:
raise NoSource("No file to run: %r" % filename)
diff --git a/coverage/parser.py b/coverage/parser.py
index f569de25..cfaf02fa 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -3,8 +3,8 @@
import collections, dis, re, token, tokenize
from coverage.backward import StringIO
-from coverage.backward import open_source, range # pylint: disable=W0622
-from coverage.backward import bytes_to_ints
+from coverage.backward import range # pylint: disable=W0622
+from coverage.backward import bytes_to_ints, open_python_source
from coverage.bytecode import ByteCodes, CodeObjects
from coverage.misc import nice_pair, expensive, join_regex
from coverage.misc import CoverageException, NoSource, NotPython
@@ -17,26 +17,14 @@ class CodeParser(object):
def _adjust_filename(self, fname):
return fname
- def first_lines(self, lines, *ignores):
+ def first_lines(self, lines):
"""Map the line numbers in `lines` to the correct first line of the
statement.
- Skip any line mentioned in any of the sequences in `ignores`.
-
Returns a set of the first lines.
"""
- ignore = set()
- for ign in ignores:
- ignore.update(ign)
- lset = set()
- for l in lines:
- if l in ignore:
- continue
- new_l = self.first_line(l)
- if new_l not in ignore:
- lset.add(new_l)
- return lset
+ return set(self.first_line(l) for l in lines)
def first_line(self, line):
return line
@@ -66,7 +54,7 @@ class PythonParser(CodeParser):
self.text = text
if not self.text:
try:
- with open_source(self.filename) as sourcef:
+ with open_python_source(self.filename) as sourcef:
self.text = sourcef.read()
except IOError as err:
raise NoSource(
@@ -229,11 +217,12 @@ class PythonParser(CodeParser):
)
excluded_lines = self.first_lines(self.excluded)
- lines = self.first_lines(
- self.statement_starts,
- excluded_lines,
- self.docstrings
- )
+ ignore = set()
+ ignore.update(excluded_lines)
+ ignore.update(self.docstrings)
+ starts = self.statement_starts - ignore
+ lines = self.first_lines(starts)
+ lines -= ignore
return lines, excluded_lines
@@ -346,7 +335,7 @@ class ByteParser(object):
else:
if not text:
assert filename, "If no code or text, need a filename"
- with open_source(filename) as sourcef:
+ with open_python_source(filename) as sourcef:
text = sourcef.read()
self.text = text