Skip to content

Distinguish slice constants from runtime slice calls - #18

Open
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/slice-constant-normalization
Open

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/slice-constant-normalization

Conversation

@EiNSTeiN-

Copy link
Copy Markdown

I am opening this PR to distinguish marshalled slice constants from runtime slice calls. The following expressions must retain their different meanings:

def select(data, stop):
    return data[:stop, :]


def runtime_call(data, stop):
    return data[slice(None, stop, None), :]

The normalizer currently converts any suitably shaped call named slice into colon syntax. For the single-axis case, data[slice(None, stop, None)] becomes:

data[:stop]

That removes the global lookup and call, so a shadowed or effectful slice callable no longer runs.

Conversely, a tuple containing actual serialized slice constants needs recursive normalization into colon syntax inside a multi-axis subscript. Currently data[:, :] can become:

data[(slice(None, None, None), slice(None, None, None))]

This introduces runtime global lookups that the constant-index source does not perform.

The replacement normalizes genuine PyObject::Slice values and tuples containing them, while preserving runtime calls. Variations include data[:stop, :], data[:, :], data[1:stop:2, :, 7], and single- or multi-axis runtime slice arguments.

Serialized slice objects are supported by marshal format 5, introduced in Python 3.14. Preserving actual runtime calls also matters for older bytecode; it does not require serialized slice constants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant