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

Add PyDict #15

Merged
merged 6 commits into from
Sep 4, 2023
Merged

Add PyDict #15

merged 6 commits into from
Sep 4, 2023

Conversation

gatesn
Copy link
Contributor

@gatesn gatesn commented Sep 2, 2023

No description provided.

/// Insert val into the dictionary p with a key of key.
/// The dictionary takes ownership of the value.
pub fn setOwnedItem(self: PyDict, key: py.PyObject, value: py.PyObject) !void {
defer value.decref();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me how this value continues to exist after you decref it at the end of this call? SetItem https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem says "This function does not steal a reference to val.". Can you explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally to the SetItem call both the key and value are incref'd - this is what it means by not stealing. If it were to steal, it wouldn't incref and it would assume it now owns the reference.

Because of this both the caller (us) and the dictionary have valid references to the object. So as the caller, we decref so the only reference remaining is that inside the dictionary.

@gatesn gatesn merged commit eb5c158 into develop Sep 4, 2023
@gatesn gatesn deleted the ngates/dicts branch September 4, 2023 08:43
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.

2 participants