GeezerVison is showing github.com — Fix: del obj[key] on a reflected indexer crashed the process by jhonabreul · Pull Request #149 · QuantConnect/pythonnet
Conversation
jhonabreul
commented
Sep 2, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Problem
del obj[key] on any reflected type with a settable indexer crashed the process.
- CPython uses the same
mp_ass_subscript slot for assignment and deletion, with a null value for del.
mp_ass_subscript_impl never checked for the null and forwarded it into PyTuple_SetItem, which threw across the native boundary.
ArrayObject.mp_ass_subscript had the same hole and died with an access violation.
- Official pythonnet fixed this in
__delitem__ for IList<T> and IDictionary<K,V> pythonnet/pythonnet#2533; this fork predates it.
Changes
ClassBase: check v.IsNull first and route to the new DeleteItemImpl.
DeleteItemImpl: TypeError when the type has no deleter or the key is a tuple; otherwise call the deleter through the binder; false from Remove becomes KeyError.
Indexer: new DeleterBinder, resolved lazily on the first del from IDictionary<K,V>.Remove(K), else IList<T>.RemoveAt(int). Uses the interface method, so explicit implementations like ConcurrentDictionary.Remove work.
ArrayObject: TypeError on a null value.
- Tests: nine pytest cases, a throwing
Remove fixture, and an embed test fixture.
Behaviour
| Target |
Before |
After |
| plain settable indexer, tuple key, array |
crash |
TypeError |
Dictionary / ConcurrentDictionary |
crash |
key removed, KeyError if missing |
List<T> |
crash |
element removed, ArgumentOutOfRangeException if out of range |
| deleter throws |
n/a |
catchable exception, interpreter alive |
Verification
- pytest: 477 passed, 1 pre-existing environmental failure (
test_explicit_assembly_load).
- Embed tests (Debug): 3 passed. Each new test crashes on
master.
- Lean with the patched DLL: a Python algorithm doing
del self.runtime_statistics[key] now completes; 324/324 Python regression algorithms and 16600/16600 Python unit tests pass.
Follow-up
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
CPython calls mp_ass_subscript with a null value for del, which mp_ass_subscript_impl forwarded into PyTuple_SetItem and threw across the native boundary. Handle deletion first: IDictionary<K,V>.Remove / IList<T>.RemoveAt through the binder (KeyError on a missing dictionary key), TypeError for every other type and for arrays.
Most types are never deleted from, so the interface walk that finds IDictionary<K,V>.Remove or IList<T>.RemoveAt now runs on the first del instead of at class creation.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|