From a93fd14672eccfe9741082ec475a02da04fe3dc3 Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Mon, 30 Jun 2025 20:57:21 +0530 Subject: [PATCH] Add new loop tests --- ASTree.cpp | 28 +++++++++++++++-- FastStack.h | 11 ++++++- tests/compiled/test_loops3.3.12.pyc | Bin 0 -> 1249 bytes tests/input/test_loops3.py | 34 ++++++++++++++++++++ tests/tokenized/test_loops3.txt | 46 ++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tests/compiled/test_loops3.3.12.pyc create mode 100644 tests/input/test_loops3.py create mode 100644 tests/tokenized/test_loops3.txt diff --git a/ASTree.cpp b/ASTree.cpp index 10f507e..ebd8e81 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -897,7 +897,8 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) case Pyc::INSTRUMENTED_FOR_ITER_A: { PycRef iter = stack.top(); // Iterable - stack.pop(); + if (mod->verCompare(3,12) < 0) + stack.pop(); /* Pop it? Don't pop it? */ int end; @@ -1678,11 +1679,34 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) case Pyc::POP_EXCEPT: /* Do nothing. */ break; - case Pyc::POP_TOP: case Pyc::END_FOR: + { + stack.pop(); + + if ((opcode == Pyc::END_FOR) && (mod->majorVer() == 3) && (mod->minorVer() == 12)) { + // one additional pop for python 3.12 + stack.pop(); + } + + // end for loop here + if (curblock->blktype() == ASTBlock::BLK_FOR) { + PycRef prev = blocks.top(); + blocks.pop(); + + curblock = blocks.top(); + curblock->append(prev.cast()); + } + else { + fprintf(stderr, "Wrong block type %i for END_FOR\n", curblock->blktype()); + break; + } + } + break; + case Pyc::POP_TOP: { PycRef value = stack.top(); stack.pop(); + if (!curblock->inited()) { if (curblock->blktype() == ASTBlock::BLK_WITH) { curblock.cast()->setExpr(value); diff --git a/FastStack.h b/FastStack.h index 45f8ed5..3f9c2c8 100644 --- a/FastStack.h +++ b/FastStack.h @@ -30,14 +30,23 @@ public: { if (m_ptr > -1) m_stack[m_ptr--] = nullptr; + else { + #ifdef BLOCK_DEBUG + fprintf(stderr, "pop from empty stack\n"); + #endif + } } PycRef top() const { if (m_ptr > -1) return m_stack[m_ptr]; - else + else { + #ifdef BLOCK_DEBUG + fprintf(stderr, "top on empty stack\n"); + #endif return nullptr; + } } bool empty() const diff --git a/tests/compiled/test_loops3.3.12.pyc b/tests/compiled/test_loops3.3.12.pyc new file mode 100644 index 0000000000000000000000000000000000000000..efb575c88ee84c87b005b3b5e2e6b21bec868f31 GIT binary patch literal 1249 zcmZ`&OHUJV5TDscS+Lu;ydp_`#AGiefbn1mNfQr5W250>NQ@S?fs)eJ|CU5ZoAdz5 z8jF^T+_^RQ9sB@(fx2)odg4au&5O?byVMfmG&`N0_xxt|n`w>!k^5z3{VM|e;+IR( zeWANc!XenO3(KHD4vu{Tf{mm@(&2zkA)P`xWk9ErP9>fCPqzlPc7(Q0zrdb4K{+48 zs~Z5z8u{t=P)<8mdW8@grUu)(Wvu#@Lsz~{Dp;Rh6bE)#S zr?WUCg5=COodmHuc;WH-;z?xO)!Ig|qe66|OM1A{uSN>l?;{cpp~B&kY&`a5>fEHx zp~8NWs&Hu|6v8}@rDQ~aY%RspMYgz>^YpUF7b_Nen0Gi)QV@*L>vUGcNjB(l#)#G7 z)QmM}KE8DE)G*!bzCq1~Mg>*9+H4=(QL@nTY0))WKD9&dOiSFzwfVSMcYzp()MzI) zl=~okO{wBWe`Ze6qpC76;Y|&yxCDadr-1_5ayeJDE!D$!9u`E5)DGK{Nq*N_kh z2FgOg*LV%eIl+e(VpD`z9WE4Ik9I;3jC^=le|8;1-V!(4eM9WO3s1_0_kpqdaL_SO zof-}vLlt-Evm5{{ra|M5JPS1=hNzCb4Z*`(ip0yq4ga(U&f + +iterable = [ 1 , 2 , 3 ] +for item in iterable : + +pass + + +loop1 ( ) +def loop2 ( ) : + +for i in range ( 2 ) : + +print ( i ) + + +loop2 ( ) +def loop3 ( ) : + +def loop ( ) : + +x = ( 1 , 2 , 3 ) +l = [ ] +for i in x : + +l . append ( i ) + +return l + +return loop ( ) + +loop3 ( ) +def loop4 ( ) : + +for i in range ( 3 ) : + +for j in range ( 2 ) : + +print ( i * j ) + + + +loop4 ( ) +for j in [ 1 , 2 , 3 ] [ : : - 1 ] : + +print ( 'hi' , j )