summaryrefslogtreecommitdiff
path: root/Cython/Coverage.py
blob: 0db3ad7ec09b0e421b97cdbfac13959c2e9c677c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""
A Cython plugin for coverage.py

Requires the coverage package at least in version 4.0 (which added the plugin API).
"""

import re
import os.path
from collections import defaultdict

from coverage.plugin import CoveragePlugin, FileTracer, FileReporter  # requires coverage.py 4.0+
from coverage.files import FileLocator  # requires coverage.py 4.0+

from .Utils import find_root_package_dir, is_package_dir


from . import __version__


def _find_c_source(base_path):
    if os.path.exists(base_path + '.c'):
        c_file = base_path + '.c'
    elif os.path.exists(base_path + '.cpp'):
        c_file = base_path + '.cpp'
    else:
        c_file = None
    return c_file


def _find_dep_file_path(main_file, file_path):
    abs_path = os.path.abspath(file_path)
    if file_path.endswith('.pxi') and not os.path.exists(abs_path):
        # include files are looked up relative to the main source file
        pxi_file_path = os.path.join(os.path.dirname(main_file), file_path)
        if os.path.exists(pxi_file_path):
            abs_path = os.path.abspath(pxi_file_path)
    return abs_path


class Plugin(CoveragePlugin):
    _c_files_map = None

    def sys_info(self):
        return [('Cython version', __version__)]

    def file_tracer(self, filename):
        """
        Try to find a C source file for a file path found by the tracer.
        """
        if filename.startswith('<') or filename.startswith('memory:'):
            return None
        c_file = py_file = None
        filename = os.path.abspath(filename)
        if self._c_files_map and filename in self._c_files_map:
            c_file = self._c_files_map[filename][0]

        if c_file is None:
            c_file, py_file = self._find_source_files(filename)
            if not c_file:
                return None

            # parse all source file paths and lines from C file
            # to learn about all relevant source files right away (pyx/pxi/pxd)
            # FIXME: this might already be too late if the first executed line
            #        is not from the main .pyx file but a file with a different
            #        name than the .c file (which prevents us from finding the
            #        .c file)
            self._parse_lines(c_file, filename)

        return CythonModuleTracer(filename, py_file, c_file, self._c_files_map)

    def file_reporter(self, filename):
        if os.path.splitext(filename)[1].lower() not in ('.pyx', '.pxi', '.pxd'):
            return None  # let coverage.py handle it (e.g. .py files)

        filename = os.path.abspath(filename)
        if self._c_files_map and filename in self._c_files_map:
            c_file, rel_file_path, code, excluded = self._c_files_map[filename]
        else:
            c_file, _ = self._find_source_files(filename)
            if not c_file:
                return None  # unknown file
            rel_file_path, code, excluded = self._parse_lines(c_file, filename)
        return CythonModuleReporter(c_file, filename, rel_file_path, code, excluded)

    def _find_source_files(self, filename):
        basename, ext = os.path.splitext(filename)
        ext = ext.lower()
        if ext in ('.py', '.pyx', '.pxd', '.c', '.cpp'):
            pass
        elif ext in ('.so', '.pyd'):
            platform_suffix = re.search(r'[.]cpython-[0-9]+[a-z]*$', basename, re.I)
            if platform_suffix:
                basename = basename[:platform_suffix.start()]
        elif ext == '.pxi':
            # if we get here, it means that the first traced line of a Cython module was
            # not in the main module but in an include file, so try a little harder to
            # find the main source file
            self._find_c_source_files(os.path.dirname(filename), filename)
            if filename in self._c_files_map:
                return self._c_files_map[filename][0], None
        else:
            # none of our business
            return None, None

        c_file = filename if ext in ('.c', '.cpp') else _find_c_source(basename)
        if c_file is None:
            # a module "pkg/mod.so" can have a source file "pkg/pkg.mod.c"
            package_root = find_root_package_dir.uncached(filename)
            package_path = os.path.relpath(basename, package_root).split(os.path.sep)
            if len(package_path) > 1:
                test_basepath = os.path.join(os.path.dirname(filename), '.'.join(package_path))
                c_file = _find_c_source(test_basepath)

        py_source_file = None
        if c_file:
            py_source_file = os.path.splitext(c_file)[0] + '.py'
            if not os.path.exists(py_source_file):
                py_source_file = None

            try:
                with open(c_file, 'rb') as f:
                    if b'/* Generated by Cython ' not in f.read(30):
                        return None  # not a Cython file
            except (IOError, OSError):
                c_file = None

        return c_file, py_source_file

    def _find_c_source_files(self, dir_path, source_file):
        """
        Desperately parse all C files in the directory or its package parents
        (not re-descending) to find the (included) source file in one of them.
        """
        if not os.path.isdir(dir_path):
            return
        splitext = os.path.splitext
        for filename in os.listdir(dir_path):
            ext = splitext(filename)[1].lower()
            if ext in ('.c', '.cpp'):
                self._parse_lines(os.path.join(dir_path, filename), source_file)
                if source_file in self._c_files_map:
                    return
        # not found? then try one package up
        if is_package_dir(dir_path):
            self._find_c_source_files(os.path.dirname(dir_path), source_file)

    def _parse_lines(self, c_file, sourcefile):
        """
        Parse a Cython generated C/C++ source file and find the executable lines.
        Each executable line starts with a comment header that states source file
        and line number, as well as the surrounding range of source code lines.
        """
        match_source_path_line = re.compile(r' */[*] +"(.*)":([0-9]+)$').match
        match_current_code_line = re.compile(r' *[*] (.*) # <<<<<<+$').match
        match_comment_end = re.compile(r' *[*]/$').match

        code_lines = defaultdict(dict)
        max_line = defaultdict(int)
        filenames = set()
        with open(c_file) as lines:
            lines = iter(lines)
            for line in lines:
                match = match_source_path_line(line)
                if not match:
                    continue
                filename, lineno = match.groups()
                filenames.add(filename)
                lineno = int(lineno)
                max_line[filename] = max(max_line[filename], lineno)
                for comment_line in lines:
                    match = match_current_code_line(comment_line)
                    if match:
                        code_lines[filename][lineno] = match.group(1).rstrip()
                        break
                    elif match_comment_end(comment_line):
                        # unexpected comment format - false positive?
                        break

        excluded_lines = dict(
            (filename, set(range(1, max_line[filename] + 1)) - set(lines))
            for filename, lines in code_lines.iteritems()
        )

        if self._c_files_map is None:
            self._c_files_map = {}

        for filename in filenames:
            abs_path = _find_dep_file_path(c_file, filename)
            self._c_files_map[abs_path] = (
                c_file, filename, code_lines[filename], excluded_lines[filename])

        if sourcefile not in self._c_files_map:
            return (None,) * 3  # shouldn't happen ...
        return self._c_files_map[sourcefile][1:]


class CythonModuleTracer(FileTracer):
    """
    Find the Python/Cython source file for a Cython module.
    """
    def __init__(self, module_file, py_file, c_file, c_files_map):
        super(CythonModuleTracer, self).__init__()
        self.module_file = module_file
        self.py_file = py_file
        self.c_file = c_file
        self._c_files_map = c_files_map

    def has_dynamic_source_filename(self):
        return True

    def dynamic_source_filename(self, filename, frame):
        source_file = frame.f_code.co_filename
        abs_path = os.path.abspath(source_file)

        if self.py_file and source_file.lower().endswith('.py'):
            # always let coverage.py handle this case itself
            return self.py_file

        assert self._c_files_map is not None
        if abs_path not in self._c_files_map:
            self._c_files_map[abs_path] = (self.c_file, source_file, None, None)
        return abs_path


class CythonModuleReporter(FileReporter):
    """
    Provide detailed trace information for one source file to coverage.py.
    """
    file_locator = FileLocator()

    def __init__(self, c_file, source_file, rel_file_path, code, excluded):
        super(CythonModuleReporter, self).__init__(source_file)
        self.name = rel_file_path
        self.c_file = c_file
        self._code = code
        self._excluded = excluded

    def statements(self):
        return self._code.viewkeys()

    def excluded_statements(self):
        return self._excluded

    def _iter_source_lines(self):
        current_line = 1
        for line_no, code_line in sorted(self._code.iteritems()):
            while line_no > current_line:
                yield ''
                current_line += 1
            yield code_line
            current_line += 1

    def source(self):
        if os.path.exists(self.filename):
            with open(self.filename) as f:
                return f.read()
        else:
            return '\n'.join(self._iter_source_lines())

    def source_token_lines(self):
        if os.path.exists(self.filename):
            with open(self.filename) as f:
                for line in f:
                    yield [('txt', line)]
        else:
            for line in self._iter_source_lines():
                yield [('txt', line)]