aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-12-05 19:29:10 +0000
committerMichael Williamson <mike@zwobble.org>2026-12-05 19:29:10 +0000
commit414cb49cd08d4952cf5eff51bd9e2da811c4661b (patch)
treeb237b5f402d7f7f5f80c6c93bc1c3415e9cfe146
parent3646167ab5685ea5a69de24442cf46e963bf7f41 (diff)
Revert "Handle XML documents that are missing a root element"
This reverts commit 659c4ffe6ade2d8132873ae3f3f3a22df38fdb37.
-rw-r--r--NEWS4
-rw-r--r--mammoth/docx/__init__.py26
-rw-r--r--mammoth/docx/xmlparser.py12
-rw-r--r--tests/docx/docx_tests.py33
-rw-r--r--tests/docx/office_xml_tests.py8
-rw-r--r--tests/docx/xmlparser_tests.py9
6 files changed, 12 insertions, 80 deletions
diff --git a/NEWS b/NEWS
index c189e19..daf4a2a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,7 +1,3 @@
-# 1.13.1
-
-* Handle XML documents that are missing a root element.
-
# 1.13.0
* Read the children of w:customXml elements.
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py
index 0579d09..0c7241d 100644
--- a/mammoth/docx/__init__.py
+++ b/mammoth/docx/__init__.py
@@ -12,7 +12,7 @@ from .styles_xml import read_styles_xml_element, Styles
from .notes_xml import read_endnotes_xml_element, read_footnotes_xml_element
from .comments_xml import read_comments_xml_element
from .files import Files
-from . import body_xml, office_xml, xmlparser
+from . import body_xml, office_xml
from ..zips import open_zip
@@ -130,17 +130,14 @@ def _read_comments(read_part_with_body, part_paths):
def _read_document(read_part_with_body, notes, comments, part_paths):
- try:
- return read_part_with_body(
- part_paths.main_document,
- partial(
- read_document_xml_element,
- notes=notes,
- comments=comments,
- ),
- )
- except xmlparser.NoRootElementError:
- raise IOError("Main document part is missing root element. Are you sure this is a valid .docx file?")
+ return read_part_with_body(
+ part_paths.main_document,
+ partial(
+ read_document_xml_element,
+ notes=notes,
+ comments=comments,
+ ),
+ )
def _part_with_body_reader(document_path, zip_file, part_paths, external_file_access):
@@ -206,10 +203,7 @@ def _read_relationships(zip_file, name):
def _try_read_entry_or_default(zip_file, name, reader, default):
if zip_file.exists(name):
- try:
- return _read_entry(zip_file, name, reader)
- except xmlparser.NoRootElementError:
- return default
+ return _read_entry(zip_file, name, reader)
else:
return default
diff --git a/mammoth/docx/xmlparser.py b/mammoth/docx/xmlparser.py
index d78e610..1ba415d 100644
--- a/mammoth/docx/xmlparser.py
+++ b/mammoth/docx/xmlparser.py
@@ -1,5 +1,4 @@
import xml.dom.minidom
-import xml.parsers.expat.errors
import cobble
@@ -75,9 +74,6 @@ XmlElement.node_type = node_types.element
XmlText.node_type = node_types.text
-class NoRootElementError(Exception):
- pass
-
def parse_xml(fileobj, namespace_mapping=None):
if namespace_mapping is None:
@@ -85,13 +81,7 @@ def parse_xml(fileobj, namespace_mapping=None):
else:
namespace_prefixes = dict((uri, prefix) for prefix, uri in namespace_mapping)
- try:
- document = xml.dom.minidom.parse(fileobj)
- except xml.parsers.expat.ExpatError as error:
- if error.code == xml.parsers.expat.errors.codes[xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS]:
- raise NoRootElementError()
- else:
- raise
+ document = xml.dom.minidom.parse(fileobj)
def convert_node(node):
if node.nodeType == xml.dom.Node.ELEMENT_NODE:
diff --git a/tests/docx/docx_tests.py b/tests/docx/docx_tests.py
index c409e11..ee6b928 100644
--- a/tests/docx/docx_tests.py
+++ b/tests/docx/docx_tests.py
@@ -72,39 +72,6 @@ def test_error_is_raised_when_main_document_part_does_not_exist():
str(error),
)
-
-def test_when_main_document_part_is_empty_then_error_is_raised():
- fileobj = _create_zip({
- "word/document.xml": textwrap.dedent("""\
- <?xml version="1.0" encoding="utf-8" ?>
- """),
- })
-
- error = assert_raises(IOError, lambda: docx.read(fileobj=fileobj))
-
- assert_equal(
- "Main document part is missing root element. Are you sure this is a valid .docx file?",
- str(error),
- )
-
-
-def test_when_part_other_than_main_document_is_empty_then_part_is_ignored():
- fileobj = _create_zip({
- "word/document.xml": textwrap.dedent("""\
- <?xml version="1.0" encoding="utf-8" ?>
- <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
- <w:body/>
- </w:document>
- """),
- "word/footnotes.xml": textwrap.dedent("""\
- <?xml version="1.0" encoding="utf-8" ?>
- """),
- })
- result = docx.read(fileobj=fileobj)
- expected_document = documents.document([])
- assert_equal(expected_document, result.value)
-
-
class PartPathsTests(object):
def test_main_document_part_is_found_using_package_relationships(self):
fileobj = _create_zip({
diff --git a/tests/docx/office_xml_tests.py b/tests/docx/office_xml_tests.py
index 34566ff..05897db 100644
--- a/tests/docx/office_xml_tests.py
+++ b/tests/docx/office_xml_tests.py
@@ -2,18 +2,10 @@ from __future__ import unicode_literals
import io
-import pytest
-
from mammoth.docx import xmlparser as xml, office_xml
from ..testing import assert_equal
-def test_when_document_is_missing_root_element_then_none_is_returned():
- xml_string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
-
- pytest.raises(xml.NoRootElementError, lambda: office_xml.read(io.StringIO(xml_string)))
-
-
class AlternateContentTests(object):
def test_when_fallback_is_present_then_fallback_is_read(self):
xml_string = (
diff --git a/tests/docx/xmlparser_tests.py b/tests/docx/xmlparser_tests.py
index 65479fc..75fc35a 100644
--- a/tests/docx/xmlparser_tests.py
+++ b/tests/docx/xmlparser_tests.py
@@ -2,7 +2,7 @@ import io
import pytest
-from mammoth.docx.xmlparser import parse_xml, element as xml_element, text as xml_text, NoRootElementError
+from mammoth.docx.xmlparser import parse_xml, element as xml_element, text as xml_text
from ..testing import assert_equal
@@ -60,13 +60,6 @@ def test_error_if_xml_is_badly_formed():
pytest.raises(Exception, lambda: _parse_xml_string(b'<bo'))
-def test_when_document_is_missing_root_element_then_error_is_raised():
- pytest.raises(
- NoRootElementError,
- lambda: _parse_xml_string(b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'),
- )
-
-
class FindChildTests(object):
def test_returns_none_if_no_children(self):
xml = xml_element("a")