Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,15 +564,15 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
del err
bad_directive = bad_directive.replace('\\s', '')
if not bad_directive:
raise ValueError("stray %% in format '%s'" % format) from None
raise ValueError(f"stray % in format {format!r}") from None
bad_directive = bad_directive.replace('\\', '', 1)
raise ValueError("'%s' is a bad directive in format '%s'" %
(bad_directive, format)) from None
raise ValueError(f"{bad_directive!r} is a bad directive "
f"in format {format!r}") from None
_regex_cache[format] = format_regex
found = format_regex.match(data_string)
if not found:
raise ValueError("time data %r does not match format %r" %
(data_string, format))
raise ValueError(f"time data {data_string!r} does not match "
f"format {format!r}")
if len(data_string) != found.end():
rest = data_string[found.end():]
# Specific check for '%:z' directive
Expand All @@ -582,9 +582,9 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
and rest[0] != ":"
):
raise ValueError(
f"Missing colon in %:z before '{rest}', got '{data_string}'"
f"Missing colon in %:z before {rest!r}, got {data_string!r}"
)
raise ValueError("unconverted data remains: %s" % rest)
raise ValueError(f"unconverted data remains: {rest!r}")

iso_year = year = None
month = day = 1
Expand Down Expand Up @@ -700,7 +700,7 @@ def parse_int(s):
z = z[:3] + z[4:]
if len(z) > 5:
if z[5] != ':':
msg = f"Inconsistent use of : in {found_dict[group_key]}"
msg = f"Inconsistent use of : in {found_dict[group_key]!r}"
raise ValueError(msg)
z = z[:5] + z[6:]
hours = int(z[1:3])
Expand Down
11 changes: 9 additions & 2 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ def test_ValueError(self):
directive = bad_format[1:].rstrip()
with (self.subTest(format=bad_format),
self.assertRaisesRegex(ValueError,
f"'{re.escape(directive)}' is a bad directive in format ")):
f"{re.escape(repr(directive))} is a bad directive "
f"in format ")):
_strptime._strptime_time("2005", bad_format)

msg_week_no_year_or_weekday = r"ISO week directive '%V' must be used with " \
Expand Down Expand Up @@ -303,6 +304,11 @@ def test_unconverteddata(self):
# Check ValueError is raised when there is unconverted data
self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m")

# gh-141540: a trailing newline must be visible in the message
with self.assertRaisesRegex(ValueError,
r"unconverted data remains: '\\n'"):
_strptime._strptime_time("2001-02-03\n", "%Y-%m-%d")

def roundtrip(self, fmt, position, time_tuple=None):
"""Helper fxn in testing."""
if time_tuple is None:
Expand Down Expand Up @@ -451,7 +457,8 @@ def test_bad_offset(self):

with self.assertRaises(ValueError) as err:
_strptime._strptime("-01:3030", "%z")
self.assertEqual("Inconsistent use of : in -01:3030", str(err.exception))
self.assertEqual("Inconsistent use of : in '-01:3030'",
str(err.exception))
with self.assertRaises(ValueError) as err:
_strptime._strptime("-01:3030", "%:z")
self.assertEqual("Missing colon in %:z before '30', got '-01:3030'",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Quote the unconverted data and the format in the error messages of
:func:`time.strptime` and :meth:`datetime.datetime.strptime` so that
whitespace such as a trailing newline is visible.
Loading