[Python-checkins] python/dist/src/Lib _strptime.py,1.21,1.22

bcannon@users.sourceforge.net bcannon@users.sourceforge.net
Wed, 23 Jul 2003 23:27:19 -0700


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

Modified Files:
	_strptime.py 
Log Message:
Remove caching of TimeRE (and thus LocaleTime) instance.  Error was being
caught when executing test_strptime, test_logging, and test_time in that order
when the testing of "%c" occured.  Suspect the cache was not being recreated
(the test passed when test_logging was forced to re-establish the locale).


Index: _strptime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/_strptime.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** _strptime.py	13 Jul 2003 01:31:38 -0000	1.21
--- _strptime.py	24 Jul 2003 06:27:17 -0000	1.22
***************
*** 28,44 ****
  def _getlang():
      # Figure out what the current language is set to.
!     current_lang = locale.getlocale(locale.LC_TIME)[0]
!     if current_lang:
!         return current_lang
!     else:
!         current_lang = locale.getdefaultlocale()[0]
!         if current_lang:
!             return current_lang
!         else:
!             return ''
  
  class LocaleTime(object):
      """Stores and handles locale-specific information related to time.
  
      ATTRIBUTES (all read-only after instance creation! Instance variables that
                  store the values have mangled names):
--- 28,40 ----
  def _getlang():
      # Figure out what the current language is set to.
!     return locale.getlocale(locale.LC_TIME)
  
  class LocaleTime(object):
      """Stores and handles locale-specific information related to time.
  
+     This is not thread-safe!  Attributes are lazily calculated and no
+     precaution is taken to check to see if the locale information has changed
+     since the creation of the instance in use.
+ 
      ATTRIBUTES (all read-only after instance creation! Instance variables that
                  store the values have mangled names):
***************
*** 104,108 ****
              else:
                  self.__timezone = self.__pad(timezone, False)
!         self.__lang = lang
  
      def __pad(self, seq, front):
--- 100,107 ----
              else:
                  self.__timezone = self.__pad(timezone, False)
!         if lang:
!             self.__lang = lang
!         else:
!             self.__lang = _getlang()
  
      def __pad(self, seq, front):
***************
*** 197,207 ****
          doc="Format string for locale's time representation ('%X' format)")
  
!     def __get_lang(self):
!         # Fetch self.lang.
!         if not self.__lang:
!             self.__calc_lang()
!         return self.__lang
! 
!     lang = property(__get_lang, __set_nothing,
                      doc="Language used for instance")
  
--- 196,200 ----
          doc="Format string for locale's time representation ('%X' format)")
  
!     lang = property(lambda self: self.__lang, __set_nothing,
                      doc="Language used for instance")
  
***************
*** 296,304 ****
          self.__timezone = self.__pad(time_zones, 0)
  
-     def __calc_lang(self):
-         # Set self.__lang by using __getlang().
-         self.__lang = _getlang()
- 
- 
  
  class TimeRE(dict):
--- 289,292 ----
***************
*** 407,432 ****
          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:
--- 395,404 ----
          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: