Skip to content

Commit 7cd0901

Browse files
committed
Python stdlib is now not measured by default. If needed, turn it on with the -L switch.
1 parent 06b74ff commit 7cd0901

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ HTML reporting, and continued refactoring.
1010
- HTML reports and annotation of source files: use the new -b switch. Thanks
1111
to George Song for code, inspiration and guidance.
1212

13+
- Code in the Python standard library is not measured by default. If you need
14+
to measure standard library code, use the -L switch during execution.
15+
1316
- Annotation into a directory (-a -d) behaves differently. The annotated files
1417
are named with their hierarchy flattened so that same-named files from
1518
different directories no longer collide. Also, only files in the current

coverage/cmdline.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
1010
Usage:
1111
12-
coverage -x [-p] MODULE.py [ARG1 ARG2 ...]
12+
coverage -x [-p] [-L] MODULE.py [ARG1 ARG2 ...]
1313
Execute the module, passing the given command-line arguments, collecting
1414
coverage data. With the -p option, include the machine name and process
15-
ID in the .coverage file name.
15+
ID in the .coverage file name. With -L, measure coverage even inside the
16+
Python standard library, which isn't done by default.
1617
1718
coverage -e
1819
Erase collected coverage data.
@@ -86,6 +87,7 @@ def command_line(self, argv, help_fn=None):
8687
'-e': 'erase',
8788
'-h': 'help',
8889
'-i': 'ignore-errors',
90+
'-L': 'stdlib',
8991
'-m': 'show-missing',
9092
'-p': 'parallel-mode',
9193
'-r': 'report',
@@ -135,6 +137,7 @@ def command_line(self, argv, help_fn=None):
135137

136138
# Do something.
137139
self.coverage.parallel_mode = settings.get('parallel-mode')
140+
self.coverage.cover_stdlib = settings.get('stdlib')
138141
self.coverage.get_ready()
139142

140143
if settings.get('erase'):

coverage/control.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Core control stuff for coverage.py"""
22

3-
import os, socket
3+
import os, socket, sys
44

55
from coverage.annotate import AnnotateReporter
66
from coverage.codeunit import code_unit_factory
@@ -17,11 +17,13 @@ def __init__(self):
1717

1818
self.parallel_mode = False
1919
self.exclude_re = ''
20+
self.cover_stdlib = False
2021
self.nesting = 0
22+
2123
self.file_locator = FileLocator()
24+
self.sysprefix = self.file_locator.abs_file(sys.prefix)
2225

2326
self.collector = Collector(self.should_trace)
24-
2527
self.data = CoverageData(collector="coverage.py v%s" % __version__)
2628

2729
# The default exclude pattern.
@@ -36,14 +38,20 @@ def should_trace(self, filename):
3638
3739
Returns a canonicalized filename if it should be traced, False if it
3840
should not.
41+
3942
"""
4043
if filename == '<string>':
4144
# There's no point in ever tracing string executions, we can't do
4245
# anything with the data later anyway.
4346
return False
44-
# TODO: flag: ignore std lib?
47+
48+
canonical = self.file_locator.canonical_filename(filename)
49+
if not self.cover_stdlib:
50+
if canonical.startswith(self.sysprefix):
51+
return False
52+
4553
# TODO: ignore by module as well as file?
46-
return self.file_locator.canonical_filename(filename)
54+
return canonical
4755

4856
def use_cache(self, usecache):
4957
"""Control the use of a data file (incorrectly called a cache).

test/test_coverage.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,44 @@ def testFileNames(self):
16881688
filename, _, _, _ = cov.analysis(sys.modules["mymod"])
16891689
self.assertEqual(os.path.basename(filename), "mymod.py")
16901690

1691+
def testIgnoreStdLib(self):
1692+
self.makeFile("mymain", """\
1693+
import mymod, colorsys
1694+
a = 1
1695+
hls = colorsys.rgb_to_hls(1.0, 0.5, 0.0)
1696+
""")
1697+
1698+
self.makeFile("mymod", """\
1699+
fooey = 17
1700+
""")
1701+
1702+
# Measure without the stdlib.
1703+
cov1 = coverage.coverage()
1704+
self.assertEqual(cov1.cover_stdlib, False)
1705+
cov1.start()
1706+
self.importModule("mymain")
1707+
cov1.stop()
1708+
1709+
# some statements were marked executed in mymain.py
1710+
_, statements, missing, _ = cov1.analysis("mymain.py")
1711+
self.assertNotEqual(statements, missing)
1712+
# but none were in colorsys.py
1713+
_, statements, missing, _ = cov1.analysis("colorsys.py")
1714+
self.assertEqual(statements, missing)
1715+
1716+
# Measure with the stdlib.
1717+
cov2 = coverage.coverage()
1718+
cov2.cover_stdlib = True
1719+
cov2.start()
1720+
self.importModule("mymain")
1721+
cov2.stop()
1722+
1723+
# some statements were marked executed in mymain.py
1724+
_, statements, missing, _ = cov2.analysis("mymain.py")
1725+
self.assertNotEqual(statements, missing)
1726+
# and some were marked executed in colorsys.py
1727+
_, statements, missing, _ = cov2.analysis("colorsys.py")
1728+
self.assertNotEqual(statements, missing)
16911729

16921730

16931731
class ProcessTest(CoverageTest):

0 commit comments

Comments
 (0)