changeset 591:d1618dfaf114 trunk

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).
author fschwarz
date Thu, 09 Aug 2012 07:36:36 +0000
parents 6f86d60dab56
children e6b1efa9a255
files ChangeLog babel/core.py babel/messages/tests/pofile.py babel/tests/core.py
diffstat 4 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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)
--- 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"
--- 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):
Copyright (C) 2012-2017 Edgewall Software