Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-143672: Add more tests for struct.pack_into()#143901

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Changes fromall 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
77 changes: 42 additions & 35 deletionsLib/test/test_struct.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -433,56 +433,63 @@ def test_unpack_from(self):
self.assertEqual(s.unpack_from(buffer=test_string, offset=2),
(b'cd01',))

deftest_pack_into(self):
def_test_pack_into(self, pack_into):
test_string = b'Reykjavik rocks, eow!'
writable_buf = array.array('b', b' '*100)
fmt = '21s'
s = struct.Struct(fmt)
writable_buf = memoryview(array.array('b', b' '*100))

# Test without offset
s.pack_into(writable_buf, 0, test_string)
pack_into(writable_buf, 0, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)]
self.assertEqual(from_buf, test_string)

# Test with offset.
s.pack_into(writable_buf, 10, test_string)
pack_into(writable_buf, 10, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)

# Test with negative offset.
pack_into(writable_buf, -30, test_string)
from_buf = writable_buf.tobytes()[-30:-30+len(test_string)]
self.assertEqual(from_buf, test_string)

# Go beyond boundaries.
small_buf = array.array('b', b' '*10)
self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
test_string)
self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(small_buf, 0, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, 90, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, -10, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, 150, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, -150, test_string)

# Test invalid buffer.
self.assertRaises(TypeError, pack_into, b' '*100, 0, test_string)
self.assertRaises(TypeError, pack_into, ' '*100, 0, test_string)
self.assertRaises(TypeError, pack_into, [0]*100, 0, test_string)
self.assertRaises(TypeError, pack_into, None, 0, test_string)
self.assertRaises(TypeError, pack_into, writable_buf[::2], 0, test_string)
self.assertRaises(TypeError, pack_into, writable_buf[::-1], 0, test_string)

# Test bogus offset (issue bpo-3694)
with self.assertRaises(TypeError):
pack_into(writable_buf, None, test_string)
with self.assertRaises(TypeError):
pack_into(writable_buf, 0.0, test_string)
with self.assertRaises((IndexError, OverflowError)):
pack_into(writable_buf, 2**1000, test_string)
with self.assertRaises((IndexError, OverflowError)):
pack_into(writable_buf, -2**1000, test_string)

# Test bogus offset (issue 3694)
sb = small_buf
self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
None)
def test_pack_into(self):
s = struct.Struct('21s')
self._test_pack_into(s.pack_into)

def test_pack_into_fn(self):
test_string = b'Reykjavik rocks, eow!'
writable_buf = array.array('b', b' '*100)
fmt = '21s'
pack_into = lambda *args: struct.pack_into(fmt, *args)

# Test without offset.
pack_into(writable_buf, 0, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)]
self.assertEqual(from_buf, test_string)

# Test with offset.
pack_into(writable_buf, 10, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)

# Go beyond boundaries.
small_buf = array.array('b', b' '*10)
self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
test_string)
self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
test_string)
pack_into = lambda *args: struct.pack_into('21s', *args)
self._test_pack_into(pack_into)

def test_unpack_with_buffer(self):
# SF bug 1563759: struct.unpack doesn't support buffer protocol objects
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp