From 44a847faeacf6f68fd9b6d97ba9e6733956daa03 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Wed, 18 Oct 2023 13:40:59 -0400 Subject: [PATCH 1/4] Add support for 3.6+ default arguments --- ASTree.cpp | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index 63a894e87..7cac549cf 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1545,15 +1545,42 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) } ASTFunction::defarg_t defArgs, kwDefArgs; - const int defCount = operand & 0xFF; - const int kwDefCount = (operand >> 8) & 0xFF; - for (int i = 0; i < defCount; ++i) { - defArgs.push_front(stack.top()); - stack.pop(); - } - for (int i = 0; i < kwDefCount; ++i) { - kwDefArgs.push_front(stack.top()); - stack.pop(); + + if (mod->minorVer() <= 5) { + const int defCount = operand & 0xFF; + const int kwDefCount = (operand >> 8) & 0xFF; + for (int i = 0; i < defCount; ++i) { + defArgs.push_front(stack.top()); + stack.pop(); + } + for (int i = 0; i < kwDefCount; ++i) { + kwDefArgs.push_front(stack.top()); + stack.pop(); + } + } else { + if (operand & 0x08) { + stack.pop(); + } + + if (operand & 0x04) { + stack.pop(); + } + + if (operand & 0x02) { + PycRef defaultsDict = stack.top().cast(); + stack.pop(); + + for (PycRef value : defaultsDict->values()) + kwDefArgs.push_front(value); + } + + if (operand & 0x01) { + PycRef defaultsTuple = stack.top().cast()->object().cast(); + stack.pop(); + + for (PycRef value : defaultsTuple->values()) + defArgs.push_back(new ASTObject(value)); + } } stack.push(new ASTFunction(fun_code, defArgs, kwDefArgs)); } From 0ab4f8a6172cf7cc562ac352c2e58eceabe75660 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Wed, 18 Oct 2023 14:22:51 -0400 Subject: [PATCH 2/4] Fix free-var closure default argument for 3.6+ --- ASTree.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ASTree.cpp b/ASTree.cpp index 7cac549cf..5041b4653 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1469,6 +1469,7 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) break; case Pyc::LOAD_CLOSURE_A: /* Ignore this */ + stack.push(new ASTNode()); break; case Pyc::LOAD_CONST_A: { From bde5464c3cd27b33a35003fd50a8ec447d2422e9 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Thu, 19 Oct 2023 14:13:29 -0400 Subject: [PATCH 3/4] Better version check. Better LOAD_CLOSURE handling --- ASTree.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index 5041b4653..5e0f729ef 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1467,10 +1467,6 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) case Pyc::LOAD_BUILD_CLASS: stack.push(new ASTLoadBuildClass(new PycObject())); break; - case Pyc::LOAD_CLOSURE_A: - /* Ignore this */ - stack.push(new ASTNode()); - break; case Pyc::LOAD_CONST_A: { PycRef t_ob = new ASTObject(code->getConst(operand)); @@ -1488,6 +1484,7 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) } break; case Pyc::LOAD_DEREF_A: + case Pyc::LOAD_CLOSURE_A: stack.push(new ASTName(code->getCellVar(mod, operand))); break; case Pyc::LOAD_FAST_A: @@ -1547,7 +1544,7 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) ASTFunction::defarg_t defArgs, kwDefArgs; - if (mod->minorVer() <= 5) { + if (mod->verCompare(3, 6) < 0) { const int defCount = operand & 0xFF; const int kwDefCount = (operand >> 8) & 0xFF; for (int i = 0; i < defCount; ++i) { From 7f4564ed87922138222561841024405c41df51b2 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Thu, 19 Oct 2023 15:48:47 -0400 Subject: [PATCH 4/4] Fix test_functions_py3 tests --- tests/compiled/test_functions_py3.3.0.pyc | Bin 2099 -> 3237 bytes tests/compiled/test_functions_py3.3.4.pyc | Bin 1546 -> 3237 bytes .../test_functions_py3.6.15.pyc} | Bin 2289 -> 2278 bytes tests/tokenized/test_functions_py3.txt | 36 ++++++++++++++++-- 4 files changed, 32 insertions(+), 4 deletions(-) rename tests/{xfail/test_functions_py3.3.7.pyc => compiled/test_functions_py3.6.15.pyc} (82%) diff --git a/tests/compiled/test_functions_py3.3.0.pyc b/tests/compiled/test_functions_py3.3.0.pyc index f29f671488bfbda506dbd65b4e3d02494fa5da8d..a5a6b3091160624963bed41905d589185fb434aa 100644 GIT binary patch literal 3237 zcmcguO>fgc5FN)&)20b&n|@Fz6bdbYqKKr9Eg^)EdfO_H6?I>Cb>6 zK$`|RKvRwlzy-j(922+*Sde2Aa0zf(jwzrBxFW}Sz!G2?@E%|VhB;`{BO$U1xF(T8 zgscNrB|@n-05@fZWxy>Nunl;h5Ti}WLu3c=fkajS9|G>mcuF45J_39!XNjG_J-{b0 zG@wmNC?Eu%0`3!Nv@3wm0H4bYBrMEu0C*@ftjP>70BbS>>5MbH1bihkQ~{6RtoEAx z8#9%xgcCM(ff_RPtj5S`jT)F}f^EX;U-})te|`~MiGH_t<)2^Q9@npL1KKPYyy0hk z9)h6|!4O~E^PNh@lL4!6Z@xAX>bqGR{LmO{Lr4t-b3c`p17g-_(42+7S(v16#tuh| z{>UN!a(Hyn{(rKXlx%S97Ds3mr15^5%DYIj-+qwYvoU*RjoFKtBf~$ZF^?MV2Xjgg ztC|GSWiPv&Ua%c}vEz)U+Rp(2M_=@MviIDZu)lPpVwlMs%dJwjs2Dl(z;ZP`soOGD z&wO54-j=4wixu%Zl^sgmE)KZUp*gqF5<8kSAuEX`MfVBEq@EqNm^N*eqtkMCCuv)5 zwEG#*dwUvvcz$@r(fLU&5qW;ps^|2*CH6H1LcXX+6{PweD;PO6v-oej2b1)+n<2f& zV#kiC?PW+Wjx_o#&BwY%kMZicuDAwLtB(V;*TH-*CdX||DshrB$W)1EX-blc$W)1N zG$l!KWU2(;YD)05wn3o!vJ1OL*fnKQ&{v7-J=Ke_w-jzE+)}uD?+#iDZzzU|B#xJnf%sG{*~gkVs6U+D&}9t CUI1wT literal 2099 zcmbtV%}(1u5FW<~BxzEpkPwxsR;pAfr_clk4n+uw0|z8};1;PuYcEt$X%gXBg*%VY zTc3dk;LSSUcsDj7OR2q9c78nG@0*|4Us~go+uva ziYOJ*z9>HFswh>`HBoA$S4gjtuG3wGhODP%LrnIkW>d^M=~v=u@w48rVa>LfO>4du zlOxmMH8ESH*XgYDhIu~9z+=Y|GN`5*oOR$i?eInmTk@WL-R|~iJWXcXvx|6kewmD; z*=RD3&!@LL-RW&o;gFlpI3k8hUIZNr5sJgc&7(#LEqt#Cf+`2SFSKJ#WrHF315wP5mQVNY;xSz`ruh423O#nmnZ z9JX=z0=gq3S)Xz}45GIgGuwlRUGotJ!z08v#~20X z&K3Cc4AeWEoSR{w*%@{=N@m!gmP>ySe#j93c%FbW?Ky$m;1)=KSAAT9j>N#inLYxZ zj76#W+*6-2BBvd^Sj>kc6}?pS1(jcUMutX4sSJ!iF#f>!2gW}z{#|45sjOzAz2r+% ZXKwpHCpx*hI6#%+E#4Y_cCGo@^%pD4S-b!M diff --git a/tests/compiled/test_functions_py3.3.4.pyc b/tests/compiled/test_functions_py3.3.4.pyc index 6c0fe16ae2f2cfd256cb14efcf851f0f8ebd564e..045bd738460e2d14d26521cd9633ab067b29837c 100644 GIT binary patch literal 3237 zcmcguO;6iE5FKMeAS4YCK1xeVOM#}PN}J%=QdL!zIP_2n4%~7<_D-mxQWD`<4fp(x z{>A=<&aBsVlC49NgB`rPydBSb`*zmy?_#NN*sHhyEC2`?xpFlALbq=l07!oZ3;|j+ zSO7HT*Z^Dv%*iop%s|4z3`c;+GQ+ye@EWixGmy?W!yCZ2GD8LM1TL%Z$iFdD z$x1k5Qx~ZrQ!lHGoL8xVi6+<~toFX&_WM`Y!L8_by0`vS@BXybyANoyVDN#T^?3+} zLIgv6anE-u8BYeR!oB-mPpBVet@A@;taTwZ5X}8lRt|_+qd{X9`bK_|z7abdE&3~m z{LA6dMf?BBYErVnsaqJKRglK}Z7S~)&HngBcF)G_l{98AW{wQMNMjx~+z;lIAl5Vq zqQhQxIK5yy_+rNyO|_o`0*=1!c4hCmcVU0yM#V6bIhI?YY*8_C=7HsEdQ!J#s-F40 zvb=3gkrylCcPcxSx?LRbphI(Rqa}7VX+l;KON#C@j!8W`Y%y)xEJvs5?oHA*-DvkS zp!fDQ`tbblilg(BS|alNs8!GDds7@}3WR)7k19y@JytMsXlC)>c8@0MZ8t-DkHwB1 zQQOOqUYuz3S(=YEjUMCGb6s%_q*fmXXs?6$UQCW#pH$)`Wss>7&(f466_KeD;b=;d z;>c79e$SHk+Yydo zK1aR7k2MK8s~i74ZX7RZ=zac|x63L;Mn{CaD&wB~r_D?^B$c2vqS2UC=H`LCbcUH2YNzhoxPar$uF*& zPIx3Jm#aMMc&Fb;L!Ozxd)@B-^>93$>`yL|$@%4U7*DQ8!{mH?chnu4~q^}NUu{0PAJU~JQSYkgGI6L@W4D>Y?_CY-?QV%Qq&``_l z=glx_zQY8o8FreLR{hKrDay!b!TAgOA$$A+E$u6j*H`wHdhTmpC=i{ItA9sIB^Zfs`s~7KBK~Rw7B|Cu#W}IMF@w{Y3 z6k2l}?ON1z_H&@wT6b&w4Cvn4(~`dpjUCi@Gc{f&jnglORpT6KY_^uoK<_k4)yPbPVV!v?2DPWv X*PlmfdUNpxQnJma8O-i>FuVT&tuVS8 diff --git a/tests/xfail/test_functions_py3.3.7.pyc b/tests/compiled/test_functions_py3.6.15.pyc similarity index 82% rename from tests/xfail/test_functions_py3.3.7.pyc rename to tests/compiled/test_functions_py3.6.15.pyc index 763ba8e6b55cd317a4fde46ed52e2807e30412cf..5a246cbaea860aa372b62c8421023cb1ec305041 100644 GIT binary patch delta 25 gcmew;_)L()n3tDJrNA(CBabE{lepeyJI2lI09i){T>t<8 delta 36 qcmaDR_)(C@iICc%nO9I+ve}q%GdlpSKMBkL diff --git a/tests/tokenized/test_functions_py3.txt b/tests/tokenized/test_functions_py3.txt index 8308e0b70..b9d25db24 100644 --- a/tests/tokenized/test_functions_py3.txt +++ b/tests/tokenized/test_functions_py3.txt @@ -34,18 +34,46 @@ def x4c ( foo , bar = 1 , bla = 2 , * args , ** kwargs ) : pass -def x5a ( * , bar = 1 ) : +def x5a ( * , bar ) : pass -def x5b ( * , bar = 1 , ** kwargs ) : +def x5b ( * , bar = 1 ) : pass -def x6a ( foo , * , bar = 1 ) : +def x5c ( * , bar = 1 , ** kwargs ) : pass -def x7a ( foo , * , bar = 1 , ** kwargs ) : +def x6a ( foo , * , bar ) : + +pass + +def x6b ( foo , * , bar = 1 ) : + +pass + +def x6c ( foo = 1 , * , bar ) : + +pass + +def x6d ( foo = 1 , * , bar = 2 ) : + +pass + +def x7a ( foo , * , bar , ** kwargs ) : + +pass + +def x7b ( foo , * , bar = 1 , ** kwargs ) : + +pass + +def x7c ( foo = 1 , * , bar , ** kwargs ) : + +pass + +def x7d ( foo = 1 , * , bar = 2 , ** kwargs ) : pass