Merge pull request #4174 from jopemachine/fix-dis

Always insert `None` at `code_stack.constants`
This commit is contained in:
Jeong YunWon
2022-10-01 22:45:08 +09:00
committed by GitHub
2 changed files with 40 additions and 0 deletions

View File

@@ -1107,6 +1107,10 @@ impl Compiler {
let (doc_str, body) = split_doc(body);
self.current_codeinfo()
.constants
.insert_full(ConstantData::None);
self.compile_statements(body)?;
// Emit None at end:
@@ -2122,6 +2126,11 @@ impl Compiler {
let name = "<lambda>".to_owned();
let mut funcflags = self.enter_function(&name, args)?;
self.current_codeinfo()
.constants
.insert_full(ConstantData::None);
self.compile_expression(body)?;
self.emit(Instruction::ReturnValue);
let code = self.pop_code_object();

View File

@@ -0,0 +1,31 @@
from asyncio import sleep
def f():
def g():
return 1
assert g.__code__.co_consts[0] == None
return 2
assert f.__code__.co_consts[0] == None
def generator():
yield 1
yield 2
assert generator().gi_code.co_consts[0] == None
async def async_f():
await sleep(1)
return 1
assert async_f.__code__.co_consts[0] == None
lambda_f = lambda: 0
assert lambda_f.__code__.co_consts[0] == None
class cls:
def f():
return 1
assert cls().f.__code__.co_consts[0] == None