Repository navigation
Conversation
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
mrkn
left a comment
There was a problem hiding this comment.
I confirmed a reference leak in the dict subclass iteration path; details are in the inline comment.
mrkn
left a comment
There was a problem hiding this comment.
One additional suggestion on matching Python's item-unpacking behavior.
mrkn
left a comment
There was a problem hiding this comment.
Suggested regression coverage for the key reference held during a failed value conversion.
…tools#131) Instances of dict subclasses (pandas' PrettyDict, OrderedDict, defaultdict, Counter, ...) are wrapped as PyCall::Dict because the type mapping falls back to the nearest mapped ancestor, but the dict_each and dict_contains helpers required the exact dict type, so #each, #to_h and #has_key? raised "unexpected Python type PrettyDict (expected dict)". Accept any dict subclass. Exact dicts keep the PyDict_Next and PyDict_Contains fast paths. Subclasses are iterated through items() and checked with `in`, as Python code would, so overridden items() or __contains__ and OrderedDict's own order (e.g. after move_to_end) are honored instead of being bypassed by the C dict API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Address review feedback on the dict subclass iteration path: - Keep the current item, its key and value, and any per-item iterator in state released by the rb_ensure handler. Previously, if converting the key or value raised a Ruby exception (e.g. from a registered type mapping's wrap_pyptr), the item and its elements were never decref'd. - Unpack each item the way Python's `for k, v in obj.items()` does: an exact tuple or list is unpacked directly, and any other iterable, including tuple and list subclasses, must yield exactly two elements through iteration. Failures raise PyCall::PyError with Python's own ValueError / TypeError messages instead of a Ruby TypeError that only accepted 2-tuples. Add PyExc_ValueError to the API table for these errors, and add specs for list, iterator, and tuple-subclass pairs, too few / too many elements, non-iterable items, and reference counts when key or value conversion fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f1cf319 to
4755c02
Compare
Fixes #131.
Problem
Instances of
dictsubclasses, such as pandas'PrettyDictreturned byDataFrame.groupby(...).groups,collections.OrderedDict,defaultdict, andCounter, are wrapped asPyCall::Dict, because the Python type mapping falls back to the nearest mapped ancestor. However,LibPython::Helpers.dict_eachanddict_containsrequired the exactdicttype, so#each,#to_h, and#has_key?(and its aliases) raised:#length,#[],#[]=, and#deletealready worked because they use the generic object protocol.Change
dict_eachanddict_containsnow accept an instance ofdictor any subclass of it.dictkeeps the existingPyDict_Next/PyDict_Containsfast paths.items()and checked within(PySequence_Contains), as Python code would do. The C dict API bypasses Python-level overrides ofitems()and__contains__, and forOrderedDictit ignores the dict's own ordering (for example aftermove_to_end), so going throughitems()keeps the Ruby view consistent with Python's.items()yields something other than 2-tuples,TypeErroris raised.Tests
New specs in
spec/pycall/dict_spec.rb, with a small helper modulespec/python/pycall/dict_subclass.py(no pandas dependency):#each/#to_hon an exact dictPrettyDict)OrderedDictiteration order aftermove_to_enddefaultdict(#has_key?does not insert a missing key)Counteritems,__iter__, and__contains__items()does not return pairsbreakout of#eachThe reporter's example now works with the pandas gem (pandas 3.x):
🤖 Generated with Claude Code