@@ -14,16 +14,14 @@ def pytest_addoption(parser):
14
14
group .addoption ('--assert' ,
15
15
action = "store" ,
16
16
dest = "assertmode" ,
17
- choices = ("rewrite" , "reinterp" , " plain" ,),
17
+ choices = ("rewrite" , "plain" ,),
18
18
default = "rewrite" ,
19
19
metavar = "MODE" ,
20
- help = """control assertion debugging tools. 'plain'
21
- performs no assertion debugging. 'reinterp'
22
- reinterprets assert statements after they failed
23
- to provide assertion expression information.
24
- 'rewrite' (the default) rewrites assert
25
- statements in test modules on import to
26
- provide assert expression information. """ )
20
+ help = """Control assertion debugging tools. 'plain'
21
+ performs no assertion debugging. 'rewrite'
22
+ (the default) rewrites assert statements in
23
+ test modules on import to provide assert
24
+ expression information.""" )
27
25
28
26
29
27
def pytest_namespace ():
@@ -60,37 +58,21 @@ class AssertionState:
60
58
def __init__ (self , config , mode ):
61
59
self .mode = mode
62
60
self .trace = config .trace .root .get ("assertion" )
61
+ self .hook = None
63
62
64
63
65
- def install_importhook (config , mode ):
66
- if mode == "rewrite" :
67
- try :
68
- import ast # noqa
69
- except ImportError :
70
- mode = "reinterp"
71
- else :
72
- # Both Jython and CPython 2.6.0 have AST bugs that make the
73
- # assertion rewriting hook malfunction.
74
- if (sys .platform .startswith ('java' ) or
75
- sys .version_info [:3 ] == (2 , 6 , 0 )):
76
- mode = "reinterp"
77
-
78
- config ._assertstate = AssertionState (config , mode )
79
-
80
- _load_modules (mode )
81
- from _pytest .monkeypatch import MonkeyPatch
82
- m = MonkeyPatch ()
83
- config ._cleanup .append (m .undo )
84
- m .setattr (py .builtin .builtins , 'AssertionError' ,
85
- reinterpret .AssertionError ) # noqa
86
-
87
- hook = None
88
- if mode == "rewrite" :
89
- hook = rewrite .AssertionRewritingHook (config ) # noqa
90
- sys .meta_path .insert (0 , hook )
91
-
92
- config ._assertstate .hook = hook
93
- config ._assertstate .trace ("configured with mode set to %r" % (mode ,))
64
+ def install_importhook (config ):
65
+ """Try to install the rewrite hook, raise SystemError if it fails."""
66
+ # Both Jython and CPython 2.6.0 have AST bugs that make the
67
+ # assertion rewriting hook malfunction.
68
+ if (sys .platform .startswith ('java' ) or
69
+ sys .version_info [:3 ] == (2 , 6 , 0 )):
70
+ raise SystemError ('rewrite not supported' )
71
+
72
+ config ._assertstate = AssertionState (config , 'rewrite' )
73
+ config ._assertstate .hook = hook = rewrite .AssertionRewritingHook (config )
74
+ sys .meta_path .insert (0 , hook )
75
+ config ._assertstate .trace ('installed rewrite import hook' )
94
76
def undo ():
95
77
hook = config ._assertstate .hook
96
78
if hook is not None and hook in sys .meta_path :
@@ -169,13 +151,5 @@ def pytest_sessionfinish(session):
169
151
assertstate .hook .set_session (None )
170
152
171
153
172
- def _load_modules (mode ):
173
- """Lazily import assertion related code."""
174
- global rewrite , reinterpret
175
- from _pytest .assertion import reinterpret # noqa
176
- if mode == "rewrite" :
177
- from _pytest .assertion import rewrite # noqa
178
-
179
-
180
154
# Expose this plugin's implementation for the pytest_assertrepr_compare hook
181
155
pytest_assertrepr_compare = util .assertrepr_compare
0 commit comments