From: Felix Schwarz Date: Thu, 9 Aug 2012 07:36:36 +0000 (+0000) Subject: change Locale comparison: Locales are now considered equal if all of their attributes... X-Git-Tag: 1.0~156 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3279328f8d17177157a9acd6deba49fbf12be045;p=thirdparty%2Fbabel.git change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information). --- diff --git a/ChangeLog b/ChangeLog index f6637851..299dd921 100644 --- a/ChangeLog +++ b/ChangeLog @@ -47,6 +47,8 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/ * handle irregular multi-line msgstr (no "" as first line) gracefully (#171) * parse_decimal() now returns Decimals not floats, API change (#178) * no warnings when running setup.py without installed setuptools (#262) + * modified Locale.__eq__ method so Locales are only equal if all of their + attributes (language, territory, script, variant) are equal Version 0.9.6 diff --git a/babel/core.py b/babel/core.py index 083f0580..c9608f71 100644 --- a/babel/core.py +++ b/babel/core.py @@ -214,7 +214,13 @@ class Locale(object): return identifier def __eq__(self, other): - return str(self) == str(other) + for key in ('language', 'territory', 'script', 'variant'): + if not hasattr(other, key): + return False + return (self.language == other.language) and \ + (self.territory == other.territory) and \ + (self.script == other.script) and \ + (self.variant == other.variant) def __ne__(self, other): return not self.__eq__(other) diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py index bb3f5487..7e5904f1 100644 --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -16,6 +16,7 @@ import doctest from StringIO import StringIO import unittest +from babel.core import Locale from babel.messages.catalog import Catalog, Message from babel.messages import pofile from babel.util import FixedOffsetTimezone @@ -27,7 +28,7 @@ class ReadPoTestCase(unittest.TestCase): buf = StringIO(r'''msgid "foo" msgstr "Voh"''') catalog = pofile.read_po(buf, locale='en_US') - self.assertEqual('en_US', catalog.locale) + self.assertEqual(Locale('en', 'US'), catalog.locale) def test_preserve_domain(self): buf = StringIO(r'''msgid "foo" diff --git a/babel/tests/core.py b/babel/tests/core.py index 1e758820..03083787 100644 --- a/babel/tests/core.py +++ b/babel/tests/core.py @@ -31,6 +31,14 @@ class LocaleTest(unittest.TestCase): repr(Locale('de', 'DE'))) self.assertEqual("Locale('zh', territory='CN', script='Hans')", repr(Locale('zh', 'CN', script='Hans'))) + + def test_locale_comparison(self): + en_US = Locale('en', 'US') + self.assertEqual(en_US, en_US) + self.assertNotEqual(None, en_US) + + bad_en_US = Locale('en_US') + self.assertNotEqual(en_US, bad_en_US) class DefaultLocaleTest(unittest.TestCase):