From 0f8ae9bcc88dc859927f9f7ad7830892ec8029f5 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 11:44:08 +0100 Subject: [PATCH 01/13] Fixed clinic and dis test --- Lib/test/test_dis.py | 7 +------ Tools/clinic/clinic.py | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index a19cf49e658ec5..e42bdac4e40330 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -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.'" - code_info_code_info = f"""\ Name: code_info Filename: (.*) @@ -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 diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 53e29df8a8e40f..da4371193cf470 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -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() From 6afde9405370466e529e15a3f9ff23f58e1d2e55 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 11:44:54 +0100 Subject: [PATCH 02/13] Fixed coroutines test --- Lib/test/test_coroutines.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index dba5ceffaf1c03..fb42f22f3efd16 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -1279,7 +1279,8 @@ async def __aexit__(self, *exc): async def func(): async with CM(): - assert (1, ) == 1 + if (1, ) != 1: + raise AssertionError with self.assertRaises(AssertionError): run_async(func()) From 0d71b7a3e6b3201a334c7734a9db39b1d99dad88 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 12:17:11 +0100 Subject: [PATCH 03/13] Skipped test_imaplib.py ThreadedNetworkedTests.test_dump_ur if not in debug mode --- Lib/test/test_imaplib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index ff13edea2d1e4a..ed26fa16d6ea8b 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -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") def test_dump_ur(self): # See: http://bugs.python.org/issue26543 untagged_resp_dict = {'READ-WRITE': [b'']} From f515d9f56a2d5fbc39ceedff6ffb4458955e1b0c Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 12:37:20 +0100 Subject: [PATCH 04/13] fix test_multiprocessing_forkserver.py test --- Lib/test/_test_multiprocessing.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 67bb17c0ede364..d9daecf4a0afcc 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5690,21 +5690,22 @@ def test_joinable_queue(self): @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 From ff6b191fd9a2300f707e84e75ebf6e18da59a069 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 12:39:47 +0100 Subject: [PATCH 05/13] Using self.assertEqual in test_coroutines.py --- Lib/test/test_coroutines.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index fb42f22f3efd16..4f97f5668da904 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -1279,8 +1279,7 @@ async def __aexit__(self, *exc): async def func(): async with CM(): - if (1, ) != 1: - raise AssertionError + self.assertEqual((1, ), 1) with self.assertRaises(AssertionError): run_async(func()) From 39261656fe11db97272463aa913a5f23deeb88f3 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 12:48:58 +0100 Subject: [PATCH 06/13] Brining other assertions in line in _test_multiprocessing.py --- Lib/test/_test_multiprocessing.py | 38 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index d9daecf4a0afcc..e00538f59196e6 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5710,26 +5710,28 @@ def test_list(self): @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): @@ -5740,10 +5742,11 @@ 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]) @@ -5751,8 +5754,9 @@ def test_array(self): @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() From 47a0eb1afc71d16c74978b75820ce211d01da9af Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 14:15:30 +0100 Subject: [PATCH 07/13] Fixed test_py_compile.py broken test --- Lib/test/test_py_compile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py index 794d6436b61ab0..5617315deb3191 100644 --- a/Lib/test/test_py_compile.py +++ b/Lib/test/test_py_compile.py @@ -236,7 +236,8 @@ def pycompilecmd(self, *args, **kwargs): # subprocess.run() instead of spawn_python() and its friends to test # stdin support of the CLI. if args and args[0] == '-' and 'input' in kwargs: - return subprocess.run([sys.executable, '-m', 'py_compile', '-'], + opts = '-m' if __debug__ else '-Om' + 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) From 1dcb59ea3889995bafdbf23ad605aae962a0c8fc Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 14:35:38 +0100 Subject: [PATCH 08/13] Fixed test_sys_settrace.py tests --- Lib/test/test_sys_settrace.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index 162fd8328582ca..917eebd2db1e3f 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -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')])) def test_continue_through_finally(self): @@ -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): From de758bba6b0abe8bf601748774e3fa8ae50b25b0 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 14:57:34 +0100 Subject: [PATCH 09/13] Fixed handlers.py tests --- Lib/wsgiref/handlers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py index 6623b700537cf9..d5ad0917da6d14 100644 --- a/Lib/wsgiref/handlers.py +++ b/Lib/wsgiref/handlers.py @@ -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 not 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: From afb2620d8ba8b23f631fd2ea8e47b0f8b83a261a Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 14:59:09 +0100 Subject: [PATCH 10/13] Fixed other test_py_compile.py test --- Lib/test/test_py_compile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py index 5617315deb3191..437ba113f482be 100644 --- a/Lib/test/test_py_compile.py +++ b/Lib/test/test_py_compile.py @@ -235,12 +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' if args and args[0] == '-' and 'input' in kwargs: - opts = '-m' if __debug__ else '-Om' 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) From 6edf1837c966b9853758908d5ab1729fd07a3080 Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 15:11:59 +0100 Subject: [PATCH 11/13] Fixed test_zipimport.py test --- Lib/test/test_zipimport.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 66789262dd6ca1..14318bf1e811bb 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -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 From 932449fd83618c46e453b2841a7fba6ebe31801f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 24 May 2022 14:24:40 +0000 Subject: [PATCH 12/13] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tests/2022-05-24-14-24-39.gh-issue-92886.8FvvAB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2022-05-24-14-24-39.gh-issue-92886.8FvvAB.rst diff --git a/Misc/NEWS.d/next/Tests/2022-05-24-14-24-39.gh-issue-92886.8FvvAB.rst b/Misc/NEWS.d/next/Tests/2022-05-24-14-24-39.gh-issue-92886.8FvvAB.rst new file mode 100644 index 00000000000000..b8a5b20ff489e7 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2022-05-24-14-24-39.gh-issue-92886.8FvvAB.rst @@ -0,0 +1 @@ +Fixed tests that were breaking while running with basic optimizations enabled (-O, assertions off). From 84563b0d2a045a5f9a3aad32881803cec5ef846f Mon Sep 17 00:00:00 2001 From: Jack Hindmarch Date: Tue, 24 May 2022 15:54:55 +0100 Subject: [PATCH 13/13] Fix condition in handlers.py --- Lib/wsgiref/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py index d5ad0917da6d14..9ff6aeee060ea9 100644 --- a/Lib/wsgiref/handlers.py +++ b/Lib/wsgiref/handlers.py @@ -237,7 +237,7 @@ 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") - if not len(status) >= 4: + 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")