eric6/DebugClients/Python/coverage/python.py

changeset 7427
362cd1b6f81a
parent 6942
2602857055c5
equal deleted inserted replaced
7426:dc171b1d8261 7427:362cd1b6f81a
1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt 2 # For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
3 3
4 """Python source expertise for coverage.py""" 4 """Python source expertise for coverage.py"""
5 5
6 import os.path 6 import os.path
7 import types 7 import types
95 return data 95 return data
96 return None 96 return None
97 97
98 98
99 def source_for_file(filename): 99 def source_for_file(filename):
100 """Return the source file for `filename`. 100 """Return the source filename for `filename`.
101 101
102 Given a file name being traced, return the best guess as to the source 102 Given a file name being traced, return the best guess as to the source
103 file to attribute it to. 103 file to attribute it to.
104 104
105 """ 105 """
127 127
128 # No idea, just use the file name as-is. 128 # No idea, just use the file name as-is.
129 return filename 129 return filename
130 130
131 131
132 def source_for_morf(morf):
133 """Get the source filename for the module-or-file `morf`."""
134 if hasattr(morf, '__file__') and morf.__file__:
135 filename = morf.__file__
136 elif isinstance(morf, types.ModuleType):
137 # A module should have had .__file__, otherwise we can't use it.
138 # This could be a PEP-420 namespace package.
139 raise CoverageException("Module {} has no file".format(morf))
140 else:
141 filename = morf
142
143 filename = source_for_file(files.unicode_filename(filename))
144 return filename
145
146
132 class PythonFileReporter(FileReporter): 147 class PythonFileReporter(FileReporter):
133 """Report support for a Python file.""" 148 """Report support for a Python file."""
134 149
135 def __init__(self, morf, coverage=None): 150 def __init__(self, morf, coverage=None):
136 self.coverage = coverage 151 self.coverage = coverage
137 152
138 if hasattr(morf, '__file__') and morf.__file__: 153 filename = source_for_morf(morf)
139 filename = morf.__file__
140 elif isinstance(morf, types.ModuleType):
141 # A module should have had .__file__, otherwise we can't use it.
142 # This could be a PEP-420 namespace package.
143 raise CoverageException("Module {0} has no file".format(morf))
144 else:
145 filename = morf
146
147 filename = source_for_file(files.unicode_filename(filename))
148 154
149 super(PythonFileReporter, self).__init__(files.canonical_filename(filename)) 155 super(PythonFileReporter, self).__init__(files.canonical_filename(filename))
150 156
151 if hasattr(morf, '__name__'): 157 if hasattr(morf, '__name__'):
152 name = morf.__name__.replace(".", os.sep) 158 name = morf.__name__.replace(".", os.sep)
158 name = files.relative_filename(filename) 164 name = files.relative_filename(filename)
159 self.relname = name 165 self.relname = name
160 166
161 self._source = None 167 self._source = None
162 self._parser = None 168 self._parser = None
163 self._statements = None
164 self._excluded = None 169 self._excluded = None
165 170
166 def __repr__(self): 171 def __repr__(self):
167 return "<PythonFileReporter {0!r}>".format(self.filename) 172 return "<PythonFileReporter {!r}>".format(self.filename)
168 173
169 @contract(returns='unicode') 174 @contract(returns='unicode')
170 def relative_filename(self): 175 def relative_filename(self):
171 return self.relname 176 return self.relname
172 177

eric ide

mercurial