Skip to content
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

'str' to 'wxString' converstation is broken, when emoji is inside string #2446

Closed
timrid opened this issue Aug 16, 2023 · 3 comments · Fixed by #2488
Closed

'str' to 'wxString' converstation is broken, when emoji is inside string #2446

timrid opened this issue Aug 16, 2023 · 3 comments · Fixed by #2488
Labels

Comments

@timrid
Copy link
Contributor

timrid commented Aug 16, 2023

Operating system: Windows 10
wxPython version & source: wxPython 4.2.1 from pypi
Python version & source: Python 3.11

Description of the problem:
Whem some special emojis like 🔄 are inside a python string, that is converted to an wxString (internally in wxPython), the last character of the string is lost. It is also not shown in the gui.

The changes in 29520a1 creates the problem. I compiled it with and without the changes and only with the changes the error occures. I guess that PyUnicode_GET_LENGTH is not an 1:1 replacement for PyUnicode_GET_SIZE.

I created the following small example to illustrate the problem.

import wx

app = wx.App(False)
txt = wx.Button()

def test_string(input):
    txt.SetLabel(input)
    output = txt.GetLabel()
    print(f"{input=}, {output=}")

test_string("asdf")  # Output is: "asdf" -> ok
test_string("⟳ asdf")  # Output is: "⟳ asdf" -> ok
test_string("🔄 asdf")  # Output is: "🔄 asd" -> error: last char is missing
test_string("🔄🔄 asdf")  # Output is: "🔄🔄 as" -> error: last two chars are missing
test_string("🔄")  # Output is: "\ud83d" -> error
@timrid

This comment was marked as outdated.

@swt2c
Copy link
Collaborator

swt2c commented Aug 16, 2023

Thanks @timrid for the bug report and analysis. I had feared that PyUnicode_GET_LENGTH was not an 1:1 replacement for PyUnicode_GET_SIZE. Unfortunately, the documentation/examples for the Python C APIs is not very extensive.

I'll try to take a look.

@timrid
Copy link
Contributor Author

timrid commented Nov 26, 2023

I also looked at the Python C API. The docs are not so useful. But when i looked in the cpython code i saw this comment for PyUnicode_AsWideChar.

/* Convert a Unicode object to a wide character string.

   - If w is NULL: return the number of wide characters (including the null
     character) required to convert the unicode object. Ignore size argument.

   - Otherwise: return the number of wide characters (excluding the null
     character) written into w. Write at most size wide characters (including
     the null character). */
Py_ssize_t
PyUnicode_AsWideChar(PyObject *unicode,
                     wchar_t *w,
                     Py_ssize_t size)
{
    ...

So i guess that

size_t len = PyUnicode_GET_SIZE(uni);

can be replaced with

size_t len = PyUnicode_AsWideChar(uni, NULL, 0);

seanbudd added a commit to nvaccess/nvda that referenced this issue Mar 5, 2024
Fixes #15714
See also nvaccess/nvda-misc-deps#32

Summary of the issue:
wxPython introduced a bug in 4.2.1: wxWidgets/Phoenix#2446
This caused #15714

wxPython is yet to release the commit which fixes this issue.
However, an alpha build wheel can be incorporate

The build used is from:
- https://alldunn.visualstudio.com/wxPython-CI/_build/results?buildId=1246&view=results
- wxWidgets/Phoenix@0205c7c

Description of user facing changes
Emojis and other unicode symbols are displayed properly in NVDA

Description of development approach
Update wxPython to an alpha snapshot
Adriani90 pushed a commit to Adriani90/nvda that referenced this issue Mar 13, 2024
Fixes nvaccess#15714
See also nvaccess/nvda-misc-deps#32

Summary of the issue:
wxPython introduced a bug in 4.2.1: wxWidgets/Phoenix#2446
This caused nvaccess#15714

wxPython is yet to release the commit which fixes this issue.
However, an alpha build wheel can be incorporate

The build used is from:
- https://alldunn.visualstudio.com/wxPython-CI/_build/results?buildId=1246&view=results
- wxWidgets/Phoenix@0205c7c

Description of user facing changes
Emojis and other unicode symbols are displayed properly in NVDA

Description of development approach
Update wxPython to an alpha snapshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants