diff --git a/src/decompiler.rs b/src/decompiler.rs index 33a3465d..a413e0ee 100644 --- a/src/decompiler.rs +++ b/src/decompiler.rs @@ -42238,6 +42238,7 @@ impl<'a> Ctx<'a> { | Some(Op::STORE_DEREF) | Some(Op::STORE_GLOBAL) | Some(Op::STORE_FAST_MAYBE_NULL) + | Some(Op::STORE_FAST_STORE_FAST) ) }; if is_store(self.instrs.get(ci + 1).map(|x| x.op)) { diff --git a/tests/behavior/cases/b_assignment_chains.py b/tests/behavior/cases/b_assignment_chains.py new file mode 100644 index 00000000..f43b9930 --- /dev/null +++ b/tests/behavior/cases/b_assignment_chains.py @@ -0,0 +1,26 @@ +# MIN_VERSION: 3.13 + + +def three(read): + a = b = c = read() + return a, b, c + + +def six(read): + a = b = c = d = e = f = read() + return a, b, c, d, e, f + + +def three_none(): + a = b = c = None + return a, b, c + + +events = [] +def read(): + events.append("read") + return len(events) + +print(three(read), events) +print(six(read), events) +print(three_none()) diff --git a/tests/fixtures/assignment_chains.py b/tests/fixtures/assignment_chains.py new file mode 100644 index 00000000..c2a067e0 --- /dev/null +++ b/tests/fixtures/assignment_chains.py @@ -0,0 +1,40 @@ +# COPY-backed chains; CPython 3.13+ can fuse the final two local stores. + +def three(read): + a = b = c = read() + return a, b, c + + +def four(read): + a = b = c = d = read() + return a, b, c, d + + +def five(read): + a = b = c = d = e = read() + return a, b, c, d, e + + +def six(read): + a = b = c = d = e = f = read() + return a, b, c, d, e, f + + +def three_none(): + a = b = c = None + return a, b, c + + +def four_none(): + a = b = c = d = None + return a, b, c, d + + +def five_none(): + a = b = c = d = e = None + return a, b, c, d, e + + +def six_none(): + a = b = c = d = e = f = None + return a, b, c, d, e, f diff --git a/tests/integration.rs b/tests/integration.rs index f4af5082..13ba7a43 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -151,6 +151,35 @@ fn py312_hello() { ); } +#[test] +fn assignment_chains() { + // 3.13 introduces fused stores; older fixtures check COPY-only chains. + for version in ["3.11", "3.12", "3.13", "3.14"] { + let fixture = format!("assignment_chains.{version}.pyc"); + let (src, clean) = decompile_fixture(&fixture); + assert!(clean, "{fixture}: output marked incomplete:\n{src}"); + for (name, names) in [ + ("three", &['a', 'b', 'c'][..]), + ("four", &['a', 'b', 'c', 'd'][..]), + ("five", &['a', 'b', 'c', 'd', 'e'][..]), + ("six", &['a', 'b', 'c', 'd', 'e', 'f'][..]), + ] { + let names: Vec = names.iter().map(char::to_string).collect(); + let targets = names.join(" = "); + let values = names.join(", "); + assert_contains( + &src, + &[ + &format!("def {name}(read):\n {targets} = read()\n return {values}"), + &format!("def {name}_none():\n {targets} = None\n return {values}"), + ], + &fixture, + ); + } + assert_eq!(src.matches("read()").count(), 4, "{fixture}:\n{src}"); + } +} + #[test] fn py313_hello() { let (src, clean) = decompile_fixture("hello.3.13.pyc"); diff --git a/tests/pyc/assignment_chains.3.11.pyc b/tests/pyc/assignment_chains.3.11.pyc new file mode 100644 index 00000000..65e13709 Binary files /dev/null and b/tests/pyc/assignment_chains.3.11.pyc differ diff --git a/tests/pyc/assignment_chains.3.12.pyc b/tests/pyc/assignment_chains.3.12.pyc new file mode 100644 index 00000000..d6d6bf68 Binary files /dev/null and b/tests/pyc/assignment_chains.3.12.pyc differ diff --git a/tests/pyc/assignment_chains.3.13.pyc b/tests/pyc/assignment_chains.3.13.pyc new file mode 100644 index 00000000..e90e0a0b Binary files /dev/null and b/tests/pyc/assignment_chains.3.13.pyc differ diff --git a/tests/pyc/assignment_chains.3.14.pyc b/tests/pyc/assignment_chains.3.14.pyc new file mode 100644 index 00000000..76a7ae4b Binary files /dev/null and b/tests/pyc/assignment_chains.3.14.pyc differ