Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 13 additions & 2 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def _parse_hh_mm_ss_ff(tstr):

return time_comps

def _parse_isoformat_time(tstr):
def _parse_isoformat_time(tstr, is_expanded=False):
# Format supported is HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]
len_str = len(tstr)
if len_str < 2:
Expand Down Expand Up @@ -493,6 +493,14 @@ def _parse_isoformat_time(tstr):
if len(tzstr) in (0, 1, 3) or tstr[tz_pos-1] == 'Z':
raise ValueError("Malformed time zone string")

if is_expanded and ":" not in tzstr and len(tzstr) > 2:
import warnings
warnings.warn(
"Support for partially expanded formats are deprecated in "
"accordance with ISO 8601:2 and will be removed in 3.15",
DeprecationWarning
)

tz_comps = _parse_hh_mm_ss_ff(tzstr)

if all(x == 0 for x in tz_comps):
Expand Down Expand Up @@ -1943,7 +1951,10 @@ def fromisoformat(cls, date_string):

if tstr:
try:
time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr)
is_expanded = date_string[4] == "-"
time_components, became_next_day, error_from_components = (
_parse_isoformat_time(tstr, is_expanded)
)
except ValueError:
raise ValueError(
f'Invalid isoformat string: {date_string!r}') from None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate support for invalid ISO formats in :func:`datetime.datetime.fromisoformat`
8 changes: 7 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,13 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
int tzsign = (*tzinfo_pos == '-') ? -1 : 1;
tzinfo_pos++;
int tzhour = 0, tzminute = 0, tzsecond = 0;

if (tzinfo_pos + 2 < p_end && tzinfo_pos[2] != ':' && strlen(tzinfo_pos) > 2) {
PyErr_WarnEx(PyExc_DeprecationWarning,
"Support for partially expanded formats is deprecated in "
"accordance with ISO 8601:2 and will be removed in 3.15", 1);
}

rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond,
tzmicrosecond);

Expand Down Expand Up @@ -5893,7 +5900,6 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
const Py_ssize_t separator_location = _find_isoformat_datetime_separator(
dt_ptr, len);


const char *p = dt_ptr;

int year = 0, month = 0, day = 0;
Expand Down
Loading