summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2009-03-30 21:30:26 (GMT)
committerBrett Cannon <bcannon@gmail.com>2009-03-30 21:30:26 (GMT)
commit7f6b4f86e305ec63441657c57f5f546d15114ca0 (patch)
treef7b41eb0be99b61e60e35d9e36746c56ef0a5d7f /Lib
parentd687887b536bf1674ac45f78bb77b346aa2b7687 (diff)
downloadcpython-7f6b4f86e305ec63441657c57f5f546d15114ca0.zip
cpython-7f6b4f86e305ec63441657c57f5f546d15114ca0.tar.gz
cpython-7f6b4f86e305ec63441657c57f5f546d15114ca0.tar.bz2
Make sure time.strptime only accepts strings (and document the fact like
strftime). Already didn't accept bytes but make the check earlier. This also lifts the limitation of requiring ASCII. Closes issue #5236. Thanks Tennessee Leeuwenburg.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_strptime.py9
-rw-r--r--Lib/test/test_time.py5
2 files changed, 13 insertions, 1 deletions
diff --git a/Lib/_strptime.py b/Lib/_strptime.py
index 896c798..9ff29bc 100644
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -262,7 +262,7 @@ class TimeRE(dict):
def compile(self, format):
"""Return a compiled re object for the format string."""
- return re_compile(self.pattern(format), IGNORECASE | ASCII)
+ return re_compile(self.pattern(format), IGNORECASE)
_cache_lock = _thread_allocate_lock()
# DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock
@@ -294,8 +294,15 @@ def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
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."""
+
+ for index, arg in enumerate([data_string, format]):
+ if not isinstance(arg, str):
+ msg = "strptime() argument {} must be str, not {}"
+ raise TypeError(msg.format(arg, index))
+
global _TimeRE_cache, _regex_cache
with _cache_lock:
+
if _getlang() != _TimeRE_cache.locale_time.lang:
_TimeRE_cache = TimeRE()
_regex_cache.clear()
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index 4c89f03..c2dfaca 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -116,6 +116,11 @@ class TimeTestCase(unittest.TestCase):
self.fail("conversion specifier %r failed with '%s' input." %
(format, strf_output))
+ def test_strptime_bytes(self):
+ # Make sure only strings are accepted as arguments to strptime.
+ self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
+ self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
+
def test_asctime(self):
time.asctime(time.gmtime(self.t))
self.assertRaises(TypeError, time.asctime, 0)