Skip to content
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

Fix method python method call without self #439

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/jsTypeFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,22 @@ 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;
nNormalArgs += bytecode->co_argcount - nDefaultArgs - 1;
if (bytecode->co_argcount == 0 && isMethod) {
nNormalArgs += nDefaultArgs;
} else {
nNormalArgs += bytecode->co_argcount - nDefaultArgs - 1;
}
Copy link
Collaborator

@zollqir zollqir Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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```

if (bytecode->co_flags & CO_VARARGS) {
varargs = true;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/python/test_functions_this.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,18 @@ def pyFunc():
pm.collect() # this should collect the JS proxy to pyFunc, which should decref pyFunc
# pyFunc should be collected by now
assert ref[0]() is None


def test_method_no_self():
class What:
def some_method():
return 3

obj = What()

try:
pm.eval('x => x.some_method()')(obj)
assert (False)
except Exception as e:
assert str(type(e)) == "<class 'pythonmonkey.SpiderMonkeyError'>"
assert str(e).__contains__('takes 0 positional arguments but 1 was given')
philippedistributive marked this conversation as resolved.
Show resolved Hide resolved
Loading