Merge lp:~gz/bzr/2.5_help_translation_unicode_error_930919 into lp:bzr/2.5

Proposed by Martin Packman
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: no longer in the source branch.
Merged at revision: 6490
Proposed branch: lp:~gz/bzr/2.5_help_translation_unicode_error_930919
Merge into: lp:bzr/2.5
Diff against target: 84 lines (+37/-8)
2 files modified
bzrlib/help.py (+3/-2)
bzrlib/tests/blackbox/test_help.py (+34/-6)
To merge this branch: bzr merge lp:~gz/bzr/2.5_help_translation_unicode_error_930919
Reviewer Review Type Date Requested Status
IWATA Hidetaka Pending
bzr-core Pending
Review via email: mp+94756@code.launchpad.net

Commit message

Default help output to ui wrapped stream rather than raw stdout for better encoding handling

Description of the change

Make the display of unicode help via the help command use a default stream that knows how to deal with unicode, rather than stdout directly which may not. The --help switch is not affected as it uses a completely different code path with a different set of issues.

There are still some niggles unaddressed. Callers could still pass in a dumb stream that it's not safe to write arbitrary unicode to. Also, as gettext is a little more forgiving than system locales, it's possible to get things translated that then bzr has no idea how to encode. In this case, the user does get a nice clear error message and non-ascii characters replaced with question marks, so it should be clear enough that they need to install the relevant locale. Possibly the fallback could be smarter, but moving that choice into bzrlib.ui seems like an improvement regardless.

To post a comment you must log in.
Revision history for this message
IWATA Hidetaka (hid-iwata) wrote :

First, My report is a bit wrong.
I said that `bzr command-name --help` works, but it also can fail depending on the target command.
e.g. If LANG=ja, `bzr commit --help` works, but `bzr diff --help` does not work.

I’ve tested this patch under the environment of Windows + Python 2.6.6, and confirmed this fixes
problem about `bzr help command-name`.

Thank you.

Revision history for this message
Alexander Belchenko (bialix) wrote :

IWATA Hidetaka пишет:
> First, My report is a bit wrong.
> I said that `bzr command-name --help` works, but it also can fail depending on the target command.
> e.g. If LANG=ja, `bzr commit --help` works, but `bzr diff --help` does not work.

I think I understand why `bzr diff --help` does not work.

We have strict encoding for diff output, and most likely --help uses
the same output stream, so we get problem for help and unicode.

That should be fixed globally by replacing the stream for *all*
commands if there is --help or --usage options given. (Don't forget
about --usage ;-)

Maybe it's worth to pre-process command-line options before initiate
the command object, or maybe in setup_outf_stream (or how it named
today?) method of the command object.

--
All the dude wanted was his rug back

Revision history for this message
Martin Packman (gz) wrote :

I've filed bug 943260 for the issue with writing to outf in commands, as it's a wider problem than just help switches and wants a different fix.

Lots of commands to output like that without being very careful about it, for instance:

C:\bzr\bzr\2.5>%PY27% bzr init B:\testbranch
standalone treeを作成しました。(フォーマット:2a)

C:\bzr\bzr\2.5>python -c "file(u'B:/testbranch/\xe9','w').close()"

C:\bzr\bzr\2.5>%PY27% bzr unknowns -d B:\testbranch
bzr: ERROR: exceptions.UnicodeEncodeError: 'cp932' codec can't encode character u'\xe9' in position 1: illegal multibyte sequence

Revision history for this message
Martin Packman (gz) wrote :

sent to pqm by email

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/help.py'
2--- bzrlib/help.py 2011-12-18 15:28:38 +0000
3+++ bzrlib/help.py 2012-02-27 12:26:18 +0000
4@@ -31,6 +31,7 @@
5 help_topics,
6 osutils,
7 plugin,
8+ ui,
9 utextwrap,
10 )
11
12@@ -38,7 +39,7 @@
13 def help(topic=None, outfile=None):
14 """Write the help for the specific topic to outfile"""
15 if outfile is None:
16- outfile = sys.stdout
17+ outfile = ui.ui_factory.make_output_stream()
18
19 indices = HelpIndices()
20
21@@ -63,7 +64,7 @@
22 def help_commands(outfile=None):
23 """List all commands"""
24 if outfile is None:
25- outfile = sys.stdout
26+ outfile = ui.ui_factory.make_output_stream()
27 outfile.write(_help_commands_to_text('commands'))
28
29
30
31=== modified file 'bzrlib/tests/blackbox/test_help.py'
32--- bzrlib/tests/blackbox/test_help.py 2011-01-12 00:58:05 +0000
33+++ bzrlib/tests/blackbox/test_help.py 2012-02-27 12:26:18 +0000
34@@ -19,12 +19,16 @@
35 """
36
37
38-import bzrlib
39-from bzrlib import config
40-from bzrlib.tests import TestCaseWithTransport
41-
42-
43-class TestHelp(TestCaseWithTransport):
44+from bzrlib import (
45+ config,
46+ i18n,
47+ tests,
48+ )
49+
50+from bzrlib.tests.test_i18n import ZzzTranslations
51+
52+
53+class TestHelp(tests.TestCaseWithTransport):
54
55 def test_help_basic(self):
56 for cmd in ['--help', 'help', '-h', '-?']:
57@@ -175,3 +179,27 @@
58
59 self.assertEqual("'bzr c' is an alias for 'bzr cat'.\n",
60 self.run_bzr('help c')[0])
61+
62+
63+class TestTranslatedHelp(tests.TestCaseWithTransport):
64+ """Tests for display of translated help topics"""
65+
66+ def setUp(self):
67+ super(TestTranslatedHelp, self).setUp()
68+ self.overrideAttr(i18n, '_translations', ZzzTranslations())
69+
70+ def test_help_command_utf8(self):
71+ out, err = self.run_bzr(["help", "push"], encoding="utf-8")
72+ self.assertContainsRe(out, "zz\xc3\xa5{{:See also:")
73+
74+ def test_help_switch_utf8(self):
75+ out, err = self.run_bzr(["push", "--help"], encoding="utf-8")
76+ self.assertContainsRe(out, "zz\xc3\xa5{{:See also:")
77+
78+ def test_help_command_ascii(self):
79+ out, err = self.run_bzr(["help", "push"], encoding="ascii")
80+ self.assertContainsRe(out, "zz\\?{{:See also:")
81+
82+ def test_help_switch_ascii(self):
83+ out, err = self.run_bzr(["push", "--help"], encoding="ascii")
84+ self.assertContainsRe(out, "zz\\?{{:See also:")

Subscribers

People subscribed via source and target branches