Skip to content

Commit b0d62bb

Browse files
FriedemannKleintQt Cherry-pick Bot
authored andcommitted
lupdate/Python: Fix extra comments being added to the wrong message
parseTranslate() can read past the message and capture extraComments intended for the next message. Use only extraComments for the current message, checking on line numbers. Since the same issue exists for message ids, fix it using the same approach. Fixes: PYSIDE-2863 Pick-to: 6.5 Change-Id: I2eb3fc0edd557d32917a05392caab05e2da9b1e7 Reviewed-by: Joerg Bornemann <[email protected]> (cherry picked from commit 0eac225) Reviewed-by: Qt Cherry-pick Bot <[email protected]>
1 parent e9c0869 commit b0d62bb

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

src/linguist/lupdate/python.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "lupdate.h"
77

88
#include <QtCore/qhash.h>
9+
#include <QtCore/qlist.h>
910
#include <QtCore/qstring.h>
1011
#include <QtCore/qtextstream.h>
1112
#include <QtCore/qstack.h>
@@ -53,8 +54,15 @@ static int yyParenDepth;
5354
static int yyLineNo;
5455
static int yyCurLineNo;
5556

56-
static QByteArray extraComment;
57-
static QByteArray id;
57+
struct ExtraComment
58+
{
59+
QByteArray extraComment;
60+
int lineNo;
61+
};
62+
63+
static QList<ExtraComment> extraComments;
64+
65+
static QList<ExtraComment> ids;
5866

5967
QHash<QByteArray, Token> tokens = {
6068
{"None", Tok_None},
@@ -302,10 +310,10 @@ static Token getToken(StringType stringType = StringType::NoString)
302310
case '#':
303311
switch (getChar()) {
304312
case ':':
305-
extraComment = readLine().trimmed();
313+
extraComments.append({readLine().trimmed(), yyCurLineNo});
306314
break;
307315
case '=':
308-
id = readLine().trimmed();
316+
ids.append({readLine().trimmed(), yyCurLineNo});
309317
break;
310318
case EOF:
311319
return Tok_Eof;
@@ -583,16 +591,24 @@ static bool parseTranslate(QByteArray *text, QByteArray *context, QByteArray *co
583591
return false;
584592
}
585593

586-
static inline void setMessageParameters(TranslatorMessage *message)
594+
static void setMessageParameters(TranslatorMessage *message,
595+
int lineNo)
587596
{
588-
if (!extraComment.isEmpty()) {
589-
message->setExtraComment(QString::fromUtf8(extraComment));
590-
extraComment.clear();
591-
}
592-
if (!id.isEmpty()) {
593-
message->setId(QString::fromUtf8(id));
594-
id.clear();
597+
// PYSIDE-2863: parseTranslate() can read past the message
598+
// and capture extraComments intended for the next message.
599+
// Use only extraComments for the current message.
600+
QByteArray extraComment;
601+
while (!extraComments.isEmpty() && extraComments.constFirst().lineNo <= lineNo) {
602+
if (!extraComment.isEmpty())
603+
extraComment += ' ';
604+
extraComment += extraComments.takeFirst().extraComment;
595605
}
606+
607+
if (!extraComment.isEmpty())
608+
message->setExtraComment(QString::fromUtf8(extraComment));
609+
610+
while (!ids.isEmpty() && ids.constFirst().lineNo <= lineNo)
611+
message->setId(QString::fromUtf8(ids.takeFirst().extraComment));
596612
}
597613

598614
static void parse(Translator &tor, ConversionData &cd,
@@ -635,9 +651,10 @@ static void parse(Translator &tor, ConversionData &cd,
635651
yyTok = getToken();
636652
break;
637653
case Tok_tr:
638-
case Tok_trUtf8:
654+
case Tok_trUtf8: {
639655
utf8 = true;
640656
yyTok = getToken();
657+
const int lineNo = yyCurLineNo;
641658
if (match(Tok_LeftParen) && matchString(&text)) {
642659
comment.clear();
643660
bool plural = false;
@@ -670,21 +687,23 @@ static void parse(Translator &tor, ConversionData &cd,
670687
QString::fromUtf8(comment),
671688
{}, yyFileName, yyLineNo,
672689
{}, TranslatorMessage::Unfinished, plural);
673-
setMessageParameters(&message);
690+
setMessageParameters(&message, lineNo);
674691
tor.extend(message, cd);
675692
}
676693
}
694+
}
677695
break;
678696
case Tok_translate: {
679697
bool plural{};
698+
const int lineNo = yyCurLineNo;
680699
if (parseTranslate(&text, &context, &comment, &utf8, &plural)
681700
&& !text.isEmpty()) {
682701
TranslatorMessage message(QString::fromUtf8(context),
683702
QString::fromUtf8(text),
684703
QString::fromUtf8(comment),
685704
{}, yyFileName, yyLineNo,
686705
{}, TranslatorMessage::Unfinished, plural);
687-
setMessageParameters(&message);
706+
setMessageParameters(&message, lineNo);
688707
tor.extend(message, cd);
689708
}
690709
}

tests/auto/linguist/lupdate/testdata/good/parsepython/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ def window_method(self): # PYSIDE-2379, Don't put this into NestedClass
7777
msg = self.tr(r"A raw strin\g""continued\\")
7878
msg = self.tr(r"A raw string with escaped quote\"bla")
7979

80+
def test_pyside2863(self):
81+
"""PYSIDE-2863, Check whether id and extra comments are correctly associated."""
82+
#= id_1
83+
msg = self.tr("msg1")
84+
#= id_2
85+
msg = self.tr("msg2")
86+
#: Extra comment 3
87+
msg = self.tr("msg3")
88+
#: Extra comment 4
89+
msg = self.tr("msg4")
90+
8091

8192
if __name__ == '__main__':
8293
app = QApplication(sys.argv)

tests/auto/linguist/lupdate/testdata/good/parsepython/project.ts.result

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,31 @@
101101
<translation type="unfinished"></translation>
102102
</message>
103103
<message>
104-
<location filename="main.py" line="81"/>
104+
<location filename="main.py" line="80"/>
105105
<source>A raw string with escaped quote&quot;bla</source>
106106
<translation type="unfinished"></translation>
107107
</message>
108+
<message id="id_1">
109+
<location filename="main.py" line="85"/>
110+
<source>msg1</source>
111+
<translation type="unfinished"></translation>
112+
</message>
113+
<message id="id_2">
114+
<location filename="main.py" line="87"/>
115+
<source>msg2</source>
116+
<translation type="unfinished"></translation>
117+
</message>
118+
<message>
119+
<location filename="main.py" line="89"/>
120+
<source>msg3</source>
121+
<extracomment>Extra comment 3</extracomment>
122+
<translation type="unfinished"></translation>
123+
</message>
124+
<message>
125+
<location filename="main.py" line="92"/>
126+
<source>msg4</source>
127+
<extracomment>Extra comment 4</extracomment>
128+
<translation type="unfinished"></translation>
129+
</message>
108130
</context>
109131
</TS>

0 commit comments

Comments
 (0)