-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix string conversion issues with emoji characters #120
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,10 @@ namespace pybind11::detail { | |
PyObject* strPtr = | ||
PyUnicode_Check(objPtr) ? PyUnicode_AsUTF8String(objPtr) : objPtr; | ||
|
||
if (!strPtr) { | ||
return false; | ||
} | ||
|
||
// Extract the character data from the python string | ||
value = QString::fromUtf8(PyBytes_AsString(strPtr)); | ||
|
||
|
@@ -68,8 +72,7 @@ namespace pybind11::detail { | |
handle /* parent */) | ||
{ | ||
static_assert(sizeof(QChar) == 2); | ||
return PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, src.constData(), | ||
src.length()); | ||
return PyUnicode_FromString(src.toStdString().c_str()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to change this? AFAIK, Unicode string in Python can be stored as UTF-16 so this adds an extra conversion from UTF-16 to UTF-8. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Emoji characters can't be directly converted back to QStrings in the previous function. The current workaround is to encode to UTF-16 with a surrogatepass and decode again. This allows us to directly use QStrings from C++ -> Python -> C++ without having reencode the strings again in Python. See #plugins-dev. |
||
} | ||
|
||
bool type_caster<QVariant>::load(handle src, bool) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is the fix for the crashes but can you tell what was in
objPtr
when this occurs?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a string object, but the string format returned when there are emojis fails to properly convert back into a QString.
This is described in more detail in #plugins-dev.