Skip to content
Open
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
6 changes: 6 additions & 0 deletions changelog.d/_5-larkindom-empty-document.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixed
-----

* Set ``bozo`` and ``bozo_exception`` (to a new ``EmptyDocument`` error)
when parsing empty input, instead of silently returning a result that
looks like a valid, empty feed. (#461)
2 changes: 2 additions & 0 deletions feedparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .exceptions import (
CharacterEncodingOverride,
CharacterEncodingUnknown,
EmptyDocument,
FeedparserError,
NonXMLContentType,
UndeclaredNamespace,
Expand Down Expand Up @@ -61,6 +62,7 @@
"FeedparserError",
"CharacterEncodingOverride",
"CharacterEncodingUnknown",
"EmptyDocument",
"NonXMLContentType",
"UndeclaredNamespace",
)
6 changes: 5 additions & 1 deletion feedparser/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

from . import http
from .encodings import MissingEncoding, convert_file_to_utf8
from .exceptions import EmptyDocument
from .html import BaseHTMLProcessor
from .mixin import XMLParserMixin
from .parsers.json import JSONParser
Expand Down Expand Up @@ -214,9 +215,12 @@ def parse(

# at this point, the file is guaranteed to be seekable;
# we read 1 byte/character to see if it's empty and return early
# (this preserves the behavior in 6.0.8)
# (this preserves the 6.0.8 behavior of not attempting to parse empty
# input, but flags it via bozo instead of silently reporting success)
initial_file_offset = file.tell()
if not file.read(1):
result["bozo"] = True
result["bozo_exception"] = EmptyDocument("document is empty")
return result
file.seek(initial_file_offset)

Expand Down
5 changes: 5 additions & 0 deletions feedparser/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"FeedparserError",
"CharacterEncodingOverride",
"CharacterEncodingUnknown",
"EmptyDocument",
"NonXMLContentType",
"UndeclaredNamespace",
]
Expand All @@ -47,6 +48,10 @@ class CharacterEncodingUnknown(FeedparserError):
pass


class EmptyDocument(FeedparserError):
pass


class NonXMLContentType(FeedparserError):
pass

Expand Down
23 changes: 23 additions & 0 deletions tests/test_empty_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import io

import feedparser


def test_empty_bytestring_sets_bozo():
result = feedparser.parse(io.BytesIO(b""))
assert result["bozo"] is True
assert isinstance(result["bozo_exception"], feedparser.EmptyDocument)
assert result["entries"] == []
assert result["feed"] == {}


def test_empty_string_sets_bozo():
result = feedparser.parse(io.StringIO(""))
assert result["bozo"] is True
assert isinstance(result["bozo_exception"], feedparser.EmptyDocument)


def test_nonempty_feed_does_not_set_bozo_via_empty_document():
result = feedparser.parse(b"<feed><entry><title>t</title></entry></feed>")
exc = result.get("bozo_exception")
assert not isinstance(exc, feedparser.EmptyDocument)