Skip to content

Commit 48ad238

Browse files
authored
Update some tests to 3.14.5 (#7909)
* Align patches for `test_class.py` * Update more tests to 3.14.5 * Update `test_math` * `test_hash.py` * Update `test_grammar.py` * Fix failing test * Restore `test_grammar.py`
1 parent 67e66bd commit 48ad238

8 files changed

Lines changed: 65 additions & 29 deletions

File tree

Lib/test/test_class.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,14 +887,14 @@ class VarSizedSubclass(tuple):
887887

888888
class TestInlineValues(unittest.TestCase):
889889

890-
@unittest.expectedFailure # TODO: RUSTPYTHON; NameError: name 'has_inline_values' is not defined.
890+
@unittest.expectedFailure # TODO: RUSTPYTHON; NameError: name 'has_inline_values' is not defined.
891891
def test_no_flags_for_slots_class(self):
892892
flags = NoManagedDict.__flags__
893893
self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, 0)
894894
self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, 0)
895895
self.assertFalse(has_inline_values(NoManagedDict()))
896896

897-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: 0 != 4
897+
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: 0 != 4
898898
def test_both_flags_for_regular_class(self):
899899
for cls in (Plain, WithAttrs):
900900
with self.subTest(cls=cls.__name__):
@@ -903,7 +903,7 @@ def test_both_flags_for_regular_class(self):
903903
self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, Py_TPFLAGS_INLINE_VALUES)
904904
self.assertTrue(has_inline_values(cls()))
905905

906-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: 0 != 4
906+
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: 0 != 4
907907
def test_managed_dict_only_for_varsized_subclass(self):
908908
flags = VarSizedSubclass.__flags__
909909
self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)
@@ -1056,6 +1056,5 @@ def __init__(self):
10561056
self.assertFalse(out, msg=out.decode('utf-8'))
10571057
self.assertFalse(err, msg=err.decode('utf-8'))
10581058

1059-
10601059
if __name__ == '__main__':
10611060
unittest.main()

Lib/test/test_hash.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ def get_expected_hash(self, position, length):
263263
platform = 3 if IS_64BIT else 2
264264
return self.known_hashes[algorithm][position][platform]
265265

266-
# TODO: RUSTPYTHON
267-
@unittest.expectedFailure
266+
@unittest.expectedFailure # TODO: RUSTPYTHON
268267
def test_null_hash(self):
269268
# PYTHONHASHSEED=0 disables the randomized hash
270269
known_hash_of_obj = self.get_expected_hash(0, 3)
@@ -275,17 +274,15 @@ def test_null_hash(self):
275274
# It can also be disabled by setting the seed to 0:
276275
self.assertEqual(self.get_hash(self.repr_, seed=0), known_hash_of_obj)
277276

278-
# TODO: RUSTPYTHON
279-
@unittest.expectedFailure
277+
@unittest.expectedFailure # TODO: RUSTPYTHON
280278
@skip_unless_internalhash
281279
def test_fixed_hash(self):
282280
# test a fixed seed for the randomized hash
283281
# Note that all types share the same values:
284282
h = self.get_expected_hash(1, 3)
285283
self.assertEqual(self.get_hash(self.repr_, seed=42), h)
286284

287-
# TODO: RUSTPYTHON
288-
@unittest.expectedFailure
285+
@unittest.expectedFailure # TODO: RUSTPYTHON
289286
@skip_unless_internalhash
290287
def test_long_fixed_hash(self):
291288
if self.repr_long is None:
@@ -304,8 +301,7 @@ class StrHashRandomizationTests(StringlikeHashRandomizationTests,
304301
def test_empty_string(self):
305302
self.assertEqual(hash(""), 0)
306303

307-
# TODO: RUSTPYTHON
308-
@unittest.expectedFailure
304+
@unittest.expectedFailure # TODO: RUSTPYTHON
309305
@skip_unless_internalhash
310306
def test_ucs2_string(self):
311307
h = self.get_expected_hash(3, 6)

Lib/test/test_import/__init__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
Py_GIL_DISABLED,
4444
no_rerun,
4545
force_not_colorized_test_class,
46+
catch_unraisable_exception
4647
)
4748
from test.support.import_helper import (
4849
forget, make_legacy_pyc, unlink, unload, ready_to_import,
@@ -1238,8 +1239,7 @@ def test_script_shadowing_stdlib_sys_path_modification(self):
12381239
stdout, stderr = popen.communicate()
12391240
self.assertRegex(stdout, expected_error)
12401241

1241-
# TODO: RUSTPYTHON: _imp.create_dynamic is for C extensions, not applicable
1242-
@unittest.skip("TODO: RustPython _imp.create_dynamic not implemented")
1242+
@unittest.expectedFailure # TODO: RUSTPYTHON; _imp.create_dynamic not implemented
12431243
def test_create_dynamic_null(self):
12441244
with self.assertRaisesRegex(ValueError, 'embedded null character'):
12451245
class Spec:
@@ -2544,6 +2544,32 @@ def test_disallowed_reimport(self):
25442544
excsnap = _interpreters.run_string(interpid, script)
25452545
self.assertIsNot(excsnap, None)
25462546

2547+
@requires_subinterpreters
2548+
def test_pyinit_function_raises_exception(self):
2549+
# gh-144601: PyInit functions that raised exceptions would cause a
2550+
# crash when imported from a subinterpreter.
2551+
import _testsinglephase
2552+
filename = _testsinglephase.__file__
2553+
script = f"""if True:
2554+
from test.test_import import import_extension_from_file
2555+
2556+
import_extension_from_file('_testsinglephase_raise_exception', {filename!r})"""
2557+
2558+
interp = _interpreters.create()
2559+
try:
2560+
with catch_unraisable_exception() as cm:
2561+
exception = _interpreters.run_string(interp, script)
2562+
unraisable = cm.unraisable
2563+
finally:
2564+
_interpreters.destroy(interp)
2565+
2566+
self.assertIsNotNone(exception)
2567+
self.assertIsNotNone(exception.type.__name__, "ImportError")
2568+
self.assertIsNotNone(exception.msg, "failed to import from subinterpreter due to exception")
2569+
self.assertIsNotNone(unraisable)
2570+
self.assertIs(unraisable.exc_type, RuntimeError)
2571+
self.assertEqual(str(unraisable.exc_value), "evil")
2572+
25472573

25482574
class TestSinglePhaseSnapshot(ModuleSnapshot):
25492575
"""A representation of a single-phase init module for testing.

Lib/test/test_math.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ def testAtanh(self):
324324
self.assertRaises(ValueError, math.atanh, NINF)
325325
self.assertTrue(math.isnan(math.atanh(NAN)))
326326

327+
@unittest.skipIf(sys.platform.startswith("sunos"),
328+
"skipping, see gh-138573")
327329
def testAtan2(self):
328330
self.assertRaises(TypeError, math.atan2)
329331
self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)

Lib/test/test_memoryview.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,28 @@ def test_array_assign(self):
581581
m[:] = new_a
582582
self.assertEqual(a, new_a)
583583

584+
def test_boolean_format(self):
585+
# Test '?' format (keep all the checks below for UBSan)
586+
# See github.com/python/cpython/issues/148390.
587+
588+
# m1a and m1b are equivalent to [False, True, False]
589+
m1a = memoryview(b'\0\2\0').cast('?')
590+
self.assertEqual(m1a.tolist(), [False, True, False])
591+
m1b = memoryview(b'\0\4\0').cast('?')
592+
self.assertEqual(m1b.tolist(), [False, True, False])
593+
self.assertEqual(m1a, m1b)
594+
595+
# m2a and m2b are equivalent to [True, True, True]
596+
m2a = memoryview(b'\1\3\5').cast('?')
597+
self.assertEqual(m2a.tolist(), [True, True, True])
598+
m2b = memoryview(b'\2\4\6').cast('?')
599+
self.assertEqual(m2b.tolist(), [True, True, True])
600+
self.assertEqual(m2a, m2b)
601+
602+
allbytes = bytes(range(256))
603+
allbytes = memoryview(allbytes).cast('?')
604+
self.assertEqual(allbytes.tolist(), [False] + [True] * 255)
605+
584606

585607
class BytesMemorySliceTest(unittest.TestCase,
586608
BaseMemorySliceTests, BaseBytesMemoryTests):

Lib/test/test_pep646_syntax.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,25 +312,17 @@
312312
>>> f4.__annotations__
313313
{'args': StarredB, 'arg1': <class 'int'>}
314314
315-
>>> def f5(*args: *b = (1,)): pass # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
315+
>>> def f5(*args: *b = (1,)): pass # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE
316316
Traceback (most recent call last):
317317
...
318318
SyntaxError: invalid syntax
319319
"""
320320

321321
__test__ = {'doctests' : doctests}
322322

323-
EXPECTED_FAILURE = doctest.register_optionflag('EXPECTED_FAILURE') # TODO: RUSTPYTHON
324-
class CustomOutputChecker(doctest.OutputChecker): # TODO: RUSTPYTHON
325-
def check_output(self, want, got, optionflags): # TODO: RUSTPYTHON
326-
if optionflags & EXPECTED_FAILURE: # TODO: RUSTPYTHON
327-
if want == got: # TODO: RUSTPYTHON
328-
return False # TODO: RUSTPYTHON
329-
return True # TODO: RUSTPYTHON
330-
return super().check_output(want, got, optionflags) # TODO: RUSTPYTHON
331-
332323
def load_tests(loader, tests, pattern):
333-
tests.addTest(doctest.DocTestSuite(checker=CustomOutputChecker())) # TODO: RUSTPYTHON
324+
from test.support.rustpython import DocTestChecker # TODO: RUSTPYTHON
325+
tests.addTest(doctest.DocTestSuite(checker=DocTestChecker())) # TODO: RUSTPYTHON
334326
return tests
335327

336328

Lib/test/test_pkg.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def mkhier(self, descr):
9494
def test_1(self):
9595
hier = [("t1", None), ("t1 __init__.py", "")]
9696
self.mkhier(hier)
97-
import t1
97+
import t1 # noqa: F401
9898

9999
def test_2(self):
100100
hier = [
@@ -124,7 +124,7 @@ def test_2(self):
124124

125125
from t2 import sub
126126
from t2.sub import subsub
127-
from t2.sub.subsub import spam
127+
from t2.sub.subsub import spam # noqa: F401
128128
self.assertEqual(sub.__name__, "t2.sub")
129129
self.assertEqual(subsub.__name__, "t2.sub.subsub")
130130
self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
@@ -190,7 +190,6 @@ def test_5(self):
190190
]
191191
self.mkhier(hier)
192192

193-
import t5
194193
s = """
195194
from t5 import *
196195
self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])

Lib/test/test_sort.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def test_safe_object_compare(self):
326326
for L in float_int_lists:
327327
check_against_PyObject_RichCompareBool(self, L)
328328

329-
@support.cpython_only # XXX RUSTPYTHON: added by us but it seems like an implementation detail
329+
@unittest.skip("TODO: RUSTPYTHON; not really a todo, it seems like an implementation detail")
330330
def test_unsafe_object_compare(self):
331331

332332
# This test is by ppperry. It ensures that unsafe_object_compare is

0 commit comments

Comments
 (0)