Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def source(self):
try:
with open(self.filename, 'rb') as f:
source = f.read()
try:
return source.decode('utf8')
except UnicodeDecodeError:
logger.debug('UnicodeDecodeError in %s for utf8. '
'Trying iso-8859-15.', self.filename)
return source.decode('iso-8859-15')
except FileNotFoundError as exc:
logger.warning('%s', exc)
raise coverage.misc.NoSource(str(exc))
except Exception as exc:
raise CoverageWrapperException(
'Could not read source for %s.' % self.filename, orig_exc=exc)
fname, fobj, fstr = get_fname_and_fobj_and_str(self.data_file)
try:
if fobj:
try:
read_fileobj = cov_data.read_fileobj
except AttributeError: # made private in coveragepy 5
read_fileobj = cov_data._read_fileobj
read_fileobj(fobj)
else:
try:
read_file = cov_data.read_file
except AttributeError: # made private in coveragepy 5
read_file = cov_data._read_file
read_file(fname)
except coverage.CoverageException as exc:
raise CoverageWrapperException(
'Coverage could not read data_file: %s' % fstr,
orig_exc=exc)
object.__setattr__(self, 'cov_data', cov_data)
def format_message(self):
"""Append information about original exception if any."""
msg = super(CoverageWrapperException, self).format_message()
if self.orig_exc:
return '%s (%s: %s)' % (
msg,
self.orig_exc.__class__.__name__,
self.orig_exc)
return msg
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except coverage.CoverageException as exc:
raise CoverageWrapperException('%s (%s)' % (
exc, exc.__class__.__name__))
return wrapper