Merge lp:~vorlon/aptdaemon/lp.1034806 into lp:aptdaemon

Proposed by Steve Langasek
Status: Merged
Merged at revision: 859
Proposed branch: lp:~vorlon/aptdaemon/lp.1034806
Merge into: lp:aptdaemon
Diff against target: 158 lines (+92/-9)
3 files modified
aptdaemon/core.py (+20/-8)
aptdaemon/utils.py (+7/-1)
tests/regressions/test_lp768691.py (+65/-0)
To merge this branch: bzr merge lp:~vorlon/aptdaemon/lp.1034806
Reviewer Review Type Date Requested Status
Aptdaemon Developers Pending
Review via email: mp+119227@code.launchpad.net

Description of the change

Fixes for str/unicode confusion in aptdaemon. These don't matter much in
practice for python3, but I think python2 compatibility is still a goal for
aptdaemon upstream.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'aptdaemon/core.py'
--- aptdaemon/core.py 2012-06-06 13:53:40 +0000
+++ aptdaemon/core.py 2012-08-11 02:38:19 +0000
@@ -68,6 +68,12 @@
6868
69# Setup i18n69# Setup i18n
70_ = lambda msg: gettext.dgettext("aptdaemon", msg)70_ = lambda msg: gettext.dgettext("aptdaemon", msg)
71if sys.version >= '3':
72 _gettext_method = "gettext"
73 _ngettext_method = "ngettext"
74else:
75 _gettext_method = "ugettext"
76 _ngettext_method = "ungettext"
7177
72APTDAEMON_DBUS_INTERFACE = 'org.debian.apt'78APTDAEMON_DBUS_INTERFACE = 'org.debian.apt'
73APTDAEMON_DBUS_PATH = '/org/debian/apt'79APTDAEMON_DBUS_PATH = '/org/debian/apt'
@@ -1220,6 +1226,16 @@
1220 """Set the kwargs which will be send to the AptWorker."""1226 """Set the kwargs which will be send to the AptWorker."""
1221 self.kwargs = kwargs1227 self.kwargs = kwargs
12221228
1229 def _get_translations(self):
1230 """Get a usable translations object, no matter what."""
1231 if self._translation:
1232 return self._translation
1233 else:
1234 domain = "aptdaemon"
1235 return gettext.translation(domain, gettext.bindtextdomain(domain),
1236 gettext.bind_textdomain_codeset(domain),
1237 fallback=True)
1238
1223 def gettext(self, msg):1239 def gettext(self, msg):
1224 """Translate the given message to the language of the transaction.1240 """Translate the given message to the language of the transaction.
1225 Fallback to the system default.1241 Fallback to the system default.
@@ -1227,19 +1243,15 @@
1227 # Avoid showing the header of the mo file for an empty string1243 # Avoid showing the header of the mo file for an empty string
1228 if not msg:1244 if not msg:
1229 return ""1245 return ""
1230 if self._translation:1246 translation = self._get_translations()
1231 return self._translation.gettext(msg)1247 return getattr(translation, _gettext_method)(msg)
1232 else:
1233 return gettext.gettext(msg)
12341248
1235 def ngettext(self, singular, plural, count):1249 def ngettext(self, singular, plural, count):
1236 """Translate the given plural message to the language of the1250 """Translate the given plural message to the language of the
1237 transaction. Fallback to the system default.1251 transaction. Fallback to the system default.
1238 """1252 """
1239 if self._translation:1253 translation = self._get_translations()
1240 return self._translation.ngettext(singular, plural, count)1254 return getattr(translation, _ngettext_method)(singular, plural, count)
1241 else:
1242 return gettext.ngettext(singular, plural, count)
12431255
12441256
1245class TransactionQueue(GObject.GObject):1257class TransactionQueue(GObject.GObject):
12461258
=== modified file 'aptdaemon/utils.py'
--- aptdaemon/utils.py 2012-05-09 00:49:57 +0000
+++ aptdaemon/utils.py 2012-08-11 02:38:19 +0000
@@ -24,11 +24,17 @@
2424
25__all__ = ("deprecated", "IsoCodes")25__all__ = ("deprecated", "IsoCodes")
2626
27import sys
27import gettext28import gettext
28import functools29import functools
29import warnings30import warnings
30from xml.etree import ElementTree31from xml.etree import ElementTree
3132
33if sys.version >= '3':
34 _gettext_method = "gettext"
35else:
36 _gettext_method = "ugettext"
37
32def deprecated(func):38def deprecated(func):
33 """This is a decorator which can be used to mark functions39 """This is a decorator which can be used to mark functions
34 as deprecated. It will result in a warning being emitted40 as deprecated. It will result in a warning being emitted
@@ -76,7 +82,7 @@
76 return None82 return None
77 trans = gettext.translation(domain=self.norm, fallback=True,83 trans = gettext.translation(domain=self.norm, fallback=True,
78 languages=[locale])84 languages=[locale])
79 return trans.gettext(name)85 return getattr(trans,_gettext_method)(name)
8086
81 def get_name(self, value):87 def get_name(self, value):
82 try:88 try:
8389
=== added file 'tests/regressions/test_lp768691.py'
--- tests/regressions/test_lp768691.py 1970-01-01 00:00:00 +0000
+++ tests/regressions/test_lp768691.py 2012-08-11 02:38:19 +0000
@@ -0,0 +1,65 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""Tests if we can handle different encodings well."""
4
5import locale
6import logging
7import os
8import sys
9import unittest
10
11from aptdaemon import core, enums, test, errors, utils
12
13DEBUG=True
14
15if sys.version >= '3':
16 unicode = str
17
18class GettextTest(test.AptDaemonTestCase):
19
20 """Regression test for LP: #768691 and LP: #926340
21
22 The gettext.translation.gettext() method returns a string in Python 2.
23 If we try to perform a string format operation, Python wants to convert
24 string to unicode. If the daemon is running with a different
25 default encoding as the translated message this results in an error.
26 By defaulf aptdaemon runs as C if activated by D-Bus.
27 """
28
29 def setUp(self):
30 # Use the mo files from the build
31 local_mo_files = os.path.join(test.get_tests_dir(),
32 "../build/mo")
33 if not os.path.isdir(local_mo_files):
34 self.skipTest("Please run setup.py build before since local mo "
35 "files are required. Run python setup.py build_i18n")
36 core.gettext._default_localedir = local_mo_files
37
38 self.trans = core.Transaction(None, enums.ROLE_FIX_BROKEN_DEPENDS,
39 None, os.getpid(), os.getuid(),
40 sys.argv[0], "org.debian.apt.test",
41 connect=False)
42 self.codes = utils.IsoCodes("iso_639", tag="iso_639_1_code",
43 fallback_tag="iso_639_2T_code")
44
45 def test(self):
46 """Test if the installation of an unauthenticated packages fails
47 if simulate hasn't been called explicitly before.
48 """
49 self.trans._set_locale("de_DE.UTF-8")
50 ret = self.trans.gettext("CD/DVD '%s' is required")
51 self.assertTrue(isinstance(ret, unicode))
52 error = errors.TransactionFailed(enums.ERROR_NO_PACKAGE,
53 "CD/DVD '%s' is required", "lala")
54 self.trans.error = error
55
56 utils.gettext._default_localedir = "/usr/share/locale"
57 lang = self.codes.get_localised_name("en", "ru.UTF-8")
58 self.assertTrue(isinstance(lang, unicode))
59
60if __name__ == "__main__":
61 if DEBUG:
62 logging.basicConfig(level=logging.DEBUG)
63 unittest.main()
64
65# vim: ts=4 et sts=4

Subscribers

People subscribed via source and target branches

to status/vote changes: