Skip to content
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

Arrays of structs do not handle assigning a struct that's already in the array #73

Open
apple1417 opened this issue Feb 26, 2025 · 0 comments

Comments

@apple1417
Copy link
Contributor

apple1417 commented Feb 26, 2025

Original report was

# Original saves are [0, 1, 2, 3, 4]

# This version results in obj.SaveGames of [4, 3, 3, 4, 0]
obj.SaveGames = sorted(obj.SaveGames,
                       key=lambda x: x.LastSaveDate,
                       reverse=True)

Slightly more insidiously, this is also equivalent to obj.SaveGames.sort(key=lambda x: x.LastSaveDate, reverse=True), which you might expect to work better.

The root cause is structs being reference types, so when we update the first value in the array we're changing the value of whatever index it got sorted to. This is very unintuitive.

As a workaround, you can make a copy of each struct before assigning them:

obj.SaveGames = sorted((copy.copy(x) for x in obj.SaveGames),
                       key=lambda x: x.LastSaveDate,
                       reverse=True)
obj.SaveGames = copy.deepcopy(sorted(obj.SaveGames,
                              key=lambda x: x.LastSaveDate,
                              reverse=True))

This is a broader problem than just sorting though, any slice assignment might run into it

>>> arr = [1, 2, 3, 4, 5] # really a wrappedarray of structs
>>> arr[2:4] = (arr[3], arr[4], arr[3], 6)
>>> arr # should be
[1, 2, 4, 5, 4, 6, 5]

And then more generally, 2d arrays or arrays of multicast delegates, currently the other two reference types, would also be vulnerable.

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

No branches or pull requests

1 participant