174 lines
5.7 KiB
Diff
174 lines
5.7 KiB
Diff
---
|
|
dnf/i18n.py | 21 ++++++++-------------
|
|
tests/test_i18n.py | 13 -------------
|
|
2 files changed, 8 insertions(+), 26 deletions(-)
|
|
|
|
diff --git a/dnf/i18n.py b/dnf/i18n.py
|
|
index 318a877..e6d3b4e 100644
|
|
--- a/dnf/i18n.py
|
|
+++ b/dnf/i18n.py
|
|
@@ -20,13 +20,12 @@
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
from dnf.pycomp import (PY3, is_py3bytes, unicode, setlocale, gettext,
|
|
- gettext_setup)
|
|
+ gettext_setup, raw_input)
|
|
import locale
|
|
import os
|
|
import signal
|
|
import sys
|
|
import unicodedata
|
|
-import readline # workaround of raw_input() bug (RhBug:1258364)
|
|
|
|
"""
|
|
Centralize i18n stuff here. Must be unittested.
|
|
@@ -98,20 +97,16 @@ def setup_stdout():
|
|
return False
|
|
return True
|
|
|
|
+
|
|
def ucd_input(ucstring):
|
|
- """ Take input from user.
|
|
+ """ It uses print instead of passing the prompt to raw_input.
|
|
|
|
- What the raw_input() built-in does, but encode the prompt first
|
|
- (raw_input() won't check sys.stdout.encoding as e.g. print does, see
|
|
- test_i18n.TestInput.test_assumption()).
|
|
+ raw_input doesn't encode the passed string and the output
|
|
+ goes into stderr
|
|
"""
|
|
- if not isinstance(ucstring, unicode):
|
|
- raise TypeError("input() accepts Unicode strings")
|
|
- if PY3:
|
|
- return input(ucstring)
|
|
- enc = sys.stdout.encoding if sys.stdout.encoding else 'utf8'
|
|
- s = ucstring.encode(enc, 'strict')
|
|
- return raw_input(s)
|
|
+ print(ucstring, end='')
|
|
+ return raw_input()
|
|
+
|
|
|
|
def ucd(obj):
|
|
""" Like the builtin unicode() but tries to use a reasonable encoding. """
|
|
diff --git a/tests/test_i18n.py b/tests/test_i18n.py
|
|
index e11dfe8..2160ec7 100644
|
|
--- a/tests/test_i18n.py
|
|
+++ b/tests/test_i18n.py
|
|
@@ -78,19 +78,6 @@ class TestInput(TestCase):
|
|
# way, for instance when nosetests is run without the -s switch).
|
|
self.assertRaises(UnicodeEncodeError, raw_input, UC_TEXT)
|
|
|
|
- @unittest.skipIf(PY3, "in python3 there's no conversion in dnf.i18n.input")
|
|
- @mock.patch('sys.stdout')
|
|
- @mock.patch('__builtin__.raw_input', lambda x: x)
|
|
- def test_input(self, stdout):
|
|
- stdout.encoding = None
|
|
- s = dnf.i18n.ucd_input(UC_TEXT)
|
|
- self.assertEqual(s, UC_TEXT.encode('utf8'))
|
|
-
|
|
- stdout.encoding = 'iso-8859-2'
|
|
- s = dnf.i18n.ucd_input(UC_TEXT)
|
|
- self.assertEqual(s, UC_TEXT.encode('iso-8859-2'))
|
|
-
|
|
- self.assertRaises(TypeError, dnf.i18n.ucd_input, b"string")
|
|
|
|
class TestConversion(TestCase):
|
|
@mock.patch('dnf.i18n._guess_encoding', return_value='utf-8')
|
|
--
|
|
2.4.3
|
|
|
|
---
|
|
dnf/i18n.py | 29 +++++++++++++++--------------
|
|
1 file changed, 15 insertions(+), 14 deletions(-)
|
|
|
|
diff --git a/dnf/i18n.py b/dnf/i18n.py
|
|
index e6d3b4e..97ef800 100644
|
|
--- a/dnf/i18n.py
|
|
+++ b/dnf/i18n.py
|
|
@@ -19,8 +19,9 @@
|
|
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
-from dnf.pycomp import (PY3, is_py3bytes, unicode, setlocale, gettext,
|
|
- gettext_setup, raw_input)
|
|
+from dnf.pycomp import unicode
|
|
+
|
|
+import dnf
|
|
import locale
|
|
import os
|
|
import signal
|
|
@@ -38,7 +39,7 @@ class UnicodeStream(object):
|
|
|
|
def write(self, s):
|
|
if not isinstance(s, str):
|
|
- s = (s.decode(self.encoding, 'replace') if PY3 else
|
|
+ s = (s.decode(self.encoding, 'replace') if dnf.pycomp.PY3 else
|
|
s.encode(self.encoding, 'replace'))
|
|
self.stream.write(s)
|
|
|
|
@@ -69,14 +70,14 @@ def _guess_encoding():
|
|
|
|
def setup_locale():
|
|
try:
|
|
- setlocale(locale.LC_ALL, '')
|
|
+ dnf.pycomp.setlocale(locale.LC_ALL, '')
|
|
# set time to C so that we output sane things in the logs (#433091)
|
|
- setlocale(locale.LC_TIME, 'C')
|
|
+ dnf.pycomp.setlocale(locale.LC_TIME, 'C')
|
|
except locale.Error as e:
|
|
# default to C locale if we get a failure.
|
|
print('Failed to set locale, defaulting to C', file=sys.stderr)
|
|
os.environ['LC_ALL'] = 'C'
|
|
- setlocale(locale.LC_ALL, 'C')
|
|
+ dnf.pycomp.setlocale(locale.LC_ALL, 'C')
|
|
|
|
def setup_stdout():
|
|
""" Check that stdout is of suitable encoding and handle the situation if
|
|
@@ -105,29 +106,29 @@ def ucd_input(ucstring):
|
|
goes into stderr
|
|
"""
|
|
print(ucstring, end='')
|
|
- return raw_input()
|
|
+ return dnf.pycomp.raw_input()
|
|
|
|
|
|
def ucd(obj):
|
|
""" Like the builtin unicode() but tries to use a reasonable encoding. """
|
|
- if PY3:
|
|
- if is_py3bytes(obj):
|
|
+ if dnf.pycomp.PY3:
|
|
+ if dnf.pycomp.is_py3bytes(obj):
|
|
return str(obj, _guess_encoding(), errors='ignore')
|
|
elif isinstance(obj, str):
|
|
return obj
|
|
return str(obj)
|
|
else:
|
|
- if isinstance(obj, unicode):
|
|
+ if isinstance(obj, dnf.pycomp.unicode):
|
|
return obj
|
|
if hasattr(obj, '__unicode__'):
|
|
# see the doc for the unicode() built-in. The logic here is: if obj
|
|
# implements __unicode__, let it take a crack at it, but handle the
|
|
# situation if it fails:
|
|
try:
|
|
- return unicode(obj)
|
|
+ return dnf.pycomp.unicode(obj)
|
|
except UnicodeError:
|
|
pass
|
|
- return unicode(str(obj), _guess_encoding(), errors='ignore')
|
|
+ return dnf.pycomp.unicode(str(obj), _guess_encoding(), errors='ignore')
|
|
|
|
|
|
# functions for formating output according to terminal width,
|
|
@@ -277,6 +278,6 @@ def textwrap_fill(text, width=70, initial_indent='', subsequent_indent=''):
|
|
return '\n'.join(ret)
|
|
|
|
# setup translation
|
|
-t = gettext.translation('dnf', fallback=True)
|
|
-_, P_ = gettext_setup(t)
|
|
+t = dnf.pycomp.gettext.translation('dnf', fallback=True)
|
|
+_, P_ = dnf.pycomp.gettext_setup(t)
|
|
|
|
--
|
|
2.4.3
|