[Python-checkins] python/dist/src/Lib/test test_strptime.py, 1.19.4.1, 1.19.4.2

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Wed Aug 6 15:17:11 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv22315a/Lib/test

Modified Files:
      Tag: release23-maint
	test_strptime.py 
Log Message:
Re-introduction of caching.  Not thread-safe against the changing of locale
in the middle of executing time.strptime .  Added new tests for caching
mechanism; taken from 2.4 branch and tweaked appropriately.


Index: test_strptime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_strptime.py,v
retrieving revision 1.19.4.1
retrieving revision 1.19.4.2
diff -C2 -d -r1.19.4.1 -r1.19.4.2
*** test_strptime.py	4 Aug 2003 22:49:42 -0000	1.19.4.1
--- test_strptime.py	6 Aug 2003 21:17:09 -0000	1.19.4.2
***************
*** 418,421 ****
--- 418,461 ----
                          "Calculation of day of the week failed;"
                           "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday))
+ 
+ class CacheTests(unittest.TestCase):
+     """Test that caching works properly."""
+ 
+     def test_time_re_recreation(self):
+         # Make sure cache is recreated when current locale does not match what
+         # cached object was created with.
+         _strptime.strptime("10", "%d")
+         _strptime._locale_cache.locale_time = _strptime.LocaleTime(lang="Ni")
+         original_time_re = id(_strptime._locale_cache)
+         _strptime.strptime("10", "%d")
+         self.failIfEqual(original_time_re, id(_strptime._locale_cache))
+ 
+     def test_regex_cleanup(self):
+         # Make sure cached regexes are discarded when cache becomes "full".
+         try:
+             del _strptime._regex_cache['%d']
+         except KeyError:
+             pass
+         bogus_key = 0
+         while len(_strptime._regex_cache) <= 5:
+             _strptime._regex_cache[bogus_key] = None
+             bogus_key += 1
+         _strptime.strptime("10", "%d")
+         self.failUnlessEqual(len(_strptime._regex_cache), 1)
+ 
+     def test_new_localetime(self):
+         # A new LocaleTime instance should be created when a new TimeRE object
+         # is created.
+         _strptime._locale_cache.locale_time = _strptime.LocaleTime(lang="Ni")
+         locale_time_id = id(_strptime._locale_cache.locale_time)
+         locale_time_lang = _strptime._locale_cache.locale_time.lang
+         _strptime.strptime("10", "%d")
+         self.failIfEqual(locale_time_id,
+                          id(_strptime._locale_cache.locale_time))
+         self.failIfEqual(locale_time_lang,
+                          _strptime._locale_cache.locale_time.lang)
+ 
+ 
+ 
  def test_main():
      test_support.run_unittest(
***************
*** 427,430 ****
--- 467,471 ----
          JulianTests,
          CalculationTests,
+         CacheTests
      )
  





More information about the Python-checkins mailing list