]> granicus.if.org Git - python/commitdiff
Clear out the regex cache when the TimeRE cache is invalidated by a locale
authorBrett Cannon <bcannon@gmail.com>
Thu, 15 Sep 2005 02:34:56 +0000 (02:34 +0000)
committerBrett Cannon <bcannon@gmail.com>
Thu, 15 Sep 2005 02:34:56 +0000 (02:34 +0000)
change.

Fixes bug #1290505.

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

index 90928ffca8d28938febdefdd44d824a82c9a78e7..08d79603a01688baed19241b8d9b59e0c6d13ec4 100644 (file)
@@ -275,13 +275,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 f63d7118ed66bb6360f159fb283f486ba9496e3a..fca9b552200537f3a91b4e06234774774de438c4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -217,6 +217,8 @@ Extension Modules
 Library
 -------
 
+- Bug #1290505: Fix clearing the regex cache for time.strptime().
+
 - Bug #1167128: Fix size of a symlink in a tarfile to be 0.
 
 - Patch #810023: Fix off-by-one bug in urllib.urlretrieve reporthook