Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_iitems(self):
d = {'a': 1, 'b': 2, 'c': 3}
items = [('a', 1), ('b', 2), ('c', 3)]
self.assertCountEqual(list(iitems(d)), items)
def write_pickled_file(self, covdata, filename):
"""Write coverage data as pickled `filename`."""
# Create the file data.
file_data = {}
if covdata._arcs:
file_data['arcs'] = dict((f, list(amap)) for f, amap in iitems(covdata._arcs))
else:
file_data['lines'] = dict((f, list(lmap)) for f, lmap in iitems(covdata._lines))
# Write the pickle to the file.
with open(filename, 'wb') as file_obj:
pickle.dump(file_data, file_obj, 2)
any_set = False
try:
for option_spec in self.CONFIG_FILE_OPTIONS:
was_set = self._set_attr_from_config_option(cp, *option_spec)
if was_set:
any_set = True
except ValueError as err:
raise CoverageException("Couldn't read config file %s: %s" % (filename, err))
# Check that there are no unrecognized options.
all_options = collections.defaultdict(set)
for option_spec in self.CONFIG_FILE_OPTIONS:
section, option = option_spec[1].split(":")
all_options[section].add(option)
for section, options in iitems(all_options):
real_section = cp.has_section(section)
if real_section:
for unknown in set(cp.options(section)) - options:
raise CoverageException(
"Unrecognized option '[%s] %s=' in config file %s" % (
real_section, unknown, filename
)
)
# [paths] is special
if cp.has_section('paths'):
for option in cp.options('paths'):
self.paths[option] = cp.getlist('paths', option)
any_set = True
# plugins can have options
def add_file_tracers(self, file_tracers):
"""Add per-file plugin information.
`file_tracers` is { filename: plugin_name, ... }
"""
if self._debug.should('dataop'):
self._debug.write("Adding file tracers: %d files" % (len(file_tracers),))
if not file_tracers:
return
self._start_using()
with self._connect() as con:
for filename, plugin_name in iitems(file_tracers):
file_id = self._file_id(filename)
if file_id is None:
raise CoverageException(
"Can't add file tracer data for unmeasured file '%s'" % (filename,)
)
existing_plugin = self.file_tracer(filename)
if existing_plugin:
if existing_plugin != plugin_name:
raise CoverageException(
"Conflicting file tracer name for '%s': %r vs %r" % (
filename, existing_plugin, plugin_name,
)
)
elif plugin_name:
con.execute(
if self.config._config_contents
else '-none-'
),
('data_file', self._data.data_filename() if self._data is not None else "-none-"),
('python', sys.version.replace('\n', '')),
('platform', platform.platform()),
('implementation', platform.python_implementation()),
('executable', sys.executable),
('def_encoding', sys.getdefaultencoding()),
('fs_encoding', sys.getfilesystemencoding()),
('pid', os.getpid()),
('cwd', os.getcwd()),
('path', sys.path),
('environment', sorted(
("%s = %s" % (k, v))
for k, v in iitems(os.environ)
if any(slug in k for slug in ("COV", "PY"))
)),
('command_line', " ".join(getattr(sys, 'argv', ['-none-']))),
]
if self._inorout:
info.extend(self._inorout.sys_info())
info.extend(CoverageData.sys_info())
return info
def write(self):
"""Write the current status."""
status_file = os.path.join(self.directory, self.STATUS_FILE)
files = {}
for filename, fileinfo in iitems(self.files):
fileinfo['index']['nums'] = fileinfo['index']['nums'].init_args()
files[filename] = fileinfo
status = {
'format': self.STATUS_FORMAT,
'version': coverage.__version__,
'globals': self.globals,
'files': files,
}
with open(status_file, "w") as fout:
json.dump(status, fout, separators=(',', ':'))
def from_args(self, **kwargs):
"""Read config values from `kwargs`."""
for k, v in iitems(kwargs):
if v is not None:
if k in self.MUST_BE_LIST and isinstance(v, string_class):
v = [v]
setattr(self, k, v)
def _branch_lines(self):
"""Returns a list of line numbers that have more than one exit."""
return [l1 for l1,count in iitems(self.exit_counts) if count > 1]
def missing_formatted(self, branches=False):
"""The missing line numbers, formatted nicely.
Returns a string like "1-2, 5-11, 13-14".
If `branches` is true, includes the missing branch arcs also.
"""
if branches and self.has_arcs():
arcs = iitems(self.missing_branch_arcs())
else:
arcs = None
return format_lines(self.statements, self.missing, arcs=arcs)