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 0000000..67bcf33 Binary files /dev/null and b/tests/compiled/private_name.1.5.pyc differ diff --git a/tests/compiled/private_name.2.2.pyc b/tests/compiled/private_name.2.2.pyc new file mode 100644 index 0000000..77f1c85 Binary files /dev/null and b/tests/compiled/private_name.2.2.pyc differ diff --git a/tests/compiled/private_name.2.5.pyc b/tests/compiled/private_name.2.5.pyc new file mode 100644 index 0000000..e2f083c Binary files /dev/null and b/tests/compiled/private_name.2.5.pyc differ diff --git a/tests/compiled/private_name.2.7.pyc b/tests/compiled/private_name.2.7.pyc new file mode 100644 index 0000000..68f9648 Binary files /dev/null and b/tests/compiled/private_name.2.7.pyc differ 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 0000000..3376af1 Binary files /dev/null and b/tests/xfail/private_name.3.0.pyc differ diff --git a/tests/xfail/private_name.3.7.pyc b/tests/xfail/private_name.3.7.pyc new file mode 100644 index 0000000..2ca40c3 Binary files /dev/null and b/tests/xfail/private_name.3.7.pyc differ