Skip to content

Commit

Permalink
Clean-ups I noticed while doing something else
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 17, 2019
1 parent e72fff0 commit d32835d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
44 changes: 22 additions & 22 deletions coverage/collector.py
Expand Up @@ -389,6 +389,25 @@ def cached_abs_file(self, filename):
except KeyError:
return self.abs_file_cache.setdefault(key, abs_file(filename))

def abs_file_dict(self, d):
"""Return a dict like d, but with keys modified by `abs_file`."""
# The call to litems() ensures that the GIL protects the dictionary
# iterator against concurrent modifications by tracers running
# in other threads. We try three times in case of concurrent
# access, hoping to get a clean copy.
runtime_err = None
for _ in range(3):
try:
items = litems(d)
except RuntimeError as ex:
runtime_err = ex
else:
break
else:
raise runtime_err

return dict((self.cached_abs_file(k), v) for k, v in items if v)

def flush_data(self):
"""Save the collected data to our associated `CoverageData`.
Expand All @@ -400,30 +419,11 @@ def flush_data(self):
if not self._activity():
return False

def abs_file_dict(d):
"""Return a dict like d, but with keys modified by `abs_file`."""
# The call to litems() ensures that the GIL protects the dictionary
# iterator against concurrent modifications by tracers running
# in other threads. We try three times in case of concurrent
# access, hoping to get a clean copy.
runtime_err = None
for _ in range(3):
try:
items = litems(d)
except RuntimeError as ex:
runtime_err = ex
else:
break
else:
raise runtime_err

return dict((self.cached_abs_file(k), v) for k, v in items if v)

if self.branch:
self.covdata.add_arcs(abs_file_dict(self.data))
self.covdata.add_arcs(self.abs_file_dict(self.data))
else:
self.covdata.add_lines(abs_file_dict(self.data))
self.covdata.add_file_tracers(abs_file_dict(self.file_tracers))
self.covdata.add_lines(self.abs_file_dict(self.data))
self.covdata.add_file_tracers(self.abs_file_dict(self.file_tracers))

self._clear_data()
return True
16 changes: 8 additions & 8 deletions coverage/control.py
Expand Up @@ -736,14 +736,14 @@ def _get_file_reporter(self, morf):
if plugin_name:
plugin = self._plugins.get(plugin_name)

if plugin:
file_reporter = plugin.file_reporter(abs_morf)
if file_reporter is None:
raise CoverageException(
"Plugin %r did not provide a file reporter for %r." % (
plugin._coverage_plugin_name, morf
)
)
if plugin:
file_reporter = plugin.file_reporter(abs_morf)
if file_reporter is None:
raise CoverageException(
"Plugin %r did not provide a file reporter for %r." % (
plugin._coverage_plugin_name, morf
)
)

if file_reporter == "python":
file_reporter = PythonFileReporter(morf, self)
Expand Down
2 changes: 0 additions & 2 deletions tests/goldtest.py
Expand Up @@ -12,8 +12,6 @@
import sys
import xml.etree.ElementTree

from unittest_mixins import change_dir # pylint: disable=unused-import

from coverage import env

from tests.coveragetest import TESTS_DIR
Expand Down
3 changes: 2 additions & 1 deletion tests/test_html.py
Expand Up @@ -13,6 +13,7 @@
import sys

import mock
from unittest_mixins import change_dir

import coverage
from coverage.backward import unicode_class
Expand All @@ -24,7 +25,7 @@

from tests.coveragetest import CoverageTest, TESTS_DIR
from tests.goldtest import gold_path
from tests.goldtest import change_dir, compare, contains, doesnt_contain, contains_any
from tests.goldtest import compare, contains, doesnt_contain, contains_any


class HtmlTestHelpers(CoverageTest):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_xml.py
Expand Up @@ -9,12 +9,14 @@
import re
from xml.etree import ElementTree

from unittest_mixins import change_dir

import coverage
from coverage.backward import import_local_file
from coverage.files import abs_file

from tests.coveragetest import CoverageTest
from tests.goldtest import change_dir, compare, gold_path
from tests.goldtest import compare, gold_path


class XmlTestHelpers(CoverageTest):
Expand Down

0 comments on commit d32835d

Please sign in to comment.