Repository navigation
Fix method python method call without self - #439
Merged
Merged
Conversation
zollqir
requested changes
Sep 11, 2024
zollqir
left a comment
Collaborator
There was a problem hiding this comment.
logic works, but a simpler solution is available
Comment on lines
+382
to
+397
| bool isMethod; | ||
| if (PyMethod_Check(pyFunc)) { | ||
| f = PyMethod_Function(pyFunc); // borrowed reference | ||
| nNormalArgs -= 1; // don't include the implicit `self` of the method as an argument | ||
| isMethod = true; | ||
| } else { | ||
| isMethod = false; | ||
| } | ||
| PyCodeObject *bytecode = (PyCodeObject *)PyFunction_GetCode(f); // borrowed reference | ||
| PyObject *defaults = PyFunction_GetDefaults(f); // borrowed reference | ||
| nDefaultArgs = defaults ? PyTuple_Size(defaults) : 0; | ||
| nNormalArgs += bytecode->co_argcount - nDefaultArgs - 1; | ||
| if (bytecode->co_argcount == 0 && isMethod) { | ||
| nNormalArgs += nDefaultArgs; | ||
| } else { | ||
| nNormalArgs += bytecode->co_argcount - nDefaultArgs - 1; | ||
| } |
Collaborator
There was a problem hiding this comment.
These changes are not necessary, the comparison on line 404 just needs to change
diff --git a/src/jsTypeFactory.cc b/src/jsTypeFactory.cc
index 24eb5c4..7238da3 100644
--- a/src/jsTypeFactory.cc
+++ b/src/jsTypeFactory.cc
@@ -379,29 +379,21 @@ bool callPyFunc(JSContext *cx, unsigned int argc, JS::Value *vp) {
else {
nNormalArgs = 1;
PyObject *f = pyFunc;
- bool isMethod;
if (PyMethod_Check(pyFunc)) {
f = PyMethod_Function(pyFunc); // borrowed reference
nNormalArgs -= 1; // don't include the implicit `self` of the method as an argument
- isMethod = true;
- } else {
- isMethod = false;
}
PyCodeObject *bytecode = (PyCodeObject *)PyFunction_GetCode(f); // borrowed reference
PyObject *defaults = PyFunction_GetDefaults(f); // borrowed reference
nDefaultArgs = defaults ? PyTuple_Size(defaults) : 0;
- if (bytecode->co_argcount == 0 && isMethod) {
- nNormalArgs += nDefaultArgs;
- } else {
- nNormalArgs += bytecode->co_argcount - nDefaultArgs - 1;
- }
+ nNormalArgs += bytecode->co_argcount - nDefaultArgs - 1;
if (bytecode->co_flags & CO_VARARGS) {
varargs = true;
}
}
// use faster calling if no arguments are needed
- if (((nNormalArgs + nDefaultArgs) == 0 && !varargs)) {
+ if (((nNormalArgs + nDefaultArgs) <= 0 && !varargs)) {
#if PY_VERSION_HEX >= 0x03090000
pyRval = PyObject_CallNoArgs(pyFunc);
#else```Co-authored-by: Caleb Aikens <caleb@distributive.network>
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.
Do check if method has 0 args meaning no self and ultimately fail in the correct manner
closes #436