-
Notifications
You must be signed in to change notification settings - Fork 6
py: func KwargsToDict #20
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -122,4 +122,14 @@ func (o *Object) VectorcallMethod(name *Object, args **Object, nargs uintptr, kw | |||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type Kwargs map[string]*Object | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func KwargsToDict(kw Kwargs) *Object { | ||||||||||||||||||||||||||||||||||||||||||
| dict := NewDict() | ||||||||||||||||||||||||||||||||||||||||||
| for k, v := range kw { | ||||||||||||||||||||||||||||||||||||||||||
| dict.DictSetItem(FromGoString(k), v) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return dict | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+127
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add proper error handling for dictionary creation and item setting. The current implementation ignores potential failures from NewDict() and DictSetItem(). Note: The actual error checking for DictSetItem may need adjustment based on the correct return type - Python C API typically returns int (-1 for error, 0 for success), but the current Go binding returns *Object.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function has several significant issues:
critical):NewDict()can returnnilif dictionary creation fails. The code does not check for this, which will cause a panic whendict.DictSetItemis called.high):FromGoString(k)likely returns a new Python string object.PyDict_SetItemincrements the reference count of the key, but the original reference fromFromGoStringis never decremented. This leads to a memory leak for every key in the map.high):FromGoStringcould fail and returnnil. This is not handled and would lead to a panic whenDictSetItemis called with anilkey.Please add checks for
nilreturns and manage the reference count of the key object correctly to prevent panics and memory leaks.