Skip to content

Commit

Permalink
Merge pull request #439 from Distributive-Network/philippe/fix-436
Browse files Browse the repository at this point in the history
Fix method python method call without self
  • Loading branch information
philippedistributive authored Sep 11, 2024
2 parents ee8a0f5 + 58dafa4 commit 2ed42d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/jsTypeFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ bool callPyFunc(JSContext *cx, unsigned int argc, JS::Value *vp) {
if (PyMethod_Check(pyFunc)) {
f = PyMethod_Function(pyFunc); // borrowed reference
nNormalArgs -= 1; // don't include the implicit `self` of the method as an argument
}
}
PyCodeObject *bytecode = (PyCodeObject *)PyFunction_GetCode(f); // borrowed reference
PyObject *defaults = PyFunction_GetDefaults(f); // borrowed reference
nDefaultArgs = defaults ? PyTuple_Size(defaults) : 0;
Expand All @@ -393,7 +393,7 @@ bool callPyFunc(JSContext *cx, unsigned int argc, JS::Value *vp) {
}

// 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
Expand Down
14 changes: 14 additions & 0 deletions tests/python/test_functions_this.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,17 @@ 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 pm.SpiderMonkeyError as e:
assert 'takes 0 positional arguments but 1 was given' in str(e)

0 comments on commit 2ed42d9

Please sign in to comment.