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

Add support for 3.6+ default arguments #391

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
49 changes: 37 additions & 12 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,6 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case Pyc::LOAD_BUILD_CLASS:
stack.push(new ASTLoadBuildClass(new PycObject()));
break;
case Pyc::LOAD_CLOSURE_A:
/* Ignore this */
break;
case Pyc::LOAD_CONST_A:
{
PycRef<ASTObject> t_ob = new ASTObject(code->getConst(operand));
Expand All @@ -1487,6 +1484,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
break;
case Pyc::LOAD_DEREF_A:
case Pyc::LOAD_CLOSURE_A:
stack.push(new ASTName(code->getCellVar(mod, operand)));
break;
case Pyc::LOAD_FAST_A:
Expand Down Expand Up @@ -1545,15 +1543,42 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}

ASTFunction::defarg_t defArgs, kwDefArgs;
const int defCount = operand & 0xFF;
const int kwDefCount = (operand >> 8) & 0xFF;
for (int i = 0; i < defCount; ++i) {
defArgs.push_front(stack.top());
stack.pop();
}
for (int i = 0; i < kwDefCount; ++i) {
kwDefArgs.push_front(stack.top());
stack.pop();

if (mod->verCompare(3, 6) < 0) {
const int defCount = operand & 0xFF;
const int kwDefCount = (operand >> 8) & 0xFF;
for (int i = 0; i < defCount; ++i) {
defArgs.push_front(stack.top());
stack.pop();
}
for (int i = 0; i < kwDefCount; ++i) {
kwDefArgs.push_front(stack.top());
stack.pop();
}
} else {
if (operand & 0x08) {
stack.pop();
}

if (operand & 0x04) {
stack.pop();
}

if (operand & 0x02) {
PycRef<ASTConstMap> defaultsDict = stack.top().cast<ASTConstMap>();
stack.pop();

for (PycRef<ASTNode> value : defaultsDict->values())
kwDefArgs.push_front(value);
}

if (operand & 0x01) {
PycRef<PycTuple> defaultsTuple = stack.top().cast<ASTObject>()->object().cast<PycTuple>();
stack.pop();

for (PycRef<PycObject> value : defaultsTuple->values())
defArgs.push_back(new ASTObject(value));
}
}
stack.push(new ASTFunction(fun_code, defArgs, kwDefArgs));
}
Expand Down
Binary file modified tests/compiled/test_functions_py3.3.0.pyc
Binary file not shown.
Binary file modified tests/compiled/test_functions_py3.3.4.pyc
Binary file not shown.
Binary file not shown.
36 changes: 32 additions & 4 deletions tests/tokenized/test_functions_py3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,46 @@ def x4c ( foo , bar = 1 , bla = 2 , * args , ** kwargs ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x5a ( * , bar = 1 ) : <EOL>
def x5a ( * , bar ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x5b ( * , bar = 1 , ** kwargs ) : <EOL>
def x5b ( * , bar = 1 ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x6a ( foo , * , bar = 1 ) : <EOL>
def x5c ( * , bar = 1 , ** kwargs ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x7a ( foo , * , bar = 1 , ** kwargs ) : <EOL>
def x6a ( foo , * , bar ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x6b ( foo , * , bar = 1 ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x6c ( foo = 1 , * , bar ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x6d ( foo = 1 , * , bar = 2 ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x7a ( foo , * , bar , ** kwargs ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x7b ( foo , * , bar = 1 , ** kwargs ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x7c ( foo = 1 , * , bar , ** kwargs ) : <EOL>
<INDENT>
pass <EOL>
<OUTDENT>
def x7d ( foo = 1 , * , bar = 2 , ** kwargs ) : <EOL>
<INDENT>
pass <EOL>
Loading