gh-67565: Add tests for C-contiguity checks (GH-110951)

This commit is contained in:
Furkan Onder 2023-10-20 01:09:57 +03:00 committed by GitHub
parent 677d4bc15e
commit 9376728ce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -474,6 +474,12 @@ class BinASCIITest(unittest.TestCase):
restored = binascii.a2b_base64(self.type2test(converted))
self.assertConversion(binary, converted, restored, newline=newline)
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

@ -153,6 +153,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):
@ -837,6 +839,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
@ -846,6 +850,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
@ -861,6 +868,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):
@ -893,6 +902,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
@ -902,6 +913,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_z(self):
from _testcapi import getargs_z
@ -920,6 +934,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
@ -929,6 +945,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

@ -1739,6 +1739,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()