-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Update test_bytes.py from CPython v3.11.2 #4746
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -704,6 +704,8 @@ def test_rindex(self): | |
self.assertEqual(b.rindex(i, 3, 9), 7) | ||
self.assertRaises(ValueError, b.rindex, w, 1, 3) | ||
|
||
# TODO: RUSTPYTHON | ||
@unittest.expectedFailure | ||
def test_mod(self): | ||
b = self.type2test(b'hello, %b!') | ||
orig = b | ||
|
@@ -721,6 +723,24 @@ def test_mod(self): | |
self.assertEqual(b, b'hello,\x00world!') | ||
self.assertIs(type(b), self.type2test) | ||
|
||
def check(fmt, vals, result): | ||
b = self.type2test(fmt) | ||
b = b % vals | ||
self.assertEqual(b, result) | ||
self.assertIs(type(b), self.type2test) | ||
|
||
# A set of tests adapted from test_unicode:UnicodeTest.test_formatting | ||
check(b'...%(foo)b...', {b'foo':b"abc"}, b'...abc...') | ||
check(b'...%(f(o)o)b...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...') | ||
check(b'...%(foo)b...', {b'foo':b"abc",b'def':123}, b'...abc...') | ||
check(b'%*b', (5, b'abc',), b' abc') | ||
check(b'%*b', (-5, b'abc',), b'abc ') | ||
check(b'%*.*b', (5, 2, b'abc',), b' ab') | ||
check(b'%*.*b', (5, 3, b'abc',), b' abc') | ||
check(b'%i %*.*b', (10, 5, 3, b'abc',), b'10 abc') | ||
check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc') | ||
check(b'%c', b'a', b'a') | ||
|
||
def test_imod(self): | ||
b = self.type2test(b'hello, %b!') | ||
orig = b | ||
|
@@ -991,6 +1011,18 @@ def test_sq_item(self): | |
class BytesTest(BaseBytesTest, unittest.TestCase): | ||
type2test = bytes | ||
|
||
def test__bytes__(self): | ||
foo = b'foo\x00bar' | ||
self.assertEqual(foo.__bytes__(), foo) | ||
self.assertEqual(type(foo.__bytes__()), self.type2test) | ||
|
||
class bytes_subclass(bytes): | ||
pass | ||
|
||
bar = bytes_subclass(b'bar\x00foo') | ||
self.assertEqual(bar.__bytes__(), bar) | ||
self.assertEqual(type(bar.__bytes__()), self.type2test) | ||
|
||
def test_getitem_error(self): | ||
b = b'python' | ||
msg = "byte indices must be integers or slices" | ||
|
@@ -1658,8 +1690,8 @@ def delslice(): | |
|
||
@test.support.cpython_only | ||
def test_obsolete_write_lock(self): | ||
from _testcapi import getbuffer_with_null_view | ||
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray()) | ||
_testcapi = import_helper.import_module('_testcapi') | ||
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray()) | ||
|
||
def test_iterator_pickling2(self): | ||
orig = bytearray(b'abc') | ||
|
@@ -1718,6 +1750,23 @@ def test_repeat_after_setslice(self): | |
self.assertEqual(b1, b) | ||
self.assertEqual(b3, b'xcxcxc') | ||
|
||
def test_mutating_index(self): | ||
class Boom: | ||
def __index__(self): | ||
b.clear() | ||
return 0 | ||
|
||
with self.subTest("tp_as_mapping"): | ||
b = bytearray(b'Now you see me...') | ||
with self.assertRaises(IndexError): | ||
b[0] = Boom() | ||
|
||
with self.subTest("tp_as_sequence"): | ||
_testcapi = import_helper.import_module('_testcapi') | ||
b = bytearray(b'Now you see me...') | ||
with self.assertRaises(IndexError): | ||
_testcapi.sequence_setitem(b, 0, Boom()) | ||
|
||
|
||
class AssortedBytesTest(unittest.TestCase): | ||
# | ||
|
@@ -1945,31 +1994,35 @@ def test_join(self): | |
s3 = s1.join([b"abcd"]) | ||
self.assertIs(type(s3), self.basetype) | ||
|
||
@unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youknowone This should have been broken out into a separate commit. |
||
def test_pickle(self): | ||
a = self.type2test(b"abcd") | ||
a.x = 10 | ||
a.y = self.type2test(b"efgh") | ||
a.z = self.type2test(b"efgh") | ||
for proto in range(pickle.HIGHEST_PROTOCOL + 1): | ||
b = pickle.loads(pickle.dumps(a, proto)) | ||
self.assertNotEqual(id(a), id(b)) | ||
self.assertEqual(a, b) | ||
self.assertEqual(a.x, b.x) | ||
self.assertEqual(a.y, b.y) | ||
self.assertEqual(a.z, b.z) | ||
self.assertEqual(type(a), type(b)) | ||
self.assertEqual(type(a.y), type(b.y)) | ||
self.assertEqual(type(a.z), type(b.z)) | ||
self.assertFalse(hasattr(b, 'y')) | ||
|
||
@unittest.skip("TODO: RUSTPYHON, Fails on ByteArraySubclassWithSlotsTest") | ||
def test_copy(self): | ||
a = self.type2test(b"abcd") | ||
a.x = 10 | ||
a.y = self.type2test(b"efgh") | ||
a.z = self.type2test(b"efgh") | ||
for copy_method in (copy.copy, copy.deepcopy): | ||
b = copy_method(a) | ||
self.assertNotEqual(id(a), id(b)) | ||
self.assertEqual(a, b) | ||
self.assertEqual(a.x, b.x) | ||
self.assertEqual(a.y, b.y) | ||
self.assertEqual(a.z, b.z) | ||
self.assertEqual(type(a), type(b)) | ||
self.assertEqual(type(a.y), type(b.y)) | ||
self.assertEqual(type(a.z), type(b.z)) | ||
self.assertFalse(hasattr(b, 'y')) | ||
|
||
def test_fromhex(self): | ||
b = self.type2test.fromhex('1a2B30') | ||
|
@@ -2002,6 +2055,9 @@ def __init__(me, *args, **kwargs): | |
class ByteArraySubclass(bytearray): | ||
pass | ||
|
||
class ByteArraySubclassWithSlots(bytearray): | ||
__slots__ = ('x', 'y', '__dict__') | ||
|
||
class BytesSubclass(bytes): | ||
pass | ||
|
||
|
@@ -2022,6 +2078,9 @@ def __init__(me, newarg=1, *args, **kwargs): | |
x = subclass(newarg=4, source=b"abcd") | ||
self.assertEqual(x, b"abcd") | ||
|
||
class ByteArraySubclassWithSlotsTest(SubclassTest, unittest.TestCase): | ||
basetype = bytearray | ||
type2test = ByteArraySubclassWithSlots | ||
|
||
class BytesSubclassTest(SubclassTest, unittest.TestCase): | ||
basetype = bytes | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowone This should have been broken out into a separate commit.