Skip to content

plistlib: an invalid XML <date> raises AttributeError/TypeError instead of a proper error #152957

Description

@tonghuaroot

Bug report

Bug description:

plistlib.loads() on an XML plist whose <date> element does not parse to a
datetime raises an internal AttributeError or TypeError instead of a
proper error. The sibling <integer> and <real> elements already raise
ValueError on malformed content, so a <date> should too.

_date_from_string (Lib/plistlib.py) does _dateParser.match(s).groupdict().
On a fully non-matching value match() returns None, so .groupdict() fails
with AttributeError. On a partial match (the regex allows year-only and
year-month), too few fields reach datetime.datetime(*lst), which fails with
TypeError.

import plistlib
plistlib.loads(b"<plist><date>not a date</date></plist>")  # AttributeError
plistlib.loads(b"<plist><date>2004Z</date></plist>")       # TypeError

The binary plist format is unaffected: it decodes dates with struct.unpack
and never uses this regex.

CPython versions tested on:

3.15 (main); the affected code is unchanged on 3.13 and 3.14.

Operating systems tested on:

Any (pure Python)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions