Skip to content

Commit cb3c655

Browse files
committed
unittest: Optimise by only formatting msg if needed.
Note that using `if not expr: raise AssertionError(msg)` is pretty much equivalent in terms of bytecode to `assert expr, msg`. But the use of `raise AssertionError` is clearer and means that the code can now be compiled with -O3 and still function. Signed-off-by: Damien George <damien@micropython.org>
1 parent 42cb162 commit cb3c655

1 file changed

Lines changed: 52 additions & 40 deletions

File tree

python-stdlib/unittest/unittest/__init__.py

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __exit__(self, exc_type, exc_value, tb):
1919
return
2020
self.exception = exc_value
2121
if exc_type is None:
22-
assert False, "%r not raised" % self.expected
22+
raise AssertionError("%r not raised" % self.expected)
2323
if issubclass(exc_type, self.expected):
2424
# store exception for later retrieval
2525
self.exception = exc_value
@@ -78,27 +78,31 @@ def skipTest(self, reason):
7878
raise SkipTest(reason)
7979

8080
def fail(self, msg=""):
81-
assert False, msg
81+
raise AssertionError(msg)
8282

8383
def assertEqual(self, x, y, msg=""):
84-
if not msg:
85-
msg = "%r vs (expected) %r" % (x, y)
86-
assert x == y, msg
84+
if not x == y:
85+
if not msg:
86+
msg = "%r vs (expected) %r" % (x, y)
87+
raise AssertionError(msg)
8788

8889
def assertNotEqual(self, x, y, msg=""):
89-
if not msg:
90-
msg = "%r not expected to be equal %r" % (x, y)
91-
assert x != y, msg
90+
if not x != y:
91+
if not msg:
92+
msg = "%r not expected to be equal %r" % (x, y)
93+
raise AssertionError(msg)
9294

9395
def assertLessEqual(self, x, y, msg=None):
94-
if msg is None:
95-
msg = "%r is expected to be <= %r" % (x, y)
96-
assert x <= y, msg
96+
if not x <= y:
97+
if msg is None:
98+
msg = "%r is expected to be <= %r" % (x, y)
99+
raise AssertionError(msg)
97100

98101
def assertGreaterEqual(self, x, y, msg=None):
99-
if msg is None:
100-
msg = "%r is expected to be >= %r" % (x, y)
101-
assert x >= y, msg
102+
if not x >= y:
103+
if msg is None:
104+
msg = "%r is expected to be >= %r" % (x, y)
105+
raise AssertionError(msg)
102106

103107
def assertAlmostEqual(self, x, y, places=None, msg="", delta=None):
104108
if x == y:
@@ -119,7 +123,7 @@ def assertAlmostEqual(self, x, y, places=None, msg="", delta=None):
119123
if not msg:
120124
msg = "%r != %r within %r places" % (x, y, places)
121125

122-
assert False, msg
126+
raise AssertionError(msg)
123127

124128
def assertNotAlmostEqual(self, x, y, places=None, msg="", delta=None):
125129
if delta is not None and places is not None:
@@ -138,45 +142,53 @@ def assertNotAlmostEqual(self, x, y, places=None, msg="", delta=None):
138142
if not msg:
139143
msg = "%r == %r within %r places" % (x, y, places)
140144

141-
assert False, msg
145+
raise AssertionError(msg)
142146

143147
def assertIs(self, x, y, msg=""):
144-
if not msg:
145-
msg = "%r is not %r" % (x, y)
146-
assert x is y, msg
148+
if not x is y:
149+
if not msg:
150+
msg = "%r is not %r" % (x, y)
151+
raise AssertionError(msg)
147152

148153
def assertIsNot(self, x, y, msg=""):
149-
if not msg:
150-
msg = "%r is %r" % (x, y)
151-
assert x is not y, msg
154+
if not x is not y:
155+
if not msg:
156+
msg = "%r is %r" % (x, y)
157+
raise AssertionError(msg)
152158

153159
def assertIsNone(self, x, msg=""):
154-
if not msg:
155-
msg = "%r is not None" % x
156-
assert x is None, msg
160+
if not x is None:
161+
if not msg:
162+
msg = "%r is not None" % x
163+
raise AssertionError(msg)
157164

158165
def assertIsNotNone(self, x, msg=""):
159-
if not msg:
160-
msg = "%r is None" % x
161-
assert x is not None, msg
166+
if not x is not None:
167+
if not msg:
168+
msg = "%r is None" % x
169+
raise AssertionError(msg)
162170

163171
def assertTrue(self, x, msg=""):
164-
if not msg:
165-
msg = "Expected %r to be True" % x
166-
assert x, msg
172+
if not x:
173+
if not msg:
174+
msg = "Expected %r to be True" % x
175+
raise AssertionError(msg)
167176

168177
def assertFalse(self, x, msg=""):
169-
if not msg:
170-
msg = "Expected %r to be False" % x
171-
assert not x, msg
178+
if x:
179+
if not msg:
180+
msg = "Expected %r to be False" % x
181+
raise AssertionError(msg)
172182

173183
def assertIn(self, x, y, msg=""):
174-
if not msg:
175-
msg = "Expected %r to be in %r" % (x, y)
176-
assert x in y, msg
184+
if not x in y:
185+
if not msg:
186+
msg = "Expected %r to be in %r" % (x, y)
187+
raise AssertionError(msg)
177188

178189
def assertIsInstance(self, x, y, msg=""):
179-
assert isinstance(x, y), msg
190+
if not isinstance(x, y):
191+
raise AssertionError(msg)
180192

181193
def assertRaises(self, exc, func=None, *args, **kwargs):
182194
if func is None:
@@ -189,7 +201,7 @@ def assertRaises(self, exc, func=None, *args, **kwargs):
189201
return
190202
raise e
191203

192-
assert False, "%r not raised" % exc
204+
raise AssertionError("%r not raised" % exc)
193205

194206
def assertWarns(self, warn):
195207
return AssertRaisesContext(None)
@@ -225,7 +237,7 @@ def test_exp_fail(*args, **kwargs):
225237
except:
226238
pass
227239
else:
228-
assert False, "unexpected success"
240+
raise AssertionError("unexpected success")
229241

230242
return test_exp_fail
231243

0 commit comments

Comments
 (0)