Skip to content

Commit

Permalink
Fix regression defusedxml.ElementTree.ParseError
Browse files Browse the repository at this point in the history
The ``defusedxml.ElementTree.ParseError`` exception is now the same class object
as ``xml.etree.ElementTree.ParseError`` again. The regression was
introduced by new patching code as part of PR #60.

See: #60
Fixes: #63
Signed-off-by: Christian Heimes <[email protected]>
  • Loading branch information
tiran committed Mar 8, 2021
1 parent 1ac00c6 commit 1dcfddc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ defusedxml 0.8.0.dev1

- Drop support for Python 2.7, 3.4, and 3.5.
- Add ``defusedxml.ElementTree.fromstringlist()``
- Fix regression ``defusedxml.ElementTree.ParseError`` (#63)
The ``ParseError`` exception is now the same class object as
``xml.etree.ElementTree.ParseError`` again.


defusedxml 0.7.0
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@ See <https://www.python.org/psf/license> for licensing details.

- Drop support for Python 2.7, 3.4, and 3.5.
- Add `defusedxml.ElementTree.fromstringlist()`
- Fix regression `defusedxml.ElementTree.ParseError` (\#63) The
`ParseError` exception is now the same class object as
`xml.etree.ElementTree.ParseError` again.

## defusedxml 0.7.0

Expand Down
8 changes: 5 additions & 3 deletions defusedxml/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import sys
import warnings
from xml.etree.ElementTree import ParseError
from xml.etree.ElementTree import TreeBuilder as _TreeBuilder
from xml.etree.ElementTree import parse as _parse
from xml.etree.ElementTree import tostring
Expand Down Expand Up @@ -51,12 +52,13 @@ def _get_py3_cls():

_XMLParser = pure_pymod.XMLParser
_iterparse = pure_pymod.iterparse
ParseError = pure_pymod.ParseError
# patch pure module to use ParseError from C extension
pure_pymod.ParseError = ParseError

return _XMLParser, _iterparse, ParseError
return _XMLParser, _iterparse


_XMLParser, _iterparse, ParseError = _get_py3_cls()
_XMLParser, _iterparse = _get_py3_cls()

_sentinel = object()

Expand Down
2 changes: 2 additions & 0 deletions defusedxml/cElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
parse,
tostring,
DefusedXMLParser,
ParseError,
)

__origin__ = "xml.etree.cElementTree"
Expand All @@ -31,6 +32,7 @@
)

__all__ = [
"ParseError",
"XML",
"XMLParse",
"XMLParser",
Expand Down
9 changes: 9 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ def test_import_order(self):

self.assertIs(orig_elementtree, second_elementtree)

def test_orig_parseerror(self):
# https://github.com/tiran/defusedxml/issues/63
self.assertIs(self.module.ParseError, orig_elementtree.ParseError)
try:
self.parseString("invalid")
except Exception as e:
self.assertIsInstance(e, orig_elementtree.ParseError)
self.assertIsInstance(e, self.module.ParseError)


class TestDefusedcElementTree(TestDefusedElementTree):
module = cElementTree
Expand Down

0 comments on commit 1dcfddc

Please sign in to comment.