From de78e1b88236ebf3e51e5e296f724c395ed8a820 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Thu, 10 Oct 2019 14:47:48 -0700 Subject: [PATCH] Unmangle private names ('__' prefix) in class declarations. Fixes #166. --- ASTree.cpp | 5 +++++ pyc_string.h | 8 ++++++++ tests/compiled/private_name.1.5.pyc | Bin 0 -> 956 bytes tests/compiled/private_name.2.2.pyc | Bin 0 -> 1066 bytes tests/compiled/private_name.2.5.pyc | Bin 0 -> 971 bytes tests/compiled/private_name.2.7.pyc | Bin 0 -> 971 bytes tests/input/private_name.py | 28 ++++++++++++++++++++++++++++ tests/tokenized/private_name.txt | 26 ++++++++++++++++++++++++++ tests/xfail/private_name.3.0.pyc | Bin 0 -> 1093 bytes tests/xfail/private_name.3.7.pyc | Bin 0 -> 868 bytes 10 files changed, 67 insertions(+) create mode 100644 tests/compiled/private_name.1.5.pyc create mode 100644 tests/compiled/private_name.2.2.pyc create mode 100644 tests/compiled/private_name.2.5.pyc create mode 100644 tests/compiled/private_name.2.7.pyc create mode 100644 tests/input/private_name.py create mode 100644 tests/tokenized/private_name.txt create mode 100644 tests/xfail/private_name.3.0.pyc create mode 100644 tests/xfail/private_name.3.7.pyc diff --git a/ASTree.cpp b/ASTree.cpp index d8905e4..e58ca27 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1942,6 +1942,11 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) break; } + // Return private names back to their original name + const std::string class_prefix = std::string("_") + code->name()->strValue(); + if (varname->startsWith(class_prefix + std::string("__"))) + varname->setValue(varname->strValue().substr(class_prefix.size())); + PycRef name = new ASTName(varname); if (curblock->blktype() == ASTBlock::BLK_FOR diff --git a/pyc_string.h b/pyc_string.h index c7fb267..2cb3b5e 100644 --- a/pyc_string.h +++ b/pyc_string.h @@ -14,10 +14,18 @@ public: bool isEqual(PycRef obj) const override; bool isEqual(const std::string& str) const { return m_value == str; } + bool startsWith(const std::string& str) const + { + return m_value.substr(0, str.size()) == str; + } + void load(class PycData* stream, class PycModule* mod) override; int length() const { return (int)m_value.size(); } const char* value() const { return m_value.c_str(); } + const std::string &strValue() const { return m_value; } + + void setValue(std::string str) { m_value = std::move(str); } private: std::string m_value; diff --git a/tests/compiled/private_name.1.5.pyc b/tests/compiled/private_name.1.5.pyc new file mode 100644 index 0000000000000000000000000000000000000000..67bcf334de1f7faa5547ba1de32c22f2ee10479b GIT binary patch literal 956 zcmbu7%}&BV5Xb-9g%&C$5HLdG1M~!s-~*TtBPW}fO~aPh2$e9E#M2)1B|LfaIh|b) zMMX{M$7KHdF~6DJc^*Zfe0{l|fFH>}U9d6y`Ov@=&;n1uBXE`Q=m|0>5&_dgCXjn} zPME$EaxG4ss5qfLjAIwM%8}1!Dba)-2zVQq4-;%K77>ECFw!C-Xz6?prUhMbzJ%#O zSCLd;y3jt7D$Fr-fW*h;IIeN@(L^voa7h%hWPqvJg2u71voRYumn!U0Hk(q9oxxz3 z7mNFHxLD?^w9HhI&a=Vdfox2r@**#l3P2u9DZVg5n4?1&J_ns=<;|TA_YfkO<{^Zp zts*bVY+0l;S6j4))PiXrOj4guvGq>M?G13-u(53s2W)oEmp%@HzGqBbUawpqsT}~> zXsdKdkzKH@(vEa~*uvwk3x2=%ZO{5}xk%BF>$fiIVienX+Abnn+U+0tyOMS?|3@A+ Q@EPY|`>@|`h)6{84YkF@=>Px# literal 0 HcmV?d00001 diff --git a/tests/compiled/private_name.2.2.pyc b/tests/compiled/private_name.2.2.pyc new file mode 100644 index 0000000000000000000000000000000000000000..77f1c85ce027fdd507ded692576db7c3ef81a4a1 GIT binary patch literal 1066 zcmb_bQBJ}@5PjQ1fl3KPjF@3#04ByrVqz1sY1k4Qp%SK=__ZH;5C6QKcW`Dw z5EcEQl*!EO?tA-YrYG-F=)b+*jKGi2f4X2}`3+!zF<=y4fM?)3;p`zY7bFU{hfE>& zWKG#47x;_d*aa0A7!SkPAy*msY?2a<*nxrvx)QPgB_M#cC0XLZhTwxtocOR2jPLvk zY#XNH{3`4|OchB0+l2{`)L;)_YDnt19L9BqJ|8LOQ7nu?Vd5Qiku{8!tj28MT&l1~ z*<`#(NuGLa`u#y(%pS_YY@R=+Wu}XCn)PQ-BKj`KySE#*fZ?ZE zd3$fdEnE?7Yd2giQI;2FHZRhN(;IE!Y{PbT$f>{9xc$kQOQ(3q*vJsY4K~Z_OE0u` zoz;4JZyqKRe(emc3r9b7QJ1l_n@GJuPQvt<&MA@&t6E5A|Cb}2UyqIR{+^xHcM!nc zs-c?x;@40Obsoz)tu~Pq;O-wKPTYouSW31$yTV#u^Y0r91Se4S}|eH*0m<@sht%}e6@49iVm3_g;KpeLe7qH9HDgQo;pL1o0M zU{CJ6ICv(aX%?e|@4#GEg>yTEfO3u%5U7V@00E(*hNS?Ff)euvIMg8%lS;^RNOj31 zq$!W&wRR%-B^&D`vy4^~wyAznvXk6CIwI456<)-jf*Tvk9 zmJhy%pw?wwdaW7dA{Y|;6La3dzO)3nbL;OGCWZI6tOF3xn76CCs6xMk|6%ISGd^sY zB~J=B3P!?YeVAtcd>UGN9*WlUg;`hlnR9B+sX3pjKjvt-X!;AnbIPVzdG4?6n3dr- zk3esaD|kQNkQ^n5&&}~b6VaF3cOnweTs-~*gf!0`-nU!MIr1Br>*0aZ?EoeL%EIud IFH(`pcgkp_2><{9 literal 0 HcmV?d00001 diff --git a/tests/compiled/private_name.2.7.pyc b/tests/compiled/private_name.2.7.pyc new file mode 100644 index 0000000000000000000000000000000000000000..68f964897e017ece720a2295f6d1df832b8ddb79 GIT binary patch literal 971 zcmchW!A=4(5QeAQWr0mF8a((2dx94*#OTq)Lpj;RG#k3c>aq~Fka*Vj@a7Bp0{&B0 zRug0Jpg@P&nc@FB(<0vnseFC8nNjnS_&&p`ComQtNmkG^(G$_NCMtrb1r>tsBaRC8 zin|$TDzqVtJ48Qpa z;_Yz-@5dXWAVGX?H4iiqeYt%nA`#8S<3B(s^W5P>yJf|h-@w8+PPYS?1ZWQm!=t`P HMJnF`JuahO literal 0 HcmV?d00001 diff --git a/tests/input/private_name.py b/tests/input/private_name.py new file mode 100644 index 0000000..dba583a --- /dev/null +++ b/tests/input/private_name.py @@ -0,0 +1,28 @@ +# Python mangles methods with a leading double-underscore with a leading +# _ prefix. This should be removed when emitting the source +# +# Valid Pythons: all + +class Klass: + def __init__(self): + pass + + def method(self): + pass + + def _internal_name(self): + pass + + def __private_name(self): + pass + + var = 1 + _internal_var = 2 + __private_var = 3 + +k = Klass() +k.method() +k._internal_name() + +# The following is not accessible due to mangling +k.__private_name() diff --git a/tests/tokenized/private_name.txt b/tests/tokenized/private_name.txt new file mode 100644 index 0000000..b991b4a --- /dev/null +++ b/tests/tokenized/private_name.txt @@ -0,0 +1,26 @@ +class Klass : + +def __init__ ( self ) : + +pass + +def method ( self ) : + +pass + +def _internal_name ( self ) : + +pass + +def __private_name ( self ) : + +pass + +var = 1 +_internal_var = 2 +__private_var = 3 + +k = Klass ( ) +k . method ( ) +k . _internal_name ( ) +k . __private_name ( ) diff --git a/tests/xfail/private_name.3.0.pyc b/tests/xfail/private_name.3.0.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3376af16f17a385a88de5e1928ea6196f0fd31cc GIT binary patch literal 1093 zcmcgrO-sW-5Ph4ZX&bCm^x#j(sr>;J5kK%C$> zONDx|8}{X8lASm6b~8HaMgG_4;{+SSCw@h<91+|r-~&(sD-S84ffbMnXn_}xxnSn6 zS>TxRFpfFVL=f=cJI`yH z+$H?k7~Uo+ExRP;bf#?1kDHrj8!zo{i}s9HKIB%*@VUxj-39ogyQx2 zsw>H7lPsrwBxcrZC6${{t7b QxtFd$_P2l16_JSiZ%Jm$>;M1& literal 0 HcmV?d00001 diff --git a/tests/xfail/private_name.3.7.pyc b/tests/xfail/private_name.3.7.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2ca40c36e21f2c7765196e9d78ed3f42304a555d GIT binary patch literal 868 zcmbtSy-ve05VrHzG&B*24!lB^l#u};1Oo$77dA`Cih4#wq-k-|O0bsq02U-(DJxT7 zfr&e(zX*w;j_%IRoxksXd=Z8L!)1TG-g1kvFFGujjhl0n@)*?>0|xvBWo8Mc*o+CT z_?z|4I74eMtV0&88y4FGytP`0-C&*IM&Vsx!U7J~3llcj;GlKD1rMzYE%4EL&=xHS zgb#b74IRV`Yxg=&%W}XMz+_1-QA!6DFbw1UPgJ#2s{Bq9vr*qiNz%)T@ucdFbv8{a zC5v>ZlJQgROPLi}C1s0n=mz7@k)cYKu75cVNz661ZOy7FtJ)i?>V5>&>m_`rfo~i; zfk&8prF4Bj+en;I`=9Y{GG!6c`B zN6N=Znl~QpVyyN^(MUvBnnL6jbZt*lEy8v6bUgeWi9a`{VO|4`L{nZwP)4YH?%U1h maEBXTKX#3+`NKL9?bFrOlWKNm!v1Mf$(g~Vu;{?!g!=;pSg6ea literal 0 HcmV?d00001