Skip to content

Commit

Permalink
Support LOAD_METHOD and CALL_METHOD from Python 3.7+
Browse files Browse the repository at this point in the history
Fixes zrax#163
  • Loading branch information
zrax committed Apr 12, 2021
1 parent f0d2d80 commit ac189cc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,34 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(call);
}
break;
case Pyc::CALL_METHOD_A:
{
ASTCall::pparam_t pparamList;
for (int i = 0; i < operand; i++) {
PycRef<ASTNode> param = stack.top();
stack.pop();
if (param.type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> fun_code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = fun_code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<PycString> function_name = code_src->name();
if (function_name->isEqual("<lambda>")) {
pparamList.push_front(param);
} else {
// Decorator used
PycRef<ASTNode> decor_name = new ASTName(function_name);
curblock->append(new ASTStore(param, decor_name));

pparamList.push_front(decor_name);
}
} else {
pparamList.push_front(param);
}
}
PycRef<ASTNode> func = stack.top();
stack.pop();
stack.push(new ASTCall(func, pparamList, ASTCall::kwparam_t()));
}
break;
case Pyc::CONTINUE_LOOP_A:
curblock->append(new ASTKeyword(ASTKeyword::KW_CONTINUE));
break;
Expand Down Expand Up @@ -1476,6 +1504,14 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case Pyc::STORE_LOCALS:
stack.pop();
break;
case Pyc::LOAD_METHOD_A:
{
// Behave like LOAD_ATTR
PycRef<ASTNode> name = stack.top();
stack.pop();
stack.push(new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR));
}
break;
case Pyc::LOAD_NAME_A:
stack.push(new ASTName(code->getName(operand)));
break;
Expand Down
Binary file added tests/compiled/load_method.3.7.pyc
Binary file not shown.
Binary file added tests/compiled/load_method.3.9.pyc
Binary file not shown.
File renamed without changes.
Binary file added tests/compiled/test_class_method_py3.3.7.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/input/load_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class C:
def test1(self):
a = 1

def test2(self, x, y, z):
a = x * y + z

# @staticmethod -- TODO: Fix decorators
def testS():
a = 3

c = C()
c.test1()
c.test2(42, 5, -1)
C.testS()
19 changes: 19 additions & 0 deletions tests/tokenized/load_method.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class C : <EOL>
<INDENT>
def test1 ( self ) : <EOL>
<INDENT>
a = 1 <EOL>
<OUTDENT>
def test2 ( self , x , y , z ) : <EOL>
<INDENT>
a = x * y + z <EOL>
<OUTDENT>
def testS ( ) : <EOL>
<INDENT>
a = 3 <EOL>
<OUTDENT>
<OUTDENT>
c = C ( ) <EOL>
c . test1 ( ) <EOL>
c . test2 ( 42 , 5 , - 1 ) <EOL>
C . testS ( ) <EOL>
Binary file added tests/xfail/loadbuild_class.3.7.pyc
Binary file not shown.

0 comments on commit ac189cc

Please sign in to comment.