Skip to content

Commit

Permalink
add again freevars to BaseBytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Feb 29, 2016
1 parent 8300835 commit 057a241
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 6 additions & 0 deletions bytecode/bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def __init__(self):
self.filename = '<string>'
self.docstring = UNSET
self.cellvars = []
# we cannot recreate freevars from instructions because of super()
# special-case
self.freevars = []

def _copy_attr_from(self, bytecode):
self.argcount = bytecode.argcount
Expand All @@ -29,6 +32,7 @@ def _copy_attr_from(self, bytecode):
self.filename = bytecode.filename
self.docstring = bytecode.docstring
self.cellvars = list(bytecode.cellvars)
self.freevars = list(bytecode.freevars)

def __eq__(self, other):
if type(self) != type(other):
Expand All @@ -52,6 +56,8 @@ def __eq__(self, other):
return False
if self.cellvars != other.cellvars:
return False
if self.freevars != other.freevars:
return False

return True

Expand Down
7 changes: 1 addition & 6 deletions bytecode/concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def __init__(self):
self.consts = []
self.names = []
self.varnames = []
self.freevars = []

def __repr__(self):
return '<ConcreteBytecode instr#=%s>' % len(self)
Expand All @@ -131,8 +130,6 @@ def __eq__(self, other):
return False
if self.varnames != other.varnames:
return False
if self.freevars != other.freevars:
return False

return super().__eq__(other)

Expand Down Expand Up @@ -350,7 +347,6 @@ def __init__(self, code):
self.consts = {}
self.names = []
self.varnames = []
self.freevars = []

def add_const(self, value):
key = const_key(value)
Expand Down Expand Up @@ -403,7 +399,7 @@ def concrete_instructions(self):
try:
arg = self.bytecode.cellvars.index(arg)
except ValueError:
arg = self.add(self.freevars, arg)
arg = self.bytecode.freevars.index(arg)

instr = ConcreteInstr(instr.name, arg, lineno=lineno)
if is_jump:
Expand Down Expand Up @@ -470,7 +466,6 @@ def to_concrete_bytecode(self):
concrete.consts = consts
concrete.names = self.names
concrete.varnames = self.varnames
concrete.freevars = self.freevars

# copy instructions
concrete[:] = self.instructions
Expand Down
1 change: 1 addition & 0 deletions bytecode/tests/test_concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def test_dont_merge_constants(self):
def test_cellvars(self):
code = Bytecode()
code.cellvars = ['x']
code.freevars = ['y']
code.extend([Instr('LOAD_DEREF', 'x', lineno=1),
Instr('LOAD_DEREF', 'y', lineno=1)])
concrete = code.to_concrete_bytecode()
Expand Down

0 comments on commit 057a241

Please sign in to comment.