Merge lp://staging/~mvo/ubuntu-release-upgrader/use-str.format into lp://staging/ubuntu-release-upgrader

Proposed by Michael Vogt
Status: Rejected
Rejected by: Michael Vogt
Proposed branch: lp://staging/~mvo/ubuntu-release-upgrader/use-str.format
Merge into: lp://staging/ubuntu-release-upgrader
Diff against target: 888 lines (+175/-142)
14 files modified
DistUpgrade/DistUpgradeAptCdrom.py (+1/-1)
DistUpgrade/DistUpgradeCache.py (+10/-10)
DistUpgrade/DistUpgradeController.py (+39/-38)
DistUpgrade/DistUpgradeFetcherCore.py (+5/-5)
DistUpgrade/DistUpgradeFetcherKDE.py (+4/-3)
DistUpgrade/DistUpgradeGettext.py (+7/-2)
DistUpgrade/DistUpgradeView.py (+27/-24)
DistUpgrade/DistUpgradeViewGtk3.py (+25/-21)
DistUpgrade/DistUpgradeViewKDE.py (+23/-15)
DistUpgrade/DistUpgradeViewText.py (+10/-5)
DistUpgrade/GtkProgress.py (+8/-8)
check-new-release-gtk (+8/-7)
do-partial-upgrade (+3/-3)
pre-build.sh (+5/-0)
To merge this branch: bzr merge lp://staging/~mvo/ubuntu-release-upgrader/use-str.format
Reviewer Review Type Date Requested Status
Ubuntu Core Development Team Pending
Review via email: mp+217004@code.staging.launchpad.net

Description of the change

Use str.format() with gettext (eventually everywhere) to avoid crashes if e.g. incorrect translations are specified. The worst case with str.format() is that the information is not displayed correctly, but it won't crash like e.g. LP: #1311396

To post a comment you must log in.
Revision history for this message
Michael Terry (mterry) wrote :

Maybe str.format is better for other reasons, but you can certainly still get tracebacks from it:

$ python3 -c "'hello {0'.format('world')"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: expected '}' before end of string

Revision history for this message
Michael Vogt (mvo) wrote :

Hi Michael, thanks for your comment. Indeed, I should have been more clear.

The issue that is fixed is:

$ python -c 'print("foo" % "bar")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: not all arguments converted during string formatting

$ python -c 'print("foo".format("bar"))'
foo

But yes, if a translation has a incorrect number of {} it will still crash, so maybe its not worth
doing all the work and instead use the "Foo %(bar)s" % { "bar": "some-str" } approach where its
also ok to leave out a argument. I.e.
$ python -c 'print("foo" % {"bar": "bar"})'
foo

Revision history for this message
Barry Warsaw (barry) wrote :

It might be a lot of work to port, but I will suggest using my flufl.i18n package, which has a lot of nice convenience APIs and safeguards for doing i18n interpolation.

http://pythonhosted.org/flufl.i18n/docs/using.html

And of course, it's packaged up :).

Revision history for this message
Barry Warsaw (barry) wrote :

Also note that with modern Pythons

'{0}'.format('foo')

is equivalent to

'{}'.format('foo')

Unmerged revisions

2779. By Michael Vogt

replace all %s style format with str.format() in gettext to be more robust against crashes from incorrectly formated translations

2778. By Michael Vogt

use str.format() for all ngettext() to avoid crashes on incorrect number of parameters (LP: #1311396

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
The diff is not available at this time. You can reload the page or download it.

Subscribers

People subscribed via source and target branches