mirror of
https://github.com/micropython/micropython.git
synced 2025-09-06 01:40:34 +02:00
The recently merged 5e9189d6d1
now allows
temporary slices to be allocated on the C stack, which is much better than
allocating them on the GC heap.
Unfortunately there are cases where the C-allocated slice can escape and be
retained as an object, which leads to crashes (because that object points
to the C stack which now has other values on it).
The fix here is to add a new `MP_TYPE_FLAG_SUBSCR_ALLOWS_STACK_SLICE`.
Native types should set this flag if their subscr method is guaranteed not
to hold on to a reference of the slice object.
Fixes issue #17733 (see also #17723).
Signed-off-by: Damien George <damien@micropython.org>
24 lines
675 B
Python
24 lines
675 B
Python
# Test that the slice-on-stack optimisation does not break various uses of slice
|
|
# (see MP_TYPE_FLAG_SUBSCR_ALLOWS_STACK_SLICE type option).
|
|
#
|
|
# Note: this test has a corresponding .py.exp file because hashing slice objects
|
|
# was not allowed in CPython until 3.12.
|
|
|
|
try:
|
|
from collections import OrderedDict
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
# Attempt to index with a slice, error should contain the slice (failed key).
|
|
try:
|
|
dict()[:]
|
|
except KeyError as e:
|
|
print("KeyError", e.args)
|
|
|
|
# Put a slice and another object into an OrderedDict, and retrieve them.
|
|
x = OrderedDict()
|
|
x[:"a"] = 1
|
|
x["b"] = 2
|
|
print(list(x.keys()), list(x.values()))
|