]> granicus.if.org Git - python/commitdiff
time.strptime's caching of its locale object was being recreated when the
authorBrett Cannon <bcannon@gmail.com>
Sun, 1 Apr 2007 18:47:27 +0000 (18:47 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sun, 1 Apr 2007 18:47:27 +0000 (18:47 +0000)
locale changed but not used during the function call it was recreated during.

The test in this checkin is untested (OS X does not have the proper locale
support for me to test), although the fix for the bug this deals with
was tested by the OP (#1290505).  Once the buildbots verify the test at least
doesn't fail it becomes a backport candidate.

Lib/_strptime.py
Lib/test/test_strptime.py
Misc/NEWS

index 5ea59ed7be0dc8a7eb3a2815e0e5cb52652a2adc..9e7823abdd983952bd01215b716425484d9666f4 100644 (file)
@@ -295,17 +295,16 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
     """Return a time struct based on the input string and the format string."""
     global _TimeRE_cache, _regex_cache
     with _cache_lock:
-        time_re = _TimeRE_cache
-        locale_time = time_re.locale_time
-        if _getlang() != locale_time.lang:
+        if _getlang() != _TimeRE_cache.locale_time.lang:
             _TimeRE_cache = TimeRE()
-            _regex_cache = {}
+            _regex_cache.clear()
         if len(_regex_cache) > _CACHE_MAX_SIZE:
             _regex_cache.clear()
+        locale_time = _TimeRE_cache.locale_time
         format_regex = _regex_cache.get(format)
         if not format_regex:
             try:
-                format_regex = time_re.compile(format)
+                format_regex = _TimeRE_cache.compile(format)
             # KeyError raised when a bad format is found; can be specified as
             # \\, in which case it was a stray % but with a space after it
             except KeyError, err:
index 56e1ab87aa2840ff65214200a0753d1a6b453df7..48e6b31d38bfd47734dd99e10c825203494e2c2f 100644 (file)
@@ -505,6 +505,23 @@ class CacheTests(unittest.TestCase):
         self.failIfEqual(locale_time_id,
                          id(_strptime._TimeRE_cache.locale_time))
 
+    def test_TimeRE_recreation(self):
+        # The TimeRE instance should be recreated upon changing the locale.
+        locale_info = locale.getlocale(locale.LC_TIME)
+        try:
+            locale.setlocale(locale.LC_TIME, ('en_US', 'UTF8'))
+        except locale.Error:
+            return
+        try:
+            _strptime.strptime('10', '%d')
+            first_time_re_id = id(_strptime._TimeRE_cache)
+            locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
+            _strptime.strptime('10', '%d')
+            second_time_re_id = id(_strptime._TimeRE_cache)
+            self.failIfEqual(first_time_re_id, second_time_re_id)
+        finally:
+            locale.setlocale(locale.LC_TIME, locale_info)
+
 
 def test_main():
     test_support.run_unittest(
index 408de56c2a4120fa54e4380e0793f955f359e016..9da3d3bc2ffe1f8897be7782b6dc79ad2d926a17 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -200,6 +200,9 @@ Core and builtins
 Library
 -------
 
+- Bug #1290505: time.strptime's internal cache of locale information is now
+  properly recreated when the locale is changed.
+
 - Patch #1685563: remove (don't add) duplicate paths in distutils.MSVCCompiler.
 
 - Added a timeout parameter to the constructor of other protocols