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
Changes from 2 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
46 changes: 37 additions & 9 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break;
case Pyc::LOAD_CLOSURE_A:
/* Ignore this */
stack.push(new ASTNode());
ptrstr marked this conversation as resolved.
Show resolved Hide resolved
break;
case Pyc::LOAD_CONST_A:
{
Expand Down Expand Up @@ -1545,15 +1546,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->minorVer() <= 5) {
ptrstr marked this conversation as resolved.
Show resolved Hide resolved
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
Loading