-
Notifications
You must be signed in to change notification settings - Fork 760
Parse cell & free vars for Py 3.11+ #576
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ lntable Obj Obj Obj Obj Obj Obj | |
exceptiontable Obj | ||
*/ | ||
|
||
// TODO : Simplify this function | ||
void PycCode::load(PycData* stream, PycModule* mod) | ||
{ | ||
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0) | ||
|
@@ -75,23 +76,49 @@ void PycCode::load(PycData* stream, PycModule* mod) | |
m_consts = LoadObject(stream, mod).cast<PycSequence>(); | ||
m_names = LoadObject(stream, mod).cast<PycSequence>(); | ||
|
||
// Represents localsplusnames for py 3.11+ | ||
if (mod->verCompare(1, 3) >= 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment is misleading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. umm so this variable is being used for two different purposes - corresponds to |
||
m_localNames = LoadObject(stream, mod).cast<PycSequence>(); | ||
else | ||
m_localNames = new PycTuple; | ||
|
||
if (mod->verCompare(3, 11) >= 0) | ||
PycSimpleSequence::value_t free_vars_extra, cell_vars_extra; | ||
|
||
if (mod->verCompare(3, 11) >= 0) { | ||
m_localKinds = LoadObject(stream, mod).cast<PycString>(); | ||
|
||
if (m_localKinds->length() != m_localNames->size()) { | ||
throw std::runtime_error("All variables kinds are not available"); | ||
} | ||
|
||
// Starting py3.11, all variables are now part of localsplusnames | ||
|
||
for (int i = 0; i < m_localKinds->length(); i++) { | ||
const char kind = m_localKinds->value()[i]; | ||
auto name = m_localNames->get(i); | ||
|
||
if (kind & Kinds::CO_FAST_CELL) { | ||
cell_vars_extra.push_back(name); | ||
} | ||
else if (kind & Kinds::CO_FAST_FREE) { | ||
free_vars_extra.push_back(name); | ||
} | ||
} | ||
} | ||
else | ||
m_localKinds = new PycString; | ||
|
||
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0) | ||
m_freeVars = LoadObject(stream, mod).cast<PycSequence>(); | ||
else if (mod->verCompare(3, 11) >= 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think given the complicated structure this whole function should be rewritten appropriately. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly seems quite hard to rewrite this function. I think default initializations would be quite useful here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default initializations themselves are not trivial :| It's annoying as well. I shall skip simplifying this logic at the moment. |
||
m_freeVars = new PycTuple(free_vars_extra); | ||
else | ||
m_freeVars = new PycTuple; | ||
|
||
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0) | ||
m_cellVars = LoadObject(stream, mod).cast<PycSequence>(); | ||
else if (mod->verCompare(3, 11) >= 0) | ||
m_cellVars = new PycTuple(cell_vars_extra); | ||
else | ||
m_cellVars = new PycTuple; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def outer(): | ||
x = 0 | ||
def inner(): | ||
nonlocal x | ||
x += 1 | ||
print(x) | ||
return inner |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def outer ( ) : <EOL> | ||
<INDENT> | ||
x = 0 <EOL> | ||
def inner ( ) : <EOL> | ||
<INDENT> | ||
nonlocal x <EOL> | ||
x += 1 <EOL> | ||
print ( x ) <EOL> | ||
<OUTDENT> | ||
return inner <EOL> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Easier to duplicate from globals instead of handling both together. Can be changed if need be.