Bug 689448

Summary: RFE: Accept unicode in python bindings
Product: [Fedora] Fedora Reporter: Miroslav Suchý <msuchy>
Component: newtAssignee: Miroslav Lichvar <mlichvar>
Status: CLOSED UPSTREAM QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: low Docs Contact:
Priority: unspecified    
Version: rawhideCC: mlichvar
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2018-07-27 13:24:52 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Miroslav Suchý 2011-03-21 14:51:50 UTC
Current newt-python does not accept unicode for text parameters.

E.g. if you modify peanut.py from example:

t = TextboxReflowed(25, "Some text which needs to be wrapped at a good place.")

to 

# -*- coding: utf-8 -*-
t = TextboxReflowed(25, u"žžž")

you will get:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

It will work if you pass as parameter 
 "žžž"
i.e. object string encoded as utf-8 - but that is different from object unicode, see:
http://docs.python.org/howto/unicode.html#the-unicode-type
and:
>>> type(u"žžž")
<type 'unicode'>
>>> type("žžž")
<type 'str'>

And it will work if you pass as parameter:
 u"žžž".encode('utf-8')

So this can be work arounded for sure. But it will be nice if newt-python can accept true unicode type.

Comment 1 Miroslav Suchý 2011-03-21 20:01:38 UTC
I spent some time investigating this. And the cause is best described here:
http://docs.python.org/c-api/arg.html

s (string or Unicode) [const char *]...Unicode objects are converted to C strings using the *default encoding*...
(emphasis is mine)

And problem with default encoding is best described here:

http://fedoraproject.org/wiki/Features/PythonEncodingUsesSystemLocale
And in mailing thread:
http://thread.gmane.org/gmane.comp.python.devel/109914

So one solution is to put into snackmodule.c:
PyUnicode_SetDefaultEncoding("utf-8");
but that is probably wrong approach.

Second option is to put at the begging each function of snack.py this:
if isinstance(text, unicode):
     text = text.encode('utf-8')
for every text parameter.

Comment 2 Miroslav Lichvar 2018-07-27 13:24:52 UTC
I've moved this RFE to the upstream tracker: https://pagure.io/newt/issue/7