forked from catboost/catboost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewrite.py
72 lines (54 loc) · 1.98 KB
/
rewrite.py
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
from __future__ import absolute_import
from __future__ import print_function
import ast
from _pytest.assertion import rewrite
from __res import importer
class AssertionRewritingHook(rewrite.AssertionRewritingHook):
def find_module(self, name, path=None):
state = self.config._assertstate
if not self._should_rewrite(name, state):
return None
state.trace("find_module called for: %s" % name)
try:
if self.is_package(name):
return None
except ImportError:
return None
self._rewritten_names.add(name)
state.trace("rewriting %s" % name)
co = _rewrite_test(self.config, name)
if co is None:
# Probably a SyntaxError in the test.
return None
self.modules[name] = co, None
return self
def _should_rewrite(self, name, state):
if name.startswith("__tests__.") or name.endswith(".conftest"):
return True
return self._is_marked_for_rewrite(name, state)
def is_package(self, name):
return importer.is_package(name)
def get_source(self, name):
return importer.get_source(name)
def _rewrite_test(config, name):
"""Try to read and rewrite *fn* and return the code object."""
state = config._assertstate
source = importer.get_source(name)
if source is None:
return None
path = importer.get_filename(name)
try:
tree = ast.parse(source, filename=path)
except SyntaxError:
# Let this pop up again in the real import.
state.trace("failed to parse: %r" % (path,))
return None
rewrite.rewrite_asserts(tree, path, config)
try:
co = compile(tree, path, "exec", dont_inherit=True)
except SyntaxError:
# It's possible that this error is from some bug in the
# assertion rewriting, but I don't know of a fast way to tell.
state.trace("failed to compile: %r" % (path,))
return None
return co