1. Armin Rigo
  2. cpython-withatomic


Brett Cannon  committed dfabf0c

Clear the regex cache when the locale changes.

Backport of fix for bug #1290505.

  • Participants
  • Parent commits b8d1b45
  • Branches 2.4

Comments (0)

Files changed (3)

File Lib/_strptime.py

View file
 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
         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:
         format_regex = _regex_cache.get(format)

File Lib/test/test_strptime.py

View file
         # 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".

File Misc/NEWS

View file
+- 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