-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathSideEffectInAssert.qhelp
More file actions
37 lines (27 loc) · 1.24 KB
/
SideEffectInAssert.qhelp
File metadata and controls
37 lines (27 loc) · 1.24 KB
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>All code defined in assert statements is ignored when optimization is
requested, that is, the program is run with the <code>-O</code> flag.
If an assert statement has any side-effects then the behavior of
the program changes when optimization is requested.</p>
</overview>
<recommendation>
<p>Move all expressions with side-effects out of assert statements.</p>
</recommendation>
<example>
<p>
In the example, the exit code from <code>subprocess.call()</code> is checked against 0, but the entire
expression is called from within an <code>assert</code> statement. If the code is ever run, then the
not only the assertion itself, but also the external call, will be discarded. It is better to save the result
of <code>subprocess.call()</code> to a temporary variable, and to assert that variable to be 0.
</p>
<sample src="SideEffectInAssert.py" />
</example>
<references>
<li>Python Language Reference: <a href="http://docs.python.org/2/reference/simple_stmts.html#assert">The assert statement</a>.</li>
<li>TutorialsPoint, Python Programming: <a href="http://www.tutorialspoint.com/python/assertions_in_python.htm">Assertions in Python</a>.</li>
</references>
</qhelp>