[3.11] gh-67565: Add tests for C-contiguity checks (GH-110951) (GH-111199)

(cherry picked from commit 9376728ce4)
This commit is contained in:
Furkan Onder 2023-10-23 12:49:44 +03:00 committed by GitHub
parent 0a23960266
commit f446df741f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -428,6 +428,12 @@ class BinASCIITest(unittest.TestCase):
self.assertEqual(binascii.b2a_base64(b, newline=False),
b'aGVsbG8=')
def test_c_contiguity(self):
m = memoryview(bytearray(b'noncontig'))
noncontig_writable = m[::-2]
with self.assertRaises(BufferError):
binascii.b2a_hex(noncontig_writable)
class ArrayBinASCIITest(BinASCIITest):
def type2test(self, s):

View file

@ -152,6 +152,8 @@ class TupleSubclass(tuple):
class DictSubclass(dict):
pass
NONCONTIG_WRITABLE = memoryview(bytearray(b'noncontig'))[::-2]
NONCONTIG_READONLY = memoryview(b'noncontig')[::-2]
class Unsigned_TestCase(unittest.TestCase):
def test_b(self):
@ -836,6 +838,8 @@ class Bytes_TestCase(unittest.TestCase):
self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray')
self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview')
self.assertRaises(TypeError, getargs_y_star, None)
self.assertRaises(BufferError, getargs_y_star, NONCONTIG_WRITABLE)
self.assertRaises(BufferError, getargs_y_star, NONCONTIG_READONLY)
def test_y_hash(self):
from _testcapi import getargs_y_hash
@ -845,6 +849,9 @@ class Bytes_TestCase(unittest.TestCase):
self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview'))
self.assertRaises(TypeError, getargs_y_hash, None)
# TypeError: must be read-only bytes-like object, not memoryview
self.assertRaises(TypeError, getargs_y_hash, NONCONTIG_WRITABLE)
self.assertRaises(TypeError, getargs_y_hash, NONCONTIG_READONLY)
def test_w_star(self):
# getargs_w_star() modifies first and last byte
@ -860,6 +867,8 @@ class Bytes_TestCase(unittest.TestCase):
self.assertEqual(getargs_w_star(memoryview(buf)), b'[emoryvie]')
self.assertEqual(buf, bytearray(b'[emoryvie]'))
self.assertRaises(TypeError, getargs_w_star, None)
self.assertRaises(TypeError, getargs_w_star, NONCONTIG_WRITABLE)
self.assertRaises(TypeError, getargs_w_star, NONCONTIG_READONLY)
class String_TestCase(unittest.TestCase):
@ -892,6 +901,8 @@ class String_TestCase(unittest.TestCase):
self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray')
self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview')
self.assertRaises(TypeError, getargs_s_star, None)
self.assertRaises(BufferError, getargs_s_star, NONCONTIG_WRITABLE)
self.assertRaises(BufferError, getargs_s_star, NONCONTIG_READONLY)
def test_s_hash(self):
from _testcapi import getargs_s_hash
@ -901,6 +912,9 @@ class String_TestCase(unittest.TestCase):
self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview'))
self.assertRaises(TypeError, getargs_s_hash, None)
# TypeError: must be read-only bytes-like object, not memoryview
self.assertRaises(TypeError, getargs_s_hash, NONCONTIG_WRITABLE)
self.assertRaises(TypeError, getargs_s_hash, NONCONTIG_READONLY)
def test_s_hash_int(self):
# "s#" without PY_SSIZE_T_CLEAN defined.
@ -936,6 +950,8 @@ class String_TestCase(unittest.TestCase):
self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray')
self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview')
self.assertIsNone(getargs_z_star(None))
self.assertRaises(BufferError, getargs_z_star, NONCONTIG_WRITABLE)
self.assertRaises(BufferError, getargs_z_star, NONCONTIG_READONLY)
def test_z_hash(self):
from _testcapi import getargs_z_hash
@ -945,6 +961,9 @@ class String_TestCase(unittest.TestCase):
self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview'))
self.assertIsNone(getargs_z_hash(None))
# TypeError: must be read-only bytes-like object, not memoryview
self.assertRaises(TypeError, getargs_z_hash, NONCONTIG_WRITABLE)
self.assertRaises(TypeError, getargs_z_hash, NONCONTIG_READONLY)
def test_es(self):
from _testcapi import getargs_es

View file

@ -1952,6 +1952,10 @@ class MemoryBIOTests(unittest.TestCase):
self.assertEqual(bio.read(), b'bar')
bio.write(memoryview(b'baz'))
self.assertEqual(bio.read(), b'baz')
m = memoryview(bytearray(b'noncontig'))
noncontig_writable = m[::-2]
with self.assertRaises(BufferError):
bio.write(memoryview(noncontig_writable))
def test_error_types(self):
bio = ssl.MemoryBIO()