Skip to content

gh-67565: Add tests for the remove redundant C-contiguity checks #110951

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 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ def test_base64_roundtrip(self, binary, newline):
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):
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_capi/test_getargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -837,6 +839,8 @@ def test_y_star(self):
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
Expand All @@ -846,6 +850,9 @@ def test_y_hash(self):
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
Expand All @@ -861,6 +868,8 @@ def test_w_star(self):
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):
Expand Down Expand Up @@ -893,6 +902,8 @@ def test_s_star(self):
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
Expand All @@ -902,6 +913,9 @@ def test_s_hash(self):
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
Expand All @@ -920,6 +934,8 @@ def test_z_star(self):
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
Expand All @@ -929,6 +945,9 @@ def test_z_hash(self):
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
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,10 @@ def test_buffer_types(self):
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()
Expand Down