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

(curses) addstr() takes str in Python 3 #50994

Closed
Trundle mannequin opened this issue Aug 20, 2009 · 11 comments
Closed

(curses) addstr() takes str in Python 3 #50994

Trundle mannequin opened this issue Aug 20, 2009 · 11 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Trundle
Copy link
Mannequin

Trundle mannequin commented Aug 20, 2009

BPO 6745
Nosy @vstinner, @Trundle, @ambv, @akheron
Files
  • umlaut2x.py: Umlauts working in Python 2.x
  • umlaut3x.py: Umlauts not working in Python 3.x
  • curses_charset.patch
  • getkey_sample.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ambv'
    closed_at = <Date 2012-06-04.23:37:38.789>
    created_at = <Date 2009-08-20.21:54:52.568>
    labels = ['type-bug', 'library']
    title = '(curses) addstr() takes str in Python 3'
    updated_at = <Date 2012-06-04.23:37:38.769>
    user = 'https://github.com/Trundle'

    bugs.python.org fields:

    activity = <Date 2012-06-04.23:37:38.769>
    actor = 'vstinner'
    assignee = 'lukasz.langa'
    closed = True
    closed_date = <Date 2012-06-04.23:37:38.789>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2009-08-20.21:54:52.568>
    creator = 'Trundle'
    dependencies = []
    files = ['14750', '14751', '14792', '19623']
    hgrepos = []
    issue_num = 6745
    keywords = ['patch']
    message_count = 11.0
    messages = ['91786', '92019', '92020', '92021', '92023', '92024', '92025', '121318', '121346', '140380', '162307']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'Trundle', 'lukasz.langa', 'petri.lehtinen']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue6745'
    versions = ['Python 3.2']

    @Trundle
    Copy link
    Mannequin Author

    Trundle mannequin commented Aug 20, 2009

    In Python 3, curses requires a str for addstr() where I think it should
    take bytes instead. Otherwise it is impossible to output anything other
    than ASCII (which is even more or less stated on top of curses'
    documentation).

    See the attached script "umlaut2x.py" for Python 2.6: Outputting
    umlauts works fine, both in single-byte and multi-byte environments.

    The attached script "umlaut3x.py" is the same script translated to
    Python 3. Note that the output here always seems to be utf-8, which is
    plain wrong.

    A quick test where I changed addstr() to take bytes instead of str
    confirmed that outputting other characters than ASCII would work then
    in Python 3, too. There are perhaps more places where the types are
    wrong. If someone confirms this issue and it is desired, I could
    provide a patch.

    @Trundle Trundle mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 20, 2009
    @vstinner
    Copy link
    Member

    First, make sure that your Python3 build uses libncursesw and not
    libncurses, because libncursesw supports unicode, whereas libncurses
    doesn't... On UNIX, use the following command to check this:

    ldd $(./python -c "import _curses; print(curses.__file_)")|grep curses

    Note that the output here always seems to be utf-8,
    which is plain wrong.

    Yes, addstr() always uses utf8 to convert unicode to bytes. It's wrong
    if the terminal uses a different charset. But I'm not sure that using
    bytes is a better idea: since you would like to print characters,
    unicode is the right type.

    An idea would be to use a configurable charset. Eg. add a 'charset'
    attribute to a window (or to the module).

    @vstinner
    Copy link
    Member

    See also issue bpo-4787

    @Trundle
    Copy link
    Mannequin Author

    Trundle mannequin commented Aug 27, 2009

    Yes, it uses a version of ncurses which supports wide characters, I
    checked that.

    I agree that using bytes instead may not be the preferred solution in
    Python 3. The point is, currently, it is broken if the user does not
    use an utf-8 environment.

    @vstinner
    Copy link
    Member

    I don't really understand because your example, umlaut3x.py, works
    correctly on my computer (py3k, ubunty jaunty).

    The point is, currently, it is broken if the user
    does not use an utf-8 environment.

    So the problem is that the charset is hardcoded to utf8. You would like
    to be able to change that. Or better, than Python guess your terminal
    charset. Right?

    @Trundle
    Copy link
    Mannequin Author

    Trundle mannequin commented Aug 27, 2009

    Of course it works for you. As you stated in issue bpo-4787, your locale
    is 'fr_FR.UTF-8'.

    And I don't want Python to guess my terminal's encoding. I want Python
    to respect my locale. Which is 'de_DE@euro', and not utf-8.

    @vstinner
    Copy link
    Member

    Here is a first patch to add a method setcharset() to the window class.

    Using my patch, you can fix your example by adding the line:

       screen.setcharset(<your charset>)

    before addstr().

    It's an initial hack to fix the issue. Next steps are:

    • use something better than utf8 as the default charset, maybe
      locale.getpreferredencoding()
    • copy the charset on new window creation?

    @ambv
    Copy link
    Contributor

    ambv commented Nov 16, 2010

    We'll try to solve this for 3.2.

    @ambv ambv self-assigned this Nov 16, 2010
    @Trundle
    Copy link
    Mannequin Author

    Trundle mannequin commented Nov 17, 2010

    Note that getkey() is broken, too. I attached a simple script to demonstrate that. If you run it and enter some non-ascii input, you can see that getkey() returns an utf-8 encoded str (in my utf-8 environment at least, I haven't check if it's always utf-8 or if it depends on the locale).

    @vstinner
    Copy link
    Member

    I created issue bpo-12567 to fix the Unicode support of the curses module in Python 3.

    @vstinner
    Copy link
    Member

    vstinner commented Jun 4, 2012

    The issue bpo-12567 fixed this one:

    • umlaut3x.py now works in Python 3.3 with an encoding different than UTF-8: Python automatically detects (and uses) the locale encoding
    • getkey_sample.py can be patched to handle Unicode correctly using get_wch() instead of getkey()

    @vstinner vstinner closed this as completed Jun 4, 2012
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants