Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for each in bc.freevars:
if each not in ctx.local:
if each not in ctx.bc.freevars:
ctx.bc.freevars.append(each)
delay_append(
Instr(
'LOAD_CLOSURE',
FreeVar(each),
lineno=def_fun.lineno + 1))
else:
if each not in ctx.bc.cellvars:
ctx.bc.cellvars.append(each)
delay_append(
Instr(
'LOAD_CLOSURE',
CellVar(each),
lineno=def_fun.lineno + 1))
delay_append(
Instr(
"BUILD_TUPLE", arg=len(bc.freevars),
lineno=def_fun.lineno + 1))
if ctx.is_nested:
bc.flags |= NESTED
bc.flags |= OPTIMIZED
# dump_bytecode(bc)
# print(name, ctx.local.keys(), ctx.bc.freevars, ctx.bc.cellvars)
yield from wait(RESOLVED)
code = bc.to_code()
ctx.bc.extend(delay)
# dis.show_code(code)
ctx.bc.append(Instr('LOAD_CONST', arg=code, lineno=def_fun.lineno + 1))
delay = []
delay_append = delay.append
if any(bc.freevars):
for each in bc.freevars:
if each not in ctx.local:
if each not in ctx.bc.freevars:
ctx.bc.freevars.append(each)
delay_append(
Instr(
'LOAD_CLOSURE', FreeVar(each), lineno=tast.lineno + 1))
else:
if each not in ctx.bc.cellvars:
ctx.bc.cellvars.append(each)
delay_append(
Instr(
'LOAD_CLOSURE', CellVar(each), lineno=tast.lineno + 1))
delay_append(
Instr("BUILD_TUPLE", arg=len(bc.freevars), lineno=tast.lineno + 1))
if ctx.is_nested:
bc.flags |= NESTED
bc.flags |= OPTIMIZED
yield from wait(RESOLVED)
code = bc.to_code()
# dis.show_code(code)
ctx.bc.extend(delay)
ctx.bc.append(Instr('LOAD_CONST', arg=code, lineno=tast.lineno + 1))
ctx.bc.append(Instr('LOAD_CONST', arg=bc.name, lineno=tast.lineno + 1))
ctx.bc.append(
Instr(
yield from wait(RESOLVED)
code = bc.to_code()
ctx.bc.extend(delay)
# dis.show_code(code)
ctx.bc.append(Instr('LOAD_CONST', arg=code, lineno=def_fun.lineno + 1))
ctx.bc.append(Instr('LOAD_CONST', arg=bc.name, lineno=def_fun.lineno + 1))
ctx.bc.append(
Instr(
'MAKE_FUNCTION',
arg=8 if any(bc.freevars) else 0,
lineno=def_fun.lineno + 1))
if name:
if name in ctx.bc.cellvars:
ctx.bc.append(
Instr("STORE_DEREF", CellVar(name), lineno=def_fun.lineno + 1))
else:
ctx.bc.append(Instr('STORE_FAST', name, lineno=def_fun.lineno + 1))
def load_name(self, tast):
name = tast.name
if name in self.local:
if name not in self.bc.cellvars:
self.bc.append(
Instr('LOAD_FAST', name, lineno=tast.lineno + 1))
else:
self.bc.append(
Instr('LOAD_DEREF', CellVar(name), lineno=tast.lineno + 1))
return
if name in self.symtb:
self.bc.append(
Instr('LOAD_DEREF', FreeVar(name), lineno=tast.lineno + 1))
else:
self.bc.append(Instr('LOAD_GLOBAL', name, lineno=tast.lineno + 1))
def load_arg(x, cellvars, lineno):
if x in cellvars:
return PyInstr(InstrNames.LOAD_DEREF, CellVar(x), lineno=lineno)
return PyInstr(InstrNames.LOAD_FAST, x, lineno=lineno)
elif b.name == InstrNames.LOAD_CLOSURE:
arg = b.arg
assert isinstance(arg, (bc.CellVar, bc.FreeVar))
a = yield am.reg_of(arg.name)
yield am.push(a)
elif b.name == InstrNames.LOAD_DEREF:
arg = b.arg
assert isinstance(arg, (bc.CellVar, bc.FreeVar))
a = yield am.load(arg.name)
yield am.push(a)
elif b.name == InstrNames.STORE_DEREF:
arg = b.arg
assert isinstance(arg, (bc.CellVar, bc.FreeVar))
a = yield am.pop()
yield am.store(arg.name, a)
elif b.name == InstrNames.LOAD_FAST:
assert isinstance(b.arg, str)
reg = yield am.reg_of(b.arg)
yield am.push(reg)
elif b.name == InstrNames.STORE_FAST:
assert isinstance(b.arg, str)
v = yield am.pop()
yield am.assign(b.arg, v)
elif b.name == InstrNames.LOAD_GLOBAL:
arg = b.arg
def load_arg(x, cellvars, lineno):
if x in cellvars:
return PyInstr(InstrNames.LOAD_DEREF, CellVar(x), lineno=lineno)
return PyInstr(InstrNames.LOAD_FAST, x, lineno=lineno)
code = bc.to_code()
# dis.show_code(code)
ctx.bc.extend(delay)
ctx.bc.append(Instr('LOAD_CONST', arg=code, lineno=tast.lineno + 1))
ctx.bc.append(Instr('LOAD_CONST', arg=bc.name, lineno=tast.lineno + 1))
ctx.bc.append(
Instr(
'MAKE_FUNCTION',
arg=8 if any(bc.freevars) else 0,
lineno=tast.lineno + 1))
ctx.bc.append(Instr("CALL_FUNCTION", arg=0, lineno=tast.lineno + 1))
if name in ctx.bc.cellvars:
ctx.bc.append(Instr("STORE_DEREF", arg=CellVar(name)))
else:
ctx.bc.append(Instr('STORE_FAST', arg=tast.name))