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 6653936 commit 8cbb1be
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

defusedxml 0.7.1
---------------------

*Release date: ??-Mar-2021*

- 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: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,14 @@ See <https://www.python.org/psf/license> for licensing details.
Injection](https://www.owasp.org/index.php/Testing_for_XML_Injection_\(OWASP-DV-008\))
# Changelog

## defusedxml 0.7.1

*Release date: ??-Mar-2021*

- 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

*Release date: 4-Mar-2021*
Expand Down
10 changes: 6 additions & 4 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 All @@ -20,7 +21,6 @@
else:
from xml.etree.ElementTree import XMLParser as _XMLParser
from xml.etree.ElementTree import iterparse as _iterparse
from xml.etree.ElementTree import ParseError


from .common import (
Expand Down Expand Up @@ -63,13 +63,15 @@ 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


if PY3:
_XMLParser, _iterparse, ParseError = _get_py3_cls()
_XMLParser, _iterparse = _get_py3_cls()


_sentinel = object()

Expand Down
2 changes: 1 addition & 1 deletion defusedxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def defuse_stdlib():
return defused


__version__ = "0.7.0"
__version__ = "0.7.1"

__all__ = [
"DefusedXmlException",
Expand Down
15 changes: 14 additions & 1 deletion defusedxml/cElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@
# iterparse from ElementTree!
from xml.etree.ElementTree import iterparse as _iterparse

from .ElementTree import DefusedXMLParser
# This module is an alias for ElementTree just like xml.etree.cElementTree
from .ElementTree import (
XML,
XMLParse,
XMLParser,
XMLTreeBuilder,
fromstring,
iterparse,
parse,
tostring,
DefusedXMLParser,
ParseError,
)

__origin__ = "xml.etree.cElementTree"

Expand All @@ -38,6 +50,7 @@
XML = fromstring

__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 @@ -214,6 +214,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 8cbb1be

Please sign in to comment.