|
27 | 27 |
|
28 | 28 | def _getlang(): |
29 | 29 | # Figure out what the current language is set to. |
30 | | - current_lang = locale.getlocale(locale.LC_TIME)[0] |
31 | | - if current_lang: |
32 | | - return current_lang |
33 | | - else: |
34 | | - current_lang = locale.getdefaultlocale()[0] |
35 | | - if current_lang: |
36 | | - return current_lang |
37 | | - else: |
38 | | - return '' |
| 30 | + return locale.getlocale(locale.LC_TIME) |
39 | 31 |
|
40 | 32 | class LocaleTime(object): |
41 | 33 | """Stores and handles locale-specific information related to time. |
42 | 34 |
|
| 35 | + This is not thread-safe! Attributes are lazily calculated and no |
| 36 | + precaution is taken to check to see if the locale information has changed |
| 37 | + since the creation of the instance in use. |
| 38 | +
|
43 | 39 | ATTRIBUTES (all read-only after instance creation! Instance variables that |
44 | 40 | store the values have mangled names): |
45 | 41 | f_weekday -- full weekday names (7-item list) |
@@ -103,7 +99,10 @@ def __init__(self, f_weekday=None, a_weekday=None, f_month=None, |
103 | 99 | raise TypeError("timezone names must contain 2 items") |
104 | 100 | else: |
105 | 101 | self.__timezone = self.__pad(timezone, False) |
106 | | - self.__lang = lang |
| 102 | + if lang: |
| 103 | + self.__lang = lang |
| 104 | + else: |
| 105 | + self.__lang = _getlang() |
107 | 106 |
|
108 | 107 | def __pad(self, seq, front): |
109 | 108 | # Add '' to seq to either front (is True), else the back. |
@@ -196,13 +195,7 @@ def __get_LC_time(self): |
196 | 195 | LC_time = property(__get_LC_time, __set_nothing, |
197 | 196 | doc="Format string for locale's time representation ('%X' format)") |
198 | 197 |
|
199 | | - def __get_lang(self): |
200 | | - # Fetch self.lang. |
201 | | - if not self.__lang: |
202 | | - self.__calc_lang() |
203 | | - return self.__lang |
204 | | - |
205 | | - lang = property(__get_lang, __set_nothing, |
| 198 | + lang = property(lambda self: self.__lang, __set_nothing, |
206 | 199 | doc="Language used for instance") |
207 | 200 |
|
208 | 201 | def __calc_weekday(self): |
@@ -295,11 +288,6 @@ def __calc_timezone(self): |
295 | 288 | time_zones.append(time.tzname[0]) |
296 | 289 | self.__timezone = self.__pad(time_zones, 0) |
297 | 290 |
|
298 | | - def __calc_lang(self): |
299 | | - # Set self.__lang by using __getlang(). |
300 | | - self.__lang = _getlang() |
301 | | - |
302 | | - |
303 | 291 |
|
304 | 292 | class TimeRE(dict): |
305 | 293 | """Handle conversion from format directives to regexes.""" |
@@ -406,28 +394,12 @@ def compile(self, format): |
406 | 394 | """Return a compiled re object for the format string.""" |
407 | 395 | return re_compile(self.pattern(format), IGNORECASE) |
408 | 396 |
|
409 | | -# Cached TimeRE; probably only need one instance ever so cache it for performance |
410 | | -_locale_cache = TimeRE() |
411 | | -# Cached regex objects; same reason as for TimeRE cache |
412 | | -_regex_cache = dict() |
413 | 397 |
|
414 | 398 | def strptime(data_string, format="%a %b %d %H:%M:%S %Y"): |
415 | 399 | """Return a time struct based on the input data and the format string.""" |
416 | | - global _locale_cache |
417 | | - global _regex_cache |
418 | | - locale_time = _locale_cache.locale_time |
419 | | - # If the language changes, caches are invalidated, so clear them |
420 | | - if locale_time.lang != _getlang(): |
421 | | - _locale_cache = TimeRE() |
422 | | - _regex_cache.clear() |
423 | | - format_regex = _regex_cache.get(format) |
424 | | - if not format_regex: |
425 | | - # Limit regex cache size to prevent major bloating of the module; |
426 | | - # The value 5 is arbitrary |
427 | | - if len(_regex_cache) > 5: |
428 | | - _regex_cache.clear() |
429 | | - format_regex = _locale_cache.compile(format) |
430 | | - _regex_cache[format] = format_regex |
| 400 | + time_re = TimeRE() |
| 401 | + locale_time = time_re.locale_time |
| 402 | + format_regex = time_re.compile(format) |
431 | 403 | found = format_regex.match(data_string) |
432 | 404 | if not found: |
433 | 405 | raise ValueError("time data did not match format: data=%s fmt=%s" % |
|
0 commit comments