[tmva][sofie] Fix Python C-API reference handling in Keras parser #20877
+17
−8
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.
This pull request
Fixes Python C-API reference handling issues in the SOFIE Keras parser (
tmva/sofie_parsers/src/RModelParser_Keras.cxx) to avoid memory leaks during model parsing.The Keras parser relies on Python C-API calls (dictionary lookups, attribute access, string conversions). A few utilities and attribute extraction paths were creating temporary Python objects (new references) without releasing them, which could lead to increasing memory usage when parsing multiple models or repeatedly parsing within the same ROOT session.
This PR ensures temporary Python objects created during parsing are properly released, improving stability when parsing multiple models in the same session.
Changes or fixes:
GetValueFromDict()by avoiding leaking temporaryPyUnicodekey objects created viaPyUnicode_FromString()PyStringAsString()implementation by usingPyUnicode_AsUTF8()for safe UTF-8 extraction (avoids leaking temporary objects and unsafe lifetime issues)MakeKerasBatchNorm()by properly releasingPyObject*returned fromPyObject_GetAttrString()when retrieving BatchNorm weight names (gamma,beta,moving_mean,moving_variance)Checklist: