fix: support python @classmethod decorator#112
Open
CrackTC wants to merge 2 commits intoantgroup:mainfrom
Open
fix: support python @classmethod decorator#112CrackTC wants to merge 2 commits intoantgroup:mainfrom
CrackTC wants to merge 2 commits intoantgroup:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces executeSingleCall and buildNewObject to the PythonAnalyzer to support Python class methods and object instantiation. The executeSingleCall method handles the injection of the class reference into the argument list for methods decorated with @classmethod. A review comment identifies a potential crash due to a missing null check on fclos._this and a logic bug where the class argument is not correctly prepended when the argument list is empty.
Comment on lines
+257
to
+274
| executeSingleCall(fclos: any, argvalues: any, state: any, node: any, scope: any) { | ||
| if (fclos.decorators?.some((d: any) => d.name === 'classmethod')) { | ||
| let cls | ||
| if (fclos._this.vtype === 'object' && fclos._this.__class) { | ||
| cls = fclos._this.__class | ||
| } else if (fclos._this.vtype === 'class') { | ||
| cls = fclos._this | ||
| } | ||
| if (cls) { | ||
| if (argvalues[0]?.vtype === 'undefine') { | ||
| argvalues[0] = cls | ||
| } else if (argvalues[0]) { | ||
| argvalues.unshift(cls) | ||
| } | ||
| } | ||
| } | ||
| return super.executeSingleCall(fclos, argvalues, state, node, scope) | ||
| } |
Contributor
There was a problem hiding this comment.
The implementation of executeSingleCall has two significant issues:
- Potential Crash:
fclos._thisis accessed directly at line 260 without a null/undefined check. If_thisis not set for the closure, the analyzer will throw an exception and crash. - Logic Bug in Argument Injection: The logic for injecting the class argument (
cls) intoargvaluesfails whenargvaluesis empty (line 268). In Python, a@classmethodalways receives the class as its first argument, even if no other arguments are passed. Additionally, usingelse if (argvalues[0])is risky as it relies on truthiness; it is safer to check the array length or handle the empty case explicitly.
executeSingleCall(fclos: any, argvalues: any, state: any, node: any, scope: any) {
if (fclos.decorators?.some((d: any) => d.name === 'classmethod')) {
let cls
const _this = fclos._this
if (_this) {
if (_this.vtype === 'object' && _this.__class) {
cls = _this.__class
} else if (_this.vtype === 'class') {
cls = _this
}
}
if (cls) {
if (argvalues.length > 0 && argvalues[0]?.vtype === 'undefine') {
argvalues[0] = cls
} else {
argvalues.unshift(cls)
}
}
}
return super.executeSingleCall(fclos, argvalues, state, node, scope)
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.