-
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
base: main
Are you sure you want to change the base?
Conversation
Code Review for PR #20 ✅Review SummaryThe PR adds a Issues FoundMissing Error Handling
Type Alias Considerations
Function Signature
Code Quality AssessmentThe code follows Go naming conventions and is consistent with the existing codebase style. The function serves a clear purpose for Python-Go interop, converting keyword arguments from Go's natural map representation to Python's dictionary format. However, the lack of error handling is concerning given that both ✅ Review completed with actionable suggestions provided above. 👉 Want me to re-run this review? Just type |
Summary of ChangesHello @visualfc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new utility function, Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces a utility function KwargsToDict to convert a Go map into a Python dictionary object for keyword arguments. The implementation has a critical issue with a potential nil pointer dereference and a high-severity memory leak due to improper reference count management. Additionally, the new code lacks documentation. I've provided suggestions to fix these issues.
| func KwargsToDict(kw Kwargs) *Object { | ||
| dict := NewDict() | ||
| for k, v := range kw { | ||
| dict.DictSetItem(FromGoString(k), v) | ||
| } | ||
| return dict | ||
| } |
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:
- Potential nil pointer dereference (
critical):NewDict()can returnnilif dictionary creation fails. The code does not check for this, which will cause a panic whendict.DictSetItemis called. - Memory leak (
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. - Unhandled errors (
high):FromGoStringcould fail and returnnil. This is not handled and would lead to a panic whenDictSetItemis called with anilkey.
Please add checks for nil returns and manage the reference count of the key object correctly to prevent panics and memory leaks.
| func KwargsToDict(kw Kwargs) *Object { | |
| dict := NewDict() | |
| for k, v := range kw { | |
| dict.DictSetItem(FromGoString(k), v) | |
| } | |
| return dict | |
| } | |
| func KwargsToDict(kw Kwargs) *Object { | |
| dict := NewDict() | |
| if dict == nil { | |
| return nil | |
| } | |
| for k, v := range kw { | |
| key := FromGoString(k) | |
| if key == nil { | |
| dict.DecRef() | |
| return nil | |
| } | |
| // TODO: The return value of DictSetItem should be checked for errors if its wrapper is fixed. | |
| dict.DictSetItem(key, v) | |
| key.DecRef() | |
| } | |
| return dict | |
| } |
py/call.go
Outdated
| type Kwargs = map[string]*Object | ||
|
|
||
| func KwargsToDict(kw Kwargs) *Object { |
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.
The new type Kwargs and function KwargsToDict are not documented. Adding Go doc comments would improve code clarity and maintainability by explaining their purpose and behavior, especially regarding return values on failure.
// Kwargs represents keyword arguments for a Python function call.
type Kwargs = map[string]*Object
// KwargsToDict converts a map of keyword arguments to a Python dictionary object.
// It returns a new reference to a PyDictObject, or nil on failure.
func KwargsToDict(kw Kwargs) *Object {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.
Found 2 key issues requiring attention:
• Missing error handling for dictionary operations that can fail
• Function signature doesn't allow callers to detect failures
| func KwargsToDict(kw Kwargs) *Object { | ||
| dict := NewDict() | ||
| for k, v := range kw { | ||
| dict.DictSetItem(FromGoString(k), v) | ||
| } | ||
| return dict | ||
| } |
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.
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.
| func KwargsToDict(kw Kwargs) *Object { | |
| dict := NewDict() | |
| for k, v := range kw { | |
| dict.DictSetItem(FromGoString(k), v) | |
| } | |
| return dict | |
| } | |
| func KwargsToDict(kw Kwargs) (*Object, error) { | |
| dict := NewDict() | |
| if dict == nil { | |
| return nil, errors.New("failed to create new dictionary") | |
| } | |
| for k, v := range kw { | |
| if result := dict.DictSetItem(FromGoString(k), v); result != nil { | |
| // Note: Actual error checking depends on DictSetItem return type | |
| // Current implementation returns *Object, but Python C API should return int | |
| } | |
| } | |
| return dict, nil | |
| } |
py/call.go
Outdated
| return nil | ||
| } | ||
|
|
||
| type Kwargs = map[string]*Object |
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.
Add documentation for the Kwargs type alias to explain its purpose and usage in the context of Python keyword arguments.
| type Kwargs = map[string]*Object | |
| // Kwargs represents keyword arguments as a map from string keys to Python objects |
No description provided.