diff options
author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2022-06-20 16:10:26 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-06-24 14:44:50 +0000 |
commit | 782ae86b89f58121d3ebb4fb02639a186be4f61c (patch) | |
tree | eb3030fc6e46d40ba1ec1b3b6182d1d1b1b01ce6 | |
parent | eb845eb7054666fdc60b8bb49e794f7c65bf99c6 (diff) |
snippet_translate double colon improvements
- Earlier, double colons were converted to dot operator only when the
statement had a QObject class or namespace. For cases with a normal C++
namespace like MyClass::x, it was still translated without modifications
to Python.
- This patch adds an extra statement at the end of snippet_translate(x)
to convert all the remaining scope resolution to dot operator
On top of the above changes, it also addresses a FIXME to handle C++
iterator declaration in Python
Task-number: PYSIDE-1972
Change-Id: I45d12954835aaa569d1a4ef15badb366eaff0fe7
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 1815221245c0c0e8e437b46975357a282b3ad9b3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | tools/snippets_translate/converter.py | 16 | ||||
-rw-r--r-- | tools/snippets_translate/tests/test_converter.py | 10 |
2 files changed, 23 insertions, 3 deletions
diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py index b437b9757..01df94505 100644 --- a/tools/snippets_translate/converter.py +++ b/tools/snippets_translate/converter.py @@ -144,7 +144,6 @@ def snippet_translate(x): x = handle_void_functions(x) # 'Q*::' -> 'Q*.' - # FIXME: This will break iterators, but it's a small price. if re.search(r"Q[\w]+::", x): x = x.replace("::", ".") @@ -310,6 +309,21 @@ def snippet_translate(x): if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+\(.*\)$", x.strip()): x = handle_functions(x) + # if it is a C++ iterator declaration, then ignore it due to dynamic typing in Python + # eg: std::vector<int> it; + # the case of iterator being used inside a for loop is already handed in handle_for(..) + # TODO: handle iterator initialization statement like it = container.begin(); + if re.search(r"(std::)?[\w]+<[\w]+>::(const_)?iterator", x): + x = "" + return x + + # By now all the typical special considerations of scope resolution operator should be handled + # 'Namespace*::' -> 'Namespace*.' + # TODO: In the case where a C++ class function is defined outside the class, this would be wrong + # but we do not have such a code snippet yet + if re.search(r"[\w]+::", x): + x = x.replace("::", ".") + # General return for no special cases return dstrip(x) diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py index 5656ff5e8..6f4048bc6 100644 --- a/tools/snippets_translate/tests/test_converter.py +++ b/tools/snippets_translate/tests/test_converter.py @@ -131,8 +131,12 @@ def test_cast(): def test_double_colon(): assert st("Qt::Align") == "Qt.Align" assert st('QSound::play("mysounds/bells.wav");') == 'QSound.play("mysounds/bells.wav")' - # FIXME - assert st("Widget::method") == "Widget::method" + assert st("Widget::method") == "Widget.method" + + # multiline statement connect statement + # eg: connect(reply, &QNetworkReply::errorOccurred, + # this, &MyClass::slotError); + assert st("this, &MyClass::slotError);") == "self, MyClass.slotError)" def test_cout_endl(): @@ -411,6 +415,8 @@ def test_special_cases(): assert st("public:") == "# public" assert st("private:") == "# private" + #iterator declaration + assert st("std::vector<int>::iterator i;") == "" # TODO: Handle the existing ones with Python equivalents # assert st("std::...") |