forked from Rust-related/RustPython
Merge pull request #4174 from jopemachine/fix-dis
Always insert `None` at `code_stack.constants`
This commit is contained in:
@@ -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();
|
||||
|
||||
31
extra_tests/snippets/code_co_consts.py
Normal file
31
extra_tests/snippets/code_co_consts.py
Normal 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
|
||||
Reference in New Issue
Block a user