Fix value for str/bytes field when save as Python

Protobuf string type should be treated as a Python unicode string usable
in both Python 2.x and Python 3.x. Since we are now using unicode strings,
force encoding as utf-8.

Protobuf bytes type should be treated as a Python byte string. Use hex
values in byte literal even for printable characters, for a better UX.

escapeString() is no longer being used, but has been retained in the
code.
This commit is contained in:
Srivats P 2022-12-28 19:15:41 +05:30
parent c07d9e8691
commit e2369c02bc
2 changed files with 27 additions and 7 deletions

View File

@ -63,6 +63,8 @@ bool PythonFileFormat::save(const OstProto::StreamConfigList streams,
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
goto _open_fail;
out.setCodec("UTF-8");
// import standard modules
emit status("Writing imports ...");
emit target(0);
@ -450,9 +452,16 @@ void PythonFileFormat::writeFieldAssignment(
std::string val = fieldDesc->is_repeated() ?
refl->GetRepeatedStringReference(msg, fieldDesc, index, &val) :
refl->GetStringReference(msg, fieldDesc, &val);
QString escVal = escapeString(QString::fromStdString(val));
if (val != fieldDesc->default_value_string())
out << fieldName << " = '" << escVal << "'\n";
if (val == fieldDesc->default_value_string())
break;
if (fieldDesc->type() == FieldDescriptor::TYPE_BYTES) {
QString strVal = byteString(QByteArray(val.c_str(),
val.size()));
out << fieldName << " = b'" << strVal << "'\n";
} else {
QString strVal = QString::fromStdString(val);
out << fieldName << " = u'" << strVal << "'\n";
}
break;
}
case FieldDescriptor::CPPTYPE_ENUM:
@ -533,16 +542,16 @@ QString PythonFileFormat::singularize(QString plural)
return singular;
}
QString PythonFileFormat::escapeString(QString str)
QString PythonFileFormat::escapeString(QByteArray str)
{
QString escStr = "";
for (int i=0; i < str.length(); i++) {
uchar c = str[i].cell();
uchar c = uchar(str.at(i));
if ((c < 128) && isprint(c)) {
if (c == '\'')
escStr.append("\\'");
else
escStr.append(str[i]);
escStr.append(QChar(c));
}
else
escStr.append(QString("\\x%1").arg(int(c), 2, 16, QChar('0')));
@ -550,6 +559,16 @@ QString PythonFileFormat::escapeString(QString str)
return escStr;
}
QString PythonFileFormat::byteString(QByteArray str)
{
QString byteStr = "";
for (int i=0; i < str.length(); i++) {
uchar c = uchar(str.at(i));
byteStr.append(QString("\\x%1").arg(int(c), 2, 16, QChar('0')));
}
return byteStr;
}
bool PythonFileFormat::useDecimalBase(QString fieldName)
{
// Heuristic - use Hex base for all except for the following

View File

@ -49,7 +49,8 @@ private:
const google::protobuf::FieldDescriptor *fieldDesc,
int index = -1);
QString singularize(QString plural);
QString escapeString(QString str);
QString escapeString(QByteArray str);
QString byteString(QByteArray str);
bool useDecimalBase(QString fieldName);
};