Skip to content

Commit 5a5e047

Browse files
authored
Merge pull request RustPython#4174 from jopemachine/fix-dis
Always insert `None` at `code_stack.constants`
2 parents 3301220 + c39c745 commit 5a5e047

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

compiler/codegen/src/compile.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,10 @@ impl Compiler {
11071107

11081108
let (doc_str, body) = split_doc(body);
11091109

1110+
self.current_codeinfo()
1111+
.constants
1112+
.insert_full(ConstantData::None);
1113+
11101114
self.compile_statements(body)?;
11111115

11121116
// Emit None at end:
@@ -2122,6 +2126,11 @@ impl Compiler {
21222126

21232127
let name = "<lambda>".to_owned();
21242128
let mut funcflags = self.enter_function(&name, args)?;
2129+
2130+
self.current_codeinfo()
2131+
.constants
2132+
.insert_full(ConstantData::None);
2133+
21252134
self.compile_expression(body)?;
21262135
self.emit(Instruction::ReturnValue);
21272136
let code = self.pop_code_object();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from asyncio import sleep
2+
3+
def f():
4+
def g():
5+
return 1
6+
7+
assert g.__code__.co_consts[0] == None
8+
return 2
9+
10+
assert f.__code__.co_consts[0] == None
11+
12+
def generator():
13+
yield 1
14+
yield 2
15+
16+
assert generator().gi_code.co_consts[0] == None
17+
18+
async def async_f():
19+
await sleep(1)
20+
return 1
21+
22+
assert async_f.__code__.co_consts[0] == None
23+
24+
lambda_f = lambda: 0
25+
assert lambda_f.__code__.co_consts[0] == None
26+
27+
class cls:
28+
def f():
29+
return 1
30+
31+
assert cls().f.__code__.co_consts[0] == None

0 commit comments

Comments
 (0)