[Python-checkins] python/dist/src/Lib _strptime.py,1.23,1.23.4.1

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
In directory sc8-pr-cvs1:/tmp/cvs-serv22315a/Lib

Modified Files:
      Tag: release23-maint
	_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: _strptime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/_strptime.py,v
retrieving revision 1.23
retrieving revision 1.23.4.1
diff -C2 -d -r1.23 -r1.23.4.1
*** _strptime.py	24 Jul 2003 20:02:28 -0000	1.23
--- _strptime.py	6 Aug 2003 21:17:09 -0000	1.23.4.1
***************
*** 398,407 ****
          return re_compile(self.pattern(format), IGNORECASE)
  
  
  def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
      """Return a time struct based on the input data and the format string."""
!     time_re = TimeRE()
!     locale_time = time_re.locale_time
!     format_regex = time_re.compile(format)
      found = format_regex.match(data_string)
      if not found:
--- 398,423 ----
          return re_compile(self.pattern(format), IGNORECASE)
  
+ # Cached TimeRE; probably only need one instance ever so cache it for performance
+ _locale_cache = TimeRE()
+ # Cached regex objects; same reason as for TimeRE cache
+ _regex_cache = dict()
  
  def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
      """Return a time struct based on the input data and the format string."""
!     global _locale_cache
!     global _regex_cache
!     locale_time = _locale_cache.locale_time
!     # If the language changes, caches are invalidated, so clear them
!     if locale_time.lang != _getlang():
!         _locale_cache = TimeRE()
!         _regex_cache.clear()
!     format_regex = _regex_cache.get(format)
!     if not format_regex:
!         # Limit regex cache size to prevent major bloating of the module;
!         # The value 5 is arbitrary
!         if len(_regex_cache) > 5:
!             _regex_cache.clear()
!         format_regex = _locale_cache.compile(format)
!         _regex_cache[format] = format_regex
      found = format_regex.match(data_string)
      if not found:





More information about the Python-checkins mailing list