Skip to content

Commit 8fc2c39

Browse files
authored
Merge pull request RustPython#5122 from NakanoMiku39/main
Update test files from CPython v3.12.0
2 parents 4d6a180 + 460e983 commit 8fc2c39

File tree

7 files changed

+622
-36
lines changed

7 files changed

+622
-36
lines changed

Lib/test/test_bigaddrspace.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
These tests are meant to exercise that requests to create objects bigger
3+
than what the address space allows are properly met with an OverflowError
4+
(rather than crash weirdly).
5+
6+
Primarily, this means 32-bit builds with at least 2 GiB of available memory.
7+
You need to pass the -M option to regrtest (e.g. "-M 2.1G") for tests to
8+
be enabled.
9+
"""
10+
11+
from test import support
12+
from test.support import bigaddrspacetest, MAX_Py_ssize_t
13+
14+
import unittest
15+
import operator
16+
import sys
17+
18+
19+
class BytesTest(unittest.TestCase):
20+
21+
@bigaddrspacetest
22+
def test_concat(self):
23+
# Allocate a bytestring that's near the maximum size allowed by
24+
# the address space, and then try to build a new, larger one through
25+
# concatenation.
26+
try:
27+
x = b"x" * (MAX_Py_ssize_t - 128)
28+
self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
29+
finally:
30+
x = None
31+
32+
@bigaddrspacetest
33+
def test_optimized_concat(self):
34+
try:
35+
x = b"x" * (MAX_Py_ssize_t - 128)
36+
37+
with self.assertRaises(OverflowError) as cm:
38+
# this statement used a fast path in ceval.c
39+
x = x + b"x" * 128
40+
41+
with self.assertRaises(OverflowError) as cm:
42+
# this statement used a fast path in ceval.c
43+
x += b"x" * 128
44+
finally:
45+
x = None
46+
47+
@bigaddrspacetest
48+
def test_repeat(self):
49+
try:
50+
x = b"x" * (MAX_Py_ssize_t - 128)
51+
self.assertRaises(OverflowError, operator.mul, x, 128)
52+
finally:
53+
x = None
54+
55+
56+
class StrTest(unittest.TestCase):
57+
58+
unicodesize = 4
59+
60+
@bigaddrspacetest
61+
def test_concat(self):
62+
try:
63+
# Create a string that would fill almost the address space
64+
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
65+
# Unicode objects trigger MemoryError in case an operation that's
66+
# going to cause a size overflow is executed
67+
self.assertRaises(MemoryError, operator.add, x, x)
68+
finally:
69+
x = None
70+
71+
@bigaddrspacetest
72+
def test_optimized_concat(self):
73+
try:
74+
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
75+
76+
with self.assertRaises(MemoryError) as cm:
77+
# this statement uses a fast path in ceval.c
78+
x = x + x
79+
80+
with self.assertRaises(MemoryError) as cm:
81+
# this statement uses a fast path in ceval.c
82+
x += x
83+
finally:
84+
x = None
85+
86+
@bigaddrspacetest
87+
def test_repeat(self):
88+
try:
89+
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
90+
self.assertRaises(MemoryError, operator.mul, x, 2)
91+
finally:
92+
x = None
93+
94+
95+
if __name__ == '__main__':
96+
if len(sys.argv) > 1:
97+
support.set_memlimit(sys.argv[1])
98+
unittest.main()

Lib/test/test_bigmem.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,15 @@ def test_sort(self, size):
12751275
self.assertEqual(l[-10:], [5] * 10)
12761276

12771277

1278+
class DictTest(unittest.TestCase):
1279+
1280+
@bigmemtest(size=357913941, memuse=160)
1281+
def test_dict(self, size):
1282+
# https://github.com/python/cpython/issues/102701
1283+
d = dict.fromkeys(range(size))
1284+
d[size] = 1
1285+
1286+
12781287
if __name__ == '__main__':
12791288
if len(sys.argv) > 1:
12801289
support.set_memlimit(sys.argv[1])

Lib/test/test_bool.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def test_float(self):
4242
self.assertEqual(float(True), 1.0)
4343
self.assertIsNot(float(True), True)
4444

45+
def test_complex(self):
46+
self.assertEqual(complex(False), 0j)
47+
self.assertEqual(complex(False), False)
48+
self.assertEqual(complex(True), 1+0j)
49+
self.assertEqual(complex(True), True)
50+
51+
# TODO: RUSTPYTHON
52+
@unittest.expectedFailure
4553
def test_math(self):
4654
self.assertEqual(+False, 0)
4755
self.assertIsNot(+False, False)
@@ -54,8 +62,22 @@ def test_math(self):
5462
self.assertEqual(-True, -1)
5563
self.assertEqual(abs(True), 1)
5664
self.assertIsNot(abs(True), True)
57-
self.assertEqual(~False, -1)
58-
self.assertEqual(~True, -2)
65+
with self.assertWarns(DeprecationWarning):
66+
# We need to put the bool in a variable, because the constant
67+
# ~False is evaluated at compile time due to constant folding;
68+
# consequently the DeprecationWarning would be issued during
69+
# module loading and not during test execution.
70+
false = False
71+
self.assertEqual(~false, -1)
72+
with self.assertWarns(DeprecationWarning):
73+
# also check that the warning is issued in case of constant
74+
# folding at compile time
75+
self.assertEqual(eval("~False"), -1)
76+
with self.assertWarns(DeprecationWarning):
77+
true = True
78+
self.assertEqual(~true, -2)
79+
with self.assertWarns(DeprecationWarning):
80+
self.assertEqual(eval("~True"), -2)
5981

6082
self.assertEqual(False+2, 2)
6183
self.assertEqual(True+2, 3)
@@ -315,6 +337,26 @@ def __len__(self):
315337
return -1
316338
self.assertRaises(ValueError, bool, Eggs())
317339

340+
def test_interpreter_convert_to_bool_raises(self):
341+
class SymbolicBool:
342+
def __bool__(self):
343+
raise TypeError
344+
345+
class Symbol:
346+
def __gt__(self, other):
347+
return SymbolicBool()
348+
349+
x = Symbol()
350+
351+
with self.assertRaises(TypeError):
352+
if x > 0:
353+
msg = "x > 0 was true"
354+
else:
355+
msg = "x > 0 was false"
356+
357+
# This used to create negative refcounts, see gh-102250
358+
del x
359+
318360
def test_from_bytes(self):
319361
self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
320362
self.assertIs(bool.from_bytes(b'abcd', 'little'), True)

Lib/test/test_bufio.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
from test import support
32
from test.support import os_helper
43

54
import io # C implementation.

0 commit comments

Comments
 (0)