# HG changeset patch # User fschwarz # Date 1344497796 0 # Node ID d1618dfaf114836c40db6c35aaac4ddd22b69bcd # Parent 6f86d60dab569ae31c1eb46cf3db35b3571980af 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 --- a/ChangeLog +++ b/ChangeLog @@ -47,6 +47,8 @@ * 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 --- a/babel/core.py +++ b/babel/core.py @@ -214,7 +214,13 @@ 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 --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -16,6 +16,7 @@ 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 @@ 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 --- a/babel/tests/core.py +++ b/babel/tests/core.py @@ -31,6 +31,14 @@ 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):