Skip to content

gh-92886: fixed tests that were breaking while running with basic optimizations enabled (-O, assertions off) #93174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
51 changes: 28 additions & 23 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5690,45 +5690,48 @@ def test_joinable_queue(self):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be worth fixing the rest of the assertions in this file?

@classmethod
def _test_list(cls, obj):
assert obj[0] == 5
assert obj.count(5) == 1
assert obj.index(5) == 0
case = unittest.TestCase()
case.assertEqual(obj[0], 5)
case.assertEqual(obj.count(5), 1)
case.assertEqual(obj.index(5), 0)
obj.sort()
obj.reverse()
for x in obj:
pass
assert len(obj) == 1
assert obj.pop(0) == 5
case.assertEqual(len(obj), 1)
case.assertEqual(obj.pop(0), 5)

def test_list(self):
o = self.manager.list()
o.append(5)
self.run_worker(self._test_list, o)
assert not o
self.assertIsNotNone(o)
self.assertEqual(len(o), 0)

@classmethod
def _test_dict(cls, obj):
assert len(obj) == 1
assert obj['foo'] == 5
assert obj.get('foo') == 5
assert list(obj.items()) == [('foo', 5)]
assert list(obj.keys()) == ['foo']
assert list(obj.values()) == [5]
assert obj.copy() == {'foo': 5}
assert obj.popitem() == ('foo', 5)
case = unittest.TestCase()
case.assertEqual(len(obj), 1)
case.assertEqual(obj['foo'], 5)
case.assertEqual(obj.get('foo'), 5)
case.assertListEqual(list(obj.items()), [('foo', 5)])
case.assertListEqual(list(obj.keys()), ['foo'])
case.assertListEqual(list(obj.values()), [5])
case.assertDictEqual(obj.copy(), {'foo': 5})
case.assertTupleEqual(obj.popitem(), ('foo', 5))

def test_dict(self):
o = self.manager.dict()
o['foo'] = 5
self.run_worker(self._test_dict, o)
assert not o
self.assertIsNotNone(o)
self.assertEqual(len(o), 0)

@classmethod
def _test_value(cls, obj):
assert obj.value == 1
assert obj.get() == 1
case = unittest.TestCase()
case.assertEqual(obj.value, 1)
case.assertEqual(obj.get(), 1)
obj.set(2)

def test_value(self):
Expand All @@ -5739,19 +5742,21 @@ def test_value(self):

@classmethod
def _test_array(cls, obj):
assert obj[0] == 0
assert obj[1] == 1
assert len(obj) == 2
assert list(obj) == [0, 1]
case = unittest.TestCase()
case.assertEqual(obj[0], 0)
case.assertEqual(obj[1], 1)
case.assertEqual(len(obj), 2)
case.assertListEqual(list(obj), [0, 1])

def test_array(self):
o = self.manager.Array('i', [0, 1])
self.run_worker(self._test_array, o)

@classmethod
def _test_namespace(cls, obj):
assert obj.x == 0
assert obj.y == 1
case = unittest.TestCase()
case.assertEqual(obj.x, 0)
case.assertEqual(obj.y, 1)

def test_namespace(self):
o = self.manager.Namespace()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ async def __aexit__(self, *exc):

async def func():
async with CM():
assert (1, ) == 1
self.assertEqual((1, ), 1)

with self.assertRaises(AssertionError):
run_async(func())
Expand Down
7 changes: 1 addition & 6 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,11 +1041,6 @@ def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
return output.getvalue()


if sys.flags.optimize:
code_info_consts = "0: None"
else:
code_info_consts = "0: 'Formatted details of methods, functions, or code.'"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing tests were:

image

It seemed as though this condition was not necessary anymore, though I'm unsure if it is the expected behaviour.

code_info_code_info = f"""\
Name: code_info
Filename: (.*)
Expand All @@ -1056,7 +1051,7 @@ def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Stack size: \\d+
Flags: OPTIMIZED, NEWLOCALS
Constants:
{code_info_consts}
0: 'Formatted details of methods, functions, or code.'
Names:
0: _format_code_info
1: _get_code_object
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ def test_with_statement_logout(self):

@threading_helper.reap_threads
@cpython_only
@unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
Copy link
Contributor Author

@jackh-ncl jackh-ncl May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_mesg() @ Lib/imaplib.py:1247 is defined within a if __debug__ block, so I assume this test can be skipped when running with optimizations.

def test_dump_ur(self):
# See: http://bugs.python.org/issue26543
untagged_resp_dict = {'READ-WRITE': [b'']}
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,12 @@ def pycompilecmd(self, *args, **kwargs):
# assert_python_* helpers don't return proc object. We'll just use
# subprocess.run() instead of spawn_python() and its friends to test
# stdin support of the CLI.
opts = '-m' if __debug__ else '-Om'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this actually fixed things, but it seems to have done the trick 🤷‍♂️

if args and args[0] == '-' and 'input' in kwargs:
return subprocess.run([sys.executable, '-m', 'py_compile', '-'],
return subprocess.run([sys.executable, opts, 'py_compile', '-'],
input=kwargs['input'].encode(),
capture_output=True)
return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs)
return script_helper.assert_python_ok(opts, 'py_compile', *args, **kwargs)

def pycompilecmd_failure(self, *args):
return script_helper.assert_python_failure('-m', 'py_compile', *args)
Expand Down
10 changes: 4 additions & 6 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,8 @@ def func():
(5, 'line'),
(6, 'line'),
(7, 'line'),
(10, 'line'),
(13, 'line'),
(13, 'return')])
(10, 'line')] +
([(13, 'line'), (13, 'return')] if __debug__ else [(10, 'return')]))
Copy link
Contributor Author

@jackh-ncl jackh-ncl May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertions on line 819 affect the line numbers we are testing here when running with optimizations.
An alternative could be for these test functions to do something other than an assert.


def test_continue_through_finally(self):

Expand Down Expand Up @@ -870,9 +869,8 @@ def func():
(6, 'line'),
(7, 'line'),
(10, 'line'),
(3, 'line'),
(13, 'line'),
(13, 'return')])
(3, 'line')] +
([(13, 'line'), (13, 'return')] if __debug__ else [(3, 'return')]))

def test_return_through_finally(self):

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,8 @@ def test(val):
sys.path.insert(0, TEMP_ZIP)
mod = importlib.import_module(TESTMOD)
self.assertEqual(mod.test(1), 1)
self.assertRaises(AssertionError, mod.test, False)
if __debug__:
self.assertRaises(AssertionError, mod.test, False)

def testImport_WithStuff(self):
# try importing from a zipfile which contains additional
Expand Down
9 changes: 6 additions & 3 deletions Lib/wsgiref/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ def start_response(self, status, headers,exc_info=None):
self.status = status
self.headers = self.headers_class(headers)
status = self._convert_string_type(status, "Status")
assert len(status)>=4,"Status must be at least 4 characters"
assert status[:3].isdigit(), "Status message must begin w/3-digit code"
assert status[3]==" ", "Status message must have a space after code"
if len(status) < 4:
raise AssertionError("Status must be at least 4 characters")
if not status[:3].isdigit():
raise AssertionError("Status message must begin w/3-digit code")
if status[3] != " ":
raise AssertionError("Status message must have a space after code")

if __debug__:
for name, val in headers:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed tests that were breaking while running with basic optimizations enabled (-O, assertions off).
4 changes: 2 additions & 2 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ def permute_optional_groups(left, required, right):
required = tuple(required)
result = []

if not required:
assert not left
if not required and left:
raise AssertionError('If required is empty, left must also be empty.')

accumulator = []
counts = set()
Expand Down