From e5116e126ac314a9846d569d5910164f6bd63d6f Mon Sep 17 00:00:00 2001 From: Francois Chagnon Date: Tue, 13 Oct 2026 14:03:14 -0400 Subject: [PATCH 1/4] Recognize fused local stores in chained assignments --- .github/workflows/assignment_chains.yml | 24 +++++++ src/decompiler.rs | 1 + tests/test_assignment_chains.py | 89 +++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .github/workflows/assignment_chains.yml create mode 100644 tests/test_assignment_chains.py diff --git a/.github/workflows/assignment_chains.yml b/.github/workflows/assignment_chains.yml new file mode 100644 index 00000000..374db366 --- /dev/null +++ b/.github/workflows/assignment_chains.yml @@ -0,0 +1,24 @@ +name: Assignment Chains + +on: + pull_request: + push: + branches: [fix/fused-store-assignment-chains] + +permissions: + contents: read + +jobs: + roundtrip: + runs-on: ubuntu-latest + strategy: + matrix: + python: ["3.11", "3.12", "3.13", "3.14"] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python }} + - run: cargo build --locked + - run: python tests/test_assignment_chains.py 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/test_assignment_chains.py b/tests/test_assignment_chains.py new file mode 100644 index 00000000..f37cd249 --- /dev/null +++ b/tests/test_assignment_chains.py @@ -0,0 +1,89 @@ +"""Compile, decompile and execute original fixtures with CPython 3.11–3.14.""" + +import importlib.util +import marshal +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +BINARY = Path(os.environ.get("PYCDC_BINARY", ROOT / "target/debug/pycdc")) + + +class RoundTrip(unittest.TestCase): + def recover(self, source): + code = compile(source, "fixture.py", "exec", dont_inherit=True) + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "fixture.pyc" + path.write_bytes( + importlib.util.MAGIC_NUMBER + bytes(12) + marshal.dumps(code) + ) + result = subprocess.run( + [str(BINARY), str(path)], capture_output=True, text=True, timeout=10 + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertNotIn("incomplete", result.stderr.lower()) + self.assertNotIn("INCOMPLETE", result.stdout) + original, recovered = {}, {} + exec(code, original) + exec( + compile(result.stdout, "recovered.py", "exec", dont_inherit=True), recovered + ) + return original["select"], recovered["select"] + + def test_long_copy_chains_keep_one_evaluation_and_fused_stores(self): + for size in (3, 4, 5, 6): + names = [f"value{index}" for index in range(size)] + for expression in ("read()", "None"): + source = ( + "def select(read):\n " + + " = ".join(names) + + " = " + + expression + + "\n return " + + ", ".join(names) + + "\n" + ) + original, recovered = self.recover(source) + self.assertEqual(original.__code__.co_code, recovered.__code__.co_code) + self.assertEqual( + original.__code__.co_exceptiontable, + recovered.__code__.co_exceptiontable, + ) + for failure in (False, True): + outcomes = [] + for function in (original, recovered): + events = [] + value = object() + + def read(): + events.append("read") + if failure: + raise ValueError("owned chain") + return value + + try: + result = function(read) + outcome = ( + "value", + all(item is result[0] for item in result), + result[0] is value + if expression == "read()" + else result[0] is None, + ) + except ValueError as error: + outcome = (type(error).__name__, str(error)) + outcomes.append((outcome, events)) + with self.subTest( + size=size, expression=expression, failure=failure + ): + self.assertEqual(outcomes[1], outcomes[0]) + self.assertEqual( + len(outcomes[1][1]), int(expression == "read()") + ) + + +if __name__ == "__main__": + unittest.main() From a109342d87a8d70d7456a2d866bf3b180667aec9 Mon Sep 17 00:00:00 2001 From: Francois Chagnon Date: Tue, 13 Oct 2026 14:19:59 -0400 Subject: [PATCH 2/4] Run version-gated Python regressions in the shared CI matrix --- .github/workflows/assignment_chains.yml | 24 ------------------------ .github/workflows/ci.yml | 17 +++++++++++++++++ tests/test_assignment_chains.py | 5 +++++ 3 files changed, 22 insertions(+), 24 deletions(-) delete mode 100644 .github/workflows/assignment_chains.yml diff --git a/.github/workflows/assignment_chains.yml b/.github/workflows/assignment_chains.yml deleted file mode 100644 index 374db366..00000000 --- a/.github/workflows/assignment_chains.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Assignment Chains - -on: - pull_request: - push: - branches: [fix/fused-store-assignment-chains] - -permissions: - contents: read - -jobs: - roundtrip: - runs-on: ubuntu-latest - strategy: - matrix: - python: ["3.11", "3.12", "3.13", "3.14"] - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python }} - - run: cargo build --locked - - run: python tests/test_assignment_chains.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 96ff9e11..240ed91a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,23 @@ jobs: "$das" tests/corpus/3.12.14/base64.pyc | head -3 cat tests/corpus/3.12.14/base64.pyc | "$bin" - | head -3 + python-regressions: + name: Python ${{ matrix.python-version }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.12", "3.13", "3.14", "3.15"] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - run: cargo build --locked + - run: python -m unittest discover -s tests -p 'test_*.py' -v + clippy: # informational: the codebase carries a style-warning long tail # (see docs/LIMITATIONS.md); this job never blocks diff --git a/tests/test_assignment_chains.py b/tests/test_assignment_chains.py index f37cd249..c20585c3 100644 --- a/tests/test_assignment_chains.py +++ b/tests/test_assignment_chains.py @@ -5,6 +5,7 @@ import os from pathlib import Path import subprocess +import sys import tempfile import unittest @@ -12,6 +13,10 @@ BINARY = Path(os.environ.get("PYCDC_BINARY", ROOT / "target/debug/pycdc")) +@unittest.skipUnless( + sys.implementation.name == "cpython" and (3, 11) <= sys.version_info[:2] < (3, 15), + "requires CPython 3.11–3.14; 3.15 LOAD_COMMON_CONSTANT 7 (None) is not handled", +) class RoundTrip(unittest.TestCase): def recover(self, source): code = compile(source, "fixture.py", "exec", dont_inherit=True) From f3324397df44c9c74b9c2ecada4f3031ecdcd222 Mon Sep 17 00:00:00 2001 From: Francois Chagnon Date: Tue, 13 Oct 2026 14:37:19 -0400 Subject: [PATCH 3/4] Use committed Python fixtures in the existing integration suite --- .github/workflows/ci.yml | 17 ----- tests/fixtures/assignment_chains.py | 40 ++++++++++++ tests/integration.rs | 29 +++++++++ tests/pyc/assignment_chains.3.11.pyc | Bin 0 -> 1848 bytes tests/pyc/assignment_chains.3.12.pyc | Bin 0 -> 1716 bytes tests/pyc/assignment_chains.3.13.pyc | Bin 0 -> 1620 bytes tests/pyc/assignment_chains.3.14.pyc | Bin 0 -> 1622 bytes tests/test_assignment_chains.py | 94 --------------------------- 8 files changed, 69 insertions(+), 111 deletions(-) create mode 100644 tests/fixtures/assignment_chains.py create mode 100644 tests/pyc/assignment_chains.3.11.pyc create mode 100644 tests/pyc/assignment_chains.3.12.pyc create mode 100644 tests/pyc/assignment_chains.3.13.pyc create mode 100644 tests/pyc/assignment_chains.3.14.pyc delete mode 100644 tests/test_assignment_chains.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 240ed91a..96ff9e11 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,23 +38,6 @@ jobs: "$das" tests/corpus/3.12.14/base64.pyc | head -3 cat tests/corpus/3.12.14/base64.pyc | "$bin" - | head -3 - python-regressions: - name: Python ${{ matrix.python-version }} - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - python-version: ["3.12", "3.13", "3.14", "3.15"] - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - run: cargo build --locked - - run: python -m unittest discover -s tests -p 'test_*.py' -v - clippy: # informational: the codebase carries a style-warning long tail # (see docs/LIMITATIONS.md); this job never blocks 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 0000000000000000000000000000000000000000..65e1370941dfacd93a0f789730fa5cca31483c54 GIT binary patch literal 1848 zcmbVN%}*0S9G&UTZa)wMQn5m9mTHXE1Y$HAi5hDNH@R^0LMpqJCQ@Xk7)-Va2NUb%R&o9<0SL)o9%qOxAWfi>)>F(n-y;QhM=t7ut-DqJDS> zu+0+8V_#5xM~~V%Z*ufR)9|>L@QkW4&-VD-9xI8qkZ#3k^Gch)Y4b%Tu);<=y4i45 zLrqm{>y0((s;P=nwYT+Ux87J-T&mRSYV!TZw%J&ct}8QGL_6U^JH#y3rGQS=3MNrpqyQ?6rifH{qAKQ@q;--uD4Eiv+9F62 zqyTO1D0)nyY?-DfqPnss0z9*7c1f_#lOPANahtn}lxZEYklF`V4&y1!Q1<~4lFPTm zjoJ~3V3jQ4?#It^`C_zcUBLbqV-Ip_!m9UWHzl6O`cV?x>P0ms**!E zqs^;|?kkek(PpUHy3FF~6?F9k0BL!lC)w#RZwo7+*YvKng|ER zLopsn#13&Id0Ju)1!WKts3RjDBO#QKQ_eDaOr>Zs=th3Z=oBo}SL&`D#ShoeRUV+D z5Q;2u&Gv-gEGdB%np6SBX;L6Y(bdEd-fq*29%|Ywaf~B^X)=7oj|3Anbqk*w~iimdm+dPl0HbHMl(^Yso;yi|PCeYPAfHrt2^29k$ zobz=5`To|%nz+T*rp%v59rP)lF?|HI+sam70dfR;o{4A*HjY%#Tfa?H9ssnLLt&E& zn^f4O!X_8n7rA(@l+s^Y{q@qx*BQ~alXSO|UY4Y@^fytvOq<{+^>lf~S$pR`mpAY$ s{eP&>fCGbbeqbf|$N`HXli;H?>;Q(>Y1jb_^EB)LhX1`b-bEAr1N9wgmjD0& literal 0 HcmV?d00001 diff --git a/tests/pyc/assignment_chains.3.12.pyc b/tests/pyc/assignment_chains.3.12.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d6d6bf6876c0ee5fd17b65aa365b0ce880e2c6c7 GIT binary patch literal 1716 zcmaKty>HV%7{>47v-43}^&^zfn#c_Z5fJSF2!sGB5@PJqjim~;O(W5i-leFO94P~X zM@t=AsS{(xpTgp?WQ36gHCrd1_nf*-nsCxRI$eBszvukC&X38-g25R3^KJRX)UVmM zzcEf5?~i&KT%nYIz|AQKqAvBpmL>Itigu8>~c?ecn;f9UcJCGfa|c7C_zsg_#t z8+)x?>8X{vQjL$zEw9;H-`K1-nriv;e%EYmO3#x8ETW@T8PZ`tr;7(xEap2kNXWz5 z>BNn&9?gW$JF7iB!3MbLM9uvGgVEezH`*d$4}>q)n4}ewmR3z^(%2%&lH?#=?kbul zD~`<5jPSR2rJTZ$9#HooKrNOI-o;{pP!~c@Qj2(>H>!m%V{ubX;L!nO+F2tnV=x{W z*rIJ-U~c?$A5EX)H@`|>;Zd|0s7Y=LI^*lj?WQN^@xv{2WkWjbbh;ReBC!^SVtyS;V1JbFYB|8>2G26s!)BCr_2O4YAmInGE zrbeLyp9(U?M@bIG*cxj<&SB4MMVUi{6Bl&r^hr@qAaG$jv2e(RLoOV0;ZTa5|D|}f zn$zD>{gu+SuIod`PSU%TbXk(l(%(LvG97~R+^el^clVR`M&7}%^uM4#5q}NN`I(jB Vb7zGNpC`FZVv}5%>(QLv?jMFpOp5>j literal 0 HcmV?d00001 diff --git a/tests/pyc/assignment_chains.3.13.pyc b/tests/pyc/assignment_chains.3.13.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e90e0a0b9b7f30fe845125e4c4d1c58f253f1d4d GIT binary patch literal 1620 zcmaKs%TC)s6ozNwv2&qR5K`&}H*y2LM7^*{RjDeKSkNV1SV7sL01iYd24+GB_?#F`E@j&8GmQyKQo-3E*Ok4yZ!z1>JQ`3FT_dX zqi9%1aK=1VW|Dav%DoIU;~7xH6HwuqP}8%ZmY0QQcXPG;_VpgL75#|%umU+_4OV4u z5m8&=0bk*Vi_K!gs2fY{g0Ek&nuvt-TRRbNMSLsb+e%=nixw?*ebrUV?aoQ}So&(Y zrBr7(*!P3(=JsBz6R0KdUo%(^)SYfXZc z)7UKHzM>p7af00W>dRS-(hT(+0%Y4c$C*|LxkAX5K1GB0bO%TMl~CBX$!Tv*{tChW z$O8fT4We&u)U7f4?49&-2D?VQt>^~g6h=81s&gVs7%if!*AS%OxaTxLx9QiQ6Stx|3YFUd!qCPQNL-O100ml_cGqq+^lv5B*YU_rzD6`>=no obNt2sC?Df1z1zAnejA+gD=Wk2t_m4GPx6SwA-OR&q%pnSUr+HJ6951J literal 0 HcmV?d00001 diff --git a/tests/pyc/assignment_chains.3.14.pyc b/tests/pyc/assignment_chains.3.14.pyc new file mode 100644 index 0000000000000000000000000000000000000000..76a7ae4b9ff521a6d8934330558e9c4c705706e3 GIT binary patch literal 1622 zcmZ{kPfrs;7{+J1v)exifl^_GwOQJTHmDp*G!Zr7fJY8{Xpf}A7Me&YGez;%V~oeuNZGHWxBlX5RXn(z!#!xN;!Gf7R)BDK6EY0^s-St)&R9|gg%71~{%q{Av~ zjcrj6-!^aYZ7z$|Y{e)WMRv{iu31ThLi)ACkk>-~IphaQ(A0I;b;DxYS8cUfZ(Ov` zrLQ(?N;Qs}Cw{ZNe{fuDG}Y_Y_t0z~OW&6nT7+%Y3Q33kn$KNYfmrJ7kaT%}=235< z`?j-pM}OG}FLu+s1-)>Rx0N8~t!lQqRTf1iF;`+y$&@Dg79Y zKRuUnj=GqjToMAz*_WRJu>!!AE{E(yl6k5pPvdiCpKvG0*vDhii(h{yJNPr9z>RlfI?(o--D;f zk`Xt~b{$hTNQm2U1L1<(1-A=s7p`=txpJ?R((j#qQ*@PTpKB{g+?>R*NbI3sD(#-= uElRyVIXyi8;(wG+=`Y@GT^WB2&iSpC;ET7J1Yd$YfjE$%vA-D8;r;l{J= literal 0 HcmV?d00001 diff --git a/tests/test_assignment_chains.py b/tests/test_assignment_chains.py deleted file mode 100644 index c20585c3..00000000 --- a/tests/test_assignment_chains.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Compile, decompile and execute original fixtures with CPython 3.11–3.14.""" - -import importlib.util -import marshal -import os -from pathlib import Path -import subprocess -import sys -import tempfile -import unittest - -ROOT = Path(__file__).resolve().parents[1] -BINARY = Path(os.environ.get("PYCDC_BINARY", ROOT / "target/debug/pycdc")) - - -@unittest.skipUnless( - sys.implementation.name == "cpython" and (3, 11) <= sys.version_info[:2] < (3, 15), - "requires CPython 3.11–3.14; 3.15 LOAD_COMMON_CONSTANT 7 (None) is not handled", -) -class RoundTrip(unittest.TestCase): - def recover(self, source): - code = compile(source, "fixture.py", "exec", dont_inherit=True) - with tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "fixture.pyc" - path.write_bytes( - importlib.util.MAGIC_NUMBER + bytes(12) + marshal.dumps(code) - ) - result = subprocess.run( - [str(BINARY), str(path)], capture_output=True, text=True, timeout=10 - ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertNotIn("incomplete", result.stderr.lower()) - self.assertNotIn("INCOMPLETE", result.stdout) - original, recovered = {}, {} - exec(code, original) - exec( - compile(result.stdout, "recovered.py", "exec", dont_inherit=True), recovered - ) - return original["select"], recovered["select"] - - def test_long_copy_chains_keep_one_evaluation_and_fused_stores(self): - for size in (3, 4, 5, 6): - names = [f"value{index}" for index in range(size)] - for expression in ("read()", "None"): - source = ( - "def select(read):\n " - + " = ".join(names) - + " = " - + expression - + "\n return " - + ", ".join(names) - + "\n" - ) - original, recovered = self.recover(source) - self.assertEqual(original.__code__.co_code, recovered.__code__.co_code) - self.assertEqual( - original.__code__.co_exceptiontable, - recovered.__code__.co_exceptiontable, - ) - for failure in (False, True): - outcomes = [] - for function in (original, recovered): - events = [] - value = object() - - def read(): - events.append("read") - if failure: - raise ValueError("owned chain") - return value - - try: - result = function(read) - outcome = ( - "value", - all(item is result[0] for item in result), - result[0] is value - if expression == "read()" - else result[0] is None, - ) - except ValueError as error: - outcome = (type(error).__name__, str(error)) - outcomes.append((outcome, events)) - with self.subTest( - size=size, expression=expression, failure=failure - ): - self.assertEqual(outcomes[1], outcomes[0]) - self.assertEqual( - len(outcomes[1][1]), int(expression == "read()") - ) - - -if __name__ == "__main__": - unittest.main() From d1b139d28882fbfdf3c2a7fc1b94bbc7c74541f6 Mon Sep 17 00:00:00 2001 From: Francois Chagnon Date: Tue, 13 Oct 2026 18:31:17 -0400 Subject: [PATCH 4/4] Add harness coverage for the reconstruction fix --- tests/behavior/cases/b_assignment_chains.py | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/behavior/cases/b_assignment_chains.py 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())