Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
& {
"LOAD_CLOSURE",
"LOAD_DEREF",
"STORE_DEREF",
"DELETE_DEREF",
"LOAD_CLASSDEREF",
}
):
flags |= CompilerFlags.NOFREE
# Copy flags for which we cannot infer the right value
flags |= bytecode.flags & (
CompilerFlags.NEWLOCALS
| CompilerFlags.VARARGS
| CompilerFlags.VARKEYWORDS
| CompilerFlags.NESTED
)
sure_generator = instr_names & {"YIELD_VALUE"}
maybe_generator = instr_names & {"YIELD_VALUE", "YIELD_FROM"}
sure_async = instr_names & {
"GET_AWAITABLE",
"GET_AITER",
"GET_ANEXT",
"BEFORE_ASYNC_WITH",
"SETUP_ASYNC_WITH",
"END_ASYNC_FOR",
}
# If performing inference or forcing an async behavior, first inspect
# the flags since this is the only way to identify iterable coroutines
raise ValueError(msg % bytecode)
instructions = (
bytecode.get_instructions()
if isinstance(bytecode, _bytecode.ControlFlowGraph)
else bytecode
)
instr_names = {
i.name
for i in instructions
if not isinstance(i, (_bytecode.SetLineno, _bytecode.Label))
}
# Identify optimized code
if not (instr_names & {"STORE_NAME", "LOAD_NAME", "DELETE_NAME"}):
flags |= CompilerFlags.OPTIMIZED
# Check for free variables
if not (
instr_names
& {
"LOAD_CLOSURE",
"LOAD_DEREF",
"STORE_DEREF",
"DELETE_DEREF",
"LOAD_CLASSDEREF",
}
):
flags |= CompilerFlags.NOFREE
# Copy flags for which we cannot infer the right value
flags |= bytecode.flags & (
if not (
instr_names
& {
"LOAD_CLOSURE",
"LOAD_DEREF",
"STORE_DEREF",
"DELETE_DEREF",
"LOAD_CLASSDEREF",
}
):
flags |= CompilerFlags.NOFREE
# Copy flags for which we cannot infer the right value
flags |= bytecode.flags & (
CompilerFlags.NEWLOCALS
| CompilerFlags.VARARGS
| CompilerFlags.VARKEYWORDS
| CompilerFlags.NESTED
)
sure_generator = instr_names & {"YIELD_VALUE"}
maybe_generator = instr_names & {"YIELD_VALUE", "YIELD_FROM"}
sure_async = instr_names & {
"GET_AWAITABLE",
"GET_AITER",
"GET_ANEXT",
"BEFORE_ASYNC_WITH",
"SETUP_ASYNC_WITH",
"END_ASYNC_FOR",
}
if sure_generator:
flags |= CompilerFlags.ASYNC_GENERATOR
else:
flags |= CompilerFlags.COROUTINE
elif maybe_generator:
if is_async:
if sure_generator:
flags |= CompilerFlags.ASYNC_GENERATOR
else:
flags |= CompilerFlags.COROUTINE
else:
flags |= CompilerFlags.GENERATOR
elif is_async:
flags |= CompilerFlags.COROUTINE
# If the code should not be asynchronous, check first it is possible and
# next set the GENERATOR flag if relevant
else:
if sure_async:
raise ValueError(
"The is_async argument is False but bytecodes "
"that can only be used in async functions have "
"been detected."
)
if maybe_generator:
flags |= CompilerFlags.GENERATOR
flags |= bytecode.flags & CompilerFlags.FUTURE_GENERATOR_STOP
bc.filename = ""
cts = tag_table.cts
has_annotation = ContextType.Annotation in cts
under_class_def_or_module = ContextType.ClassDef in cts or ContextType.Module in cts
if has_annotation and under_class_def_or_module:
bc.append(SETUP_ANNOTATIONS())
if not under_class_def_or_module:
bc.flags |= CompilerFlags.OPTIMIZED
bc.flags |= CompilerFlags.NEWLOCALS
if ContextType.Module in self.cts:
bc.flags |= CompilerFlags.NESTED
if ContextType.Coroutine in cts:
if ContextType.Generator in cts:
bc.flags |= CompilerFlags.ASYNC_GENERATOR
else:
bc.flags |= CompilerFlags.COROUTINE
elif ContextType.Generator in cts:
bc.flags |= CompilerFlags.GENERATOR
# not elif for further designing(async lambda)
freevars = sym_tb.freevars
cellvars = sym_tb.cellvars
if ContextType.Module not in cts and ContextType.ClassDef in self.cts and '__class__' not in freevars:
freevars.append('__class__')
if freevars:
# Identify optimized code
if not (instr_names & {"STORE_NAME", "LOAD_NAME", "DELETE_NAME"}):
flags |= CompilerFlags.OPTIMIZED
# Check for free variables
if not (
instr_names
& {
"LOAD_CLOSURE",
"LOAD_DEREF",
"STORE_DEREF",
"DELETE_DEREF",
"LOAD_CLASSDEREF",
}
):
flags |= CompilerFlags.NOFREE
# Copy flags for which we cannot infer the right value
flags |= bytecode.flags & (
CompilerFlags.NEWLOCALS
| CompilerFlags.VARARGS
| CompilerFlags.VARKEYWORDS
| CompilerFlags.NESTED
)
sure_generator = instr_names & {"YIELD_VALUE"}
maybe_generator = instr_names & {"YIELD_VALUE", "YIELD_FROM"}
sure_async = instr_names & {
"GET_AWAITABLE",
"GET_AITER",
"GET_ANEXT",
# Check for free variables
if not (
instr_names
& {
"LOAD_CLOSURE",
"LOAD_DEREF",
"STORE_DEREF",
"DELETE_DEREF",
"LOAD_CLASSDEREF",
}
):
flags |= CompilerFlags.NOFREE
# Copy flags for which we cannot infer the right value
flags |= bytecode.flags & (
CompilerFlags.NEWLOCALS
| CompilerFlags.VARARGS
| CompilerFlags.VARKEYWORDS
| CompilerFlags.NESTED
)
sure_generator = instr_names & {"YIELD_VALUE"}
maybe_generator = instr_names & {"YIELD_VALUE", "YIELD_FROM"}
sure_async = instr_names & {
"GET_AWAITABLE",
"GET_AITER",
"GET_ANEXT",
"BEFORE_ASYNC_WITH",
"SETUP_ASYNC_WITH",
"END_ASYNC_FOR",
}
instr_names
& {
"LOAD_CLOSURE",
"LOAD_DEREF",
"STORE_DEREF",
"DELETE_DEREF",
"LOAD_CLASSDEREF",
}
):
flags |= CompilerFlags.NOFREE
# Copy flags for which we cannot infer the right value
flags |= bytecode.flags & (
CompilerFlags.NEWLOCALS
| CompilerFlags.VARARGS
| CompilerFlags.VARKEYWORDS
| CompilerFlags.NESTED
)
sure_generator = instr_names & {"YIELD_VALUE"}
maybe_generator = instr_names & {"YIELD_VALUE", "YIELD_FROM"}
sure_async = instr_names & {
"GET_AWAITABLE",
"GET_AITER",
"GET_ANEXT",
"BEFORE_ASYNC_WITH",
"SETUP_ASYNC_WITH",
"END_ASYNC_FOR",
}
# If performing inference or forcing an async behavior, first inspect
"GET_ANEXT",
"BEFORE_ASYNC_WITH",
"SETUP_ASYNC_WITH",
"END_ASYNC_FOR",
}
# If performing inference or forcing an async behavior, first inspect
# the flags since this is the only way to identify iterable coroutines
if is_async in (None, True):
if bytecode.flags & CompilerFlags.COROUTINE:
if sure_generator:
flags |= CompilerFlags.ASYNC_GENERATOR
else:
flags |= CompilerFlags.COROUTINE
elif bytecode.flags & CompilerFlags.ITERABLE_COROUTINE:
if sure_async:
msg = (
"The ITERABLE_COROUTINE flag is set but bytecode that"
"can only be used in async functions have been "
"detected. Please unset that flag before performing "
"inference."
)
raise ValueError(msg)
flags |= CompilerFlags.ITERABLE_COROUTINE
elif bytecode.flags & CompilerFlags.ASYNC_GENERATOR:
if not sure_generator:
flags |= CompilerFlags.COROUTINE
else:
flags |= CompilerFlags.ASYNC_GENERATOR
# If the code was not asynchronous before determine if it should now be
elif is_async:
flags |= CompilerFlags.COROUTINE
# If the code should not be asynchronous, check first it is possible and
# next set the GENERATOR flag if relevant
else:
if sure_async:
raise ValueError(
"The is_async argument is False but bytecodes "
"that can only be used in async functions have "
"been detected."
)
if maybe_generator:
flags |= CompilerFlags.GENERATOR
flags |= bytecode.flags & CompilerFlags.FUTURE_GENERATOR_STOP
return flags