]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Clear the regex cache when the locale changes.
authorBrett Cannon <bcannon@gmail.com>
Thu, 15 Sep 2005 02:42:05 +0000 (02:42 +0000)
committerBrett Cannon <bcannon@gmail.com>
Thu, 15 Sep 2005 02:42:05 +0000 (02:42 +0000)
Backport of fix for bug #1290505.

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

index f8dbdd5679b1bf7d92f4b4150aff2c1e7017f0cf..3bd5a8f2384bed929cc227eff117f15b3a471842 100644 (file)
@@ -272,13 +272,14 @@ _regex_cache = {}
 
 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
+    global _TimeRE_cache, _regex_cache
     _cache_lock.acquire()
     try:
         time_re = _TimeRE_cache
         locale_time = time_re.locale_time
         if _getlang() != locale_time.lang:
             _TimeRE_cache = TimeRE()
+            _regex_cache = {}
         if len(_regex_cache) > _CACHE_MAX_SIZE:
             _regex_cache.clear()
         format_regex = _regex_cache.get(format)
index 785497a8d5539f261d4a217f62d32126098e81eb..f9763aa34c2c0c8a3adc4e901886cffbc497dc06 100644 (file)
@@ -462,10 +462,12 @@ class CacheTests(unittest.TestCase):
         # Make sure cache is recreated when current locale does not match what
         # cached object was created with.
         _strptime.strptime("10", "%d")
+        _strptime.strptime("2005", "%Y")
         _strptime._TimeRE_cache.locale_time.lang = "Ni"
         original_time_re = id(_strptime._TimeRE_cache)
         _strptime.strptime("10", "%d")
         self.failIfEqual(original_time_re, id(_strptime._TimeRE_cache))
+        self.failUnlessEqual(len(_strptime._regex_cache), 1)
 
     def test_regex_cleanup(self):
         # Make sure cached regexes are discarded when cache becomes "full".
index cbc6dc146e3fc557f1c44644e9bf464555ba10b5..793c4c35350eca8e4ad45e1226c1a49b4c1f6ba4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -92,6 +92,9 @@ Extension Modules
 Library
 -------
 
+- Bug #1290505: time.strptime() was not invalidating its regex cache when the
+  locale changed.
+
 - Fix a misuse of str.find() in detection of use of %U or %W in datetime
   format.