Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update test files from CPython v3.12.0 #5122

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

Merged
merged 10 commits into from
Nov 23, 2023
98 changes: 98 additions & 0 deletions Lib/test/test_bigaddrspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""
These tests are meant to exercise that requests to create objects bigger
than what the address space allows are properly met with an OverflowError
(rather than crash weirdly).

Primarily, this means 32-bit builds with at least 2 GiB of available memory.
You need to pass the -M option to regrtest (e.g. "-M 2.1G") for tests to
be enabled.
"""

from test import support
from test.support import bigaddrspacetest, MAX_Py_ssize_t

import unittest
import operator
import sys


class BytesTest(unittest.TestCase):

@bigaddrspacetest
def test_concat(self):
# Allocate a bytestring that's near the maximum size allowed by
# the address space, and then try to build a new, larger one through
# concatenation.
try:
x = b"x" * (MAX_Py_ssize_t - 128)
self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
finally:
x = None

@bigaddrspacetest
def test_optimized_concat(self):
try:
x = b"x" * (MAX_Py_ssize_t - 128)

with self.assertRaises(OverflowError) as cm:
# this statement used a fast path in ceval.c
x = x + b"x" * 128

with self.assertRaises(OverflowError) as cm:
# this statement used a fast path in ceval.c
x += b"x" * 128
finally:
x = None

@bigaddrspacetest
def test_repeat(self):
try:
x = b"x" * (MAX_Py_ssize_t - 128)
self.assertRaises(OverflowError, operator.mul, x, 128)
finally:
x = None


class StrTest(unittest.TestCase):

unicodesize = 4

@bigaddrspacetest
def test_concat(self):
try:
# Create a string that would fill almost the address space
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
# Unicode objects trigger MemoryError in case an operation that's
# going to cause a size overflow is executed
self.assertRaises(MemoryError, operator.add, x, x)
finally:
x = None

@bigaddrspacetest
def test_optimized_concat(self):
try:
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))

with self.assertRaises(MemoryError) as cm:
# this statement uses a fast path in ceval.c
x = x + x

with self.assertRaises(MemoryError) as cm:
# this statement uses a fast path in ceval.c
x += x
finally:
x = None

@bigaddrspacetest
def test_repeat(self):
try:
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
self.assertRaises(MemoryError, operator.mul, x, 2)
finally:
x = None


if __name__ == '__main__':
if len(sys.argv) > 1:
support.set_memlimit(sys.argv[1])
unittest.main()
9 changes: 9 additions & 0 deletions Lib/test/test_bigmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,15 @@ def test_sort(self, size):
self.assertEqual(l[-10:], [5] * 10)


class DictTest(unittest.TestCase):

@bigmemtest(size=357913941, memuse=160)
def test_dict(self, size):
# https://github.com/python/cpython/issues/102701
d = dict.fromkeys(range(size))
d[size] = 1


if __name__ == '__main__':
if len(sys.argv) > 1:
support.set_memlimit(sys.argv[1])
Expand Down
46 changes: 44 additions & 2 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def test_float(self):
self.assertEqual(float(True), 1.0)
self.assertIsNot(float(True), True)

def test_complex(self):
self.assertEqual(complex(False), 0j)
self.assertEqual(complex(False), False)
self.assertEqual(complex(True), 1+0j)
self.assertEqual(complex(True), True)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_math(self):
self.assertEqual(+False, 0)
self.assertIsNot(+False, False)
Expand All @@ -54,8 +62,22 @@ def test_math(self):
self.assertEqual(-True, -1)
self.assertEqual(abs(True), 1)
self.assertIsNot(abs(True), True)
self.assertEqual(~False, -1)
self.assertEqual(~True, -2)
with self.assertWarns(DeprecationWarning):
# We need to put the bool in a variable, because the constant
# ~False is evaluated at compile time due to constant folding;
# consequently the DeprecationWarning would be issued during
# module loading and not during test execution.
false = False
self.assertEqual(~false, -1)
with self.assertWarns(DeprecationWarning):
# also check that the warning is issued in case of constant
# folding at compile time
self.assertEqual(eval("~False"), -1)
with self.assertWarns(DeprecationWarning):
true = True
self.assertEqual(~true, -2)
with self.assertWarns(DeprecationWarning):
self.assertEqual(eval("~True"), -2)

self.assertEqual(False+2, 2)
self.assertEqual(True+2, 3)
Expand Down Expand Up @@ -315,6 +337,26 @@ def __len__(self):
return -1
self.assertRaises(ValueError, bool, Eggs())

def test_interpreter_convert_to_bool_raises(self):
class SymbolicBool:
def __bool__(self):
raise TypeError

class Symbol:
def __gt__(self, other):
return SymbolicBool()

x = Symbol()

with self.assertRaises(TypeError):
if x > 0:
msg = "x > 0 was true"
else:
msg = "x > 0 was false"

# This used to create negative refcounts, see gh-102250
del x

def test_from_bytes(self):
self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_bufio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from test import support
from test.support import os_helper

import io # C implementation.
Expand Down
Loading