summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
Diffstat (limited to 'coverage')
-rw-r--r--coverage/backward.py5
-rw-r--r--coverage/cmdline.py11
-rw-r--r--coverage/collector.py21
-rw-r--r--coverage/control.py3
-rw-r--r--coverage/env.py21
-rw-r--r--coverage/files.py5
-rw-r--r--coverage/html.py8
-rw-r--r--coverage/misc.py4
-rw-r--r--coverage/phystokens.py4
-rw-r--r--coverage/python.py7
-rw-r--r--coverage/templite.py5
11 files changed, 64 insertions, 30 deletions
diff --git a/coverage/backward.py b/coverage/backward.py
index 9a3c9f7..5a32365 100644
--- a/coverage/backward.py
+++ b/coverage/backward.py
@@ -7,6 +7,9 @@
import sys
+from coverage import env
+
+
# Pythons 2 and 3 differ on where to get StringIO.
try:
from cStringIO import StringIO
@@ -63,7 +66,7 @@ else:
# Python 3.x is picky about bytes and strings, so provide methods to
# get them right, and make them no-ops in 2.x
-if sys.version_info >= (3, 0):
+if env.PY3:
def to_bytes(s):
"""Convert string `s` to bytes."""
return s.encode('utf8')
diff --git a/coverage/cmdline.py b/coverage/cmdline.py
index 816c545..f016084 100644
--- a/coverage/cmdline.py
+++ b/coverage/cmdline.py
@@ -1,7 +1,12 @@
"""Command-line support for Coverage."""
-import glob, optparse, os, sys, traceback
+import glob
+import optparse
+import os
+import sys
+import traceback
+from coverage import env
from coverage.execfile import run_python_file, run_python_module
from coverage.misc import CoverageException, ExceptionDuringRun, NoSource
from coverage.debug import info_formatter
@@ -597,7 +602,7 @@ def unshell_list(s):
"""Turn a command-line argument into a list."""
if not s:
return None
- if sys.platform == 'win32':
+ if env.WINDOWS:
# When running coverage as coverage.exe, some of the behavior
# of the shell is emulated: wildcards are expanded into a list of
# filenames. So you have to single-quote patterns on the command
@@ -609,7 +614,7 @@ def unshell_list(s):
def unglob_args(args):
"""Interpret shell wildcards for platforms that need it."""
- if sys.platform == 'win32':
+ if env.WINDOWS:
globbed = []
for arg in args:
if '?' in arg or '*' in arg:
diff --git a/coverage/collector.py b/coverage/collector.py
index 39acd7b..ded6d92 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -2,6 +2,7 @@
import os, sys
+from coverage import env
from coverage.misc import CoverageException
from coverage.pytracer import PyTracer
@@ -11,23 +12,18 @@ try:
except ImportError:
# Couldn't import the C extension, maybe it isn't built.
if os.getenv('COVERAGE_TEST_TRACER') == 'c':
- # During testing, we use the COVERAGE_TEST_TRACER env var to indicate
- # that we've fiddled with the environment to test this fallback code.
- # If we thought we had a C tracer, but couldn't import it, then exit
- # quickly and clearly instead of dribbling confusing errors. I'm using
- # sys.exit here instead of an exception because an exception here
- # causes all sorts of other noise in unittest.
+ # During testing, we use the COVERAGE_TEST_TRACER environment variable
+ # to indicate that we've fiddled with the environment to test this
+ # fallback code. If we thought we had a C tracer, but couldn't import
+ # it, then exit quickly and clearly instead of dribbling confusing
+ # errors. I'm using sys.exit here instead of an exception because an
+ # exception here causes all sorts of other noise in unittest.
sys.stderr.write(
"*** COVERAGE_TEST_TRACER is 'c' but can't import CTracer!\n"
)
sys.exit(1)
CTracer = None
-try:
- import __pypy__
-except ImportError:
- __pypy__ = None
-
class Collector(object):
"""Collects trace data.
@@ -141,7 +137,8 @@ class Collector(object):
# A cache of the results from should_trace, the decision about whether
# to trace execution in a file. A dict of filename to (filename or
# None).
- if __pypy__ is not None:
+ if env.PYPY:
+ import __pypy__ # pylint: disable=import-error
# Alex Gaynor said:
# should_trace_cache is a strictly growing key: once a key is in
# it, it never changes. Further, the keys used to access it are
diff --git a/coverage/control.py b/coverage/control.py
index 16a9297..36687fe 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -8,6 +8,7 @@ import random
import socket
import sys
+from coverage import env
from coverage.annotate import AnnotateReporter
from coverage.backward import string_class, iitems
from coverage.collector import Collector
@@ -323,7 +324,7 @@ class Coverage(object):
if os.path.exists(py_filename):
# Found a .py file, use that.
return py_filename
- if sys.platform == "win32":
+ if env.WINDOWS:
# On Windows, it could be a .pyw file.
pyw_filename = py_filename + "w"
if os.path.exists(pyw_filename):
diff --git a/coverage/env.py b/coverage/env.py
new file mode 100644
index 0000000..85ffa5f
--- /dev/null
+++ b/coverage/env.py
@@ -0,0 +1,21 @@
+"""Determine facts about the environment."""
+
+import os
+import sys
+
+# Operating systems.
+WINDOWS = sys.platform == "win32"
+LINUX = sys.platform == "linux2"
+
+# Python implementations.
+PYPY = '__pypy__' in sys.builtin_module_names
+
+# Python versions.
+PY2 = sys.version_info < (3, 0)
+PY3 = sys.version_info >= (3, 0)
+
+# Coverage.py specifics.
+# Are we using the C-implemented trace function?
+C_TRACER = os.getenv('COVERAGE_TEST_TRACER', 'c') == 'c'
+# Are we coverage-measuring ourselves?
+METACOV = os.getenv('COVERAGE_COVERAGE', '') != ''
diff --git a/coverage/files.py b/coverage/files.py
index 15ccabc..f7fc969 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -8,6 +8,7 @@ import posixpath
import re
import sys
+from coverage import env
from coverage.misc import CoverageException, join_regex
@@ -54,7 +55,7 @@ class FileLocator(object):
return self.canonical_filename_cache[filename]
-if sys.platform == 'win32':
+if env.WINDOWS:
def actual_path(path):
"""Get the actual path of `path`, including the correct case."""
@@ -188,7 +189,7 @@ class FnmatchMatcher(object):
# take care of that ourselves.
fnpats = (fnmatch.translate(p) for p in pats)
fnpats = (p.replace(r"\/", r"[\\/]") for p in fnpats)
- if sys.platform == 'win32':
+ if env.WINDOWS:
# Windows is also case-insensitive. BTW: the regex docs say that
# flags like (?i) have to be at the beginning, but fnmatch puts
# them at the end, and having two there seems to work fine.
diff --git a/coverage/html.py b/coverage/html.py
index ec151b4..2a9e0d1 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -2,9 +2,13 @@
from __future__ import unicode_literals
-import json, os, re, shutil, sys
+import json
+import os
+import re
+import shutil
import coverage
+from coverage import env
from coverage.backward import iitems
from coverage.misc import CoverageException, Hasher
from coverage.report import Reporter
@@ -68,7 +72,7 @@ class HtmlReporter(Reporter):
super(HtmlReporter, self).__init__(cov, config)
self.directory = None
title = self.config.html_title
- if sys.version_info < (3, 0):
+ if env.PY2:
title = title.decode("utf8")
self.template_globals = {
'escape': escape,
diff --git a/coverage/misc.py b/coverage/misc.py
index 924199e..875d904 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -4,8 +4,8 @@ import errno
import hashlib
import inspect
import os
-import sys
+from coverage import env
from coverage.backward import string_class, to_bytes
@@ -154,7 +154,7 @@ def overrides(obj, method_name, base_class):
# Python 2/3 compatibility: Python 2 returns an instancemethod object, the
# function is the .im_func attribute. Python 3 returns a plain function
# object already.
- if sys.version_info < (3, 0):
+ if env.PY2:
klass_func = klass_func.im_func
base_func = base_func.im_func
diff --git a/coverage/phystokens.py b/coverage/phystokens.py
index c52e28a..b3b0870 100644
--- a/coverage/phystokens.py
+++ b/coverage/phystokens.py
@@ -3,10 +3,10 @@
import codecs
import keyword
import re
-import sys
import token
import tokenize
+from coverage import env
from coverage.backward import iternext
@@ -258,7 +258,7 @@ def _source_encoding_py3(source):
return tokenize.detect_encoding(readline)[0]
-if sys.version_info >= (3, 0):
+if env.PY3:
source_encoding = _source_encoding_py3
else:
source_encoding = _source_encoding_py2
diff --git a/coverage/python.py b/coverage/python.py
index 977497a..53da561 100644
--- a/coverage/python.py
+++ b/coverage/python.py
@@ -5,6 +5,7 @@ import sys
import tokenize
import zipimport
+from coverage import env
from coverage.backward import unicode_class
from coverage.codeunit import CodeUnit
from coverage.misc import NoSource, join_regex
@@ -31,7 +32,7 @@ def read_python_source(filename):
def get_python_source(filename):
"""Return the source code, as a str."""
base, ext = os.path.splitext(filename)
- if ext == ".py" and sys.platform == "win32":
+ if ext == ".py" and env.WINDOWS:
exts = [".py", ".pyw"]
else:
exts = [ext]
@@ -46,7 +47,7 @@ def get_python_source(filename):
# Maybe it's in a zip file?
source = get_zip_bytes(try_filename)
if source is not None:
- if sys.version_info >= (3, 0):
+ if env.PY3:
source = source.decode(source_encoding(source))
break
else:
@@ -148,7 +149,7 @@ class PythonCodeUnit(CodeUnit):
def source(self):
if self._source is None:
self._source = get_python_source(self.filename)
- if sys.version_info < (3, 0):
+ if env.PY2:
encoding = source_encoding(self._source)
self._source = self._source.decode(encoding, "replace")
assert isinstance(self._source, unicode_class)
diff --git a/coverage/templite.py b/coverage/templite.py
index c42e380..20b0e50 100644
--- a/coverage/templite.py
+++ b/coverage/templite.py
@@ -3,7 +3,8 @@
# Coincidentally named the same as http://code.activestate.com/recipes/496702/
import re
-import sys
+
+from coverage import env
class TempliteSyntaxError(ValueError):
@@ -122,7 +123,7 @@ class Templite(object):
code.add_line("result = []")
code.add_line("append_result = result.append")
code.add_line("extend_result = result.extend")
- if sys.version_info < (3, 0):
+ if env.PY2:
code.add_line("to_str = unicode")
else:
code.add_line("to_str = str")