Return an empty enclosures list for items with no link (fixes #340)#569
Open
gaoflow wants to merge 1 commit into
Open
Return an empty enclosures list for items with no link (fixes #340)#569gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
The computed "enclosures" and "license" keys iterate over the "links" key, which is only created lazily when a link is parsed. A feed or entry that has an id but no link therefore had no "links" key, so accessing enclosures raised KeyError (surfaced as AttributeError on attribute access) instead of returning an empty list as documented. Seed an empty "links" list when an entry or the feed is created so enclosures returns [] and license is absent. Bare FeedParserDict instances are untouched, preserving the documented behaviour on an empty dict. Fixes kurtmckee#340.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #340.
You agreed back in 2023 that this should not crash; since the issue is still open (and was bumped again in 2024) I put together a fix. Happy to defer if you would rather take it from here.
The bug
Accessing
enclosures(orlicense) on a feed or entry that has an id but no link crashes instead of returning an empty list:Reproduced on
main(6.0.12) for both Atom and RSS. The documented contract is thatenclosuresis always a list.Cause
The computed
enclosuresandlicensekeys infeedparser/util.pyiterate overdict.__getitem__(self, "links"). Thelinkskey is only created lazily (setdefault("links", [])) when a<link>is actually parsed, so an entry or feed with no link never gets it, and the iteration raisesKeyError: 'links'(surfaced asAttributeErroron attribute access).Fix
Seed an empty
linkslist when an entry (_base.py_start_item) or the feed (mixin.py.__init__) is created.enclosuresthen returns[]andlicenseis simply absent, matching the docs. BareFeedParserDict()instances are deliberately left untouched, sotest_empty(which assertsenclosures/licenseare absent on an empty dict) still holds.The
util.pyone-linerdict.get(self, "links", [])would be simpler, but it breakstest_emptybecause it cannot distinguish a parsed-but-link-less object from a bare dict. Seeding at parse time preserves that distinction.Scope note
The issue is about entries, but the identical crash exists at the feed level (
feed.license/feed.enclosureson a feed with no link), so I fixed both. If you would prefer to keep this scoped to the entry case in #340, I am happy to drop themixin.pychange and its fixture.Tests
Added three well-formed fixtures (an Atom entry, an RSS item, and a feed, each with an id/guid but no link) asserting
enclosures == []. Without the fix these fail withKeyError: 'links'; with it the full suite is green (4303 passed, 8 skipped).Disclosure: I prepared this fix with AI assistance under my direction; I reviewed and verified the change and the tests myself.