Permalink
Browse files

Fix regression in last change.

'closure' was out of order because we used 'self.cellvars' and not the
reordered local copy 'cellvars'.

Extract method and simplify code.
  • Loading branch information...
Andy Chu
Andy Chu committed Mar 20, 2018
1 parent 9c47ae3 commit e93995f67661de16c057454d67e1a129067683f3
Showing with 19 additions and 14 deletions.
  1. +19 −14 opy/compiler2/pyassem.py
View
@@ -371,32 +371,37 @@ def setFlag(self, flag):
def checkFlag(self, flag):
return bool(self.flags & flag)
def _ReorderCellVars(self):
"""Reorder cellvars so the ones in self.varnames are first.
And prune from freevars (?)
"""
lookup = set(self.cellvars)
remaining = lookup - set(self.varnames)
cellvars = [n for n in self.varnames if n in lookup]
cellvars.extend(remaining)
return cellvars
def MakeCodeObject(self):
"""Assemble a Python code object."""
stacksize = ComputeStackDepth(self.blocks, self.entry, self.exit)
blocks = OrderBlocks(self.entry, self.exit)
insts = FlattenGraph(blocks)
# Sort self.cellvars so the ones in self.varnames are first. And prune
# from freevars (?)
lookup = set(self.cellvars)
remaining = lookup - set(self.varnames)
cellvars = [n for n in self.varnames if n in lookup]
cellvars.extend(remaining)
cellvars = self._ReorderCellVars()
consts = [self.docstring]
names = []
# The closure list is used to track the order of cell variables and
# free variables in the resulting code object. The offsets used by
# LOAD_CLOSURE/LOAD_DEREF refer to both kinds of variables.
closure = self.cellvars + self.freevars
closure = cellvars + self.freevars
# Convert arguments from symbolic to concrete form
# Mutates the insts argument. The converters mutate self.names,
# self.varnames, etc.
# Convert arguments from symbolic to concrete form. Mutates
# self.consts, self.names, etc.
enc = ArgEncoder(self.klass, consts, names, self.varnames, closure)
for i, t in enumerate(insts):
@@ -407,10 +412,10 @@ def MakeCodeObject(self):
arg_index = method(enc, oparg)
insts[i] = opname, arg_index
if (self.flags & CO_NEWLOCALS) == 0:
nlocals = 0
else:
if self.flags & CO_NEWLOCALS:
nlocals = len(self.varnames)
else:
nlocals = 0
if self.flags & CO_VARKEYWORDS:
self.argcount -= 1

0 comments on commit e93995f

Please sign in to comment.