Issue #22668: Ensure that format strings survive slicing after casting. · pythoncapi/cpython@fa5d6a5 · GitHub
Skip to content

Commit fa5d6a5

Browse files
committed
Issue python#22668: Ensure that format strings survive slicing after casting.
1 parent 2934262 commit fa5d6a5

3 files changed

Lines changed: 73 additions & 6 deletions

File tree

Include/memoryobject.h

Lines changed: 2 additions & 2 deletions

Lib/test/test_memoryview.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,25 @@ def test_reversed(self):
360360
self.assertEqual(list(reversed(m)), aslist)
361361
self.assertEqual(list(reversed(m)), list(m[::-1]))
362362

363+
def test_issue22668(self):
364+
m = memoryview(bytes(range(8)))
365+
b = m.cast('H')
366+
c = b[0:2]
367+
d = memoryview(b)
368+
369+
del b
370+
371+
self.assertEqual(c[0], 256)
372+
self.assertEqual(d[0], 256)
373+
self.assertEqual(c.format, "H")
374+
self.assertEqual(d.format, "H")
375+
376+
_ = m.cast('I')
377+
self.assertEqual(c[0], 256)
378+
self.assertEqual(d[0], 256)
379+
self.assertEqual(c.format, "H")
380+
self.assertEqual(d.format, "H")
381+
363382

364383
# Variations on source objects for the buffer: bytes-like objects, then arrays
365384
# with itemsize > 1.

Objects/memoryobject.c

Lines changed: 52 additions & 4 deletions

0 commit comments

Comments
 (0)