Skip to content

Commit

Permalink
Add ElementTree.fromstringlist()
Browse files Browse the repository at this point in the history
Include ``fromstringlist()`` method introduced in Python 3.2.

Closes: #47
Co-authored-by: Antoine <antoine.sb@orange.fr>
Signed-off-by: Christian Heimes <christian@python.org>
  • Loading branch information
tiran and AntoineSebert committed Mar 4, 2021
1 parent 62634db commit 1ac00c6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defusedxml 0.8.0.dev1
---------------------

- Drop support for Python 2.7, 3.4, and 3.5.
- Add ``defusedxml.ElementTree.fromstringlist()``


defusedxml 0.7.0
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ See <https://www.python.org/psf/license> for licensing details.
## defusedxml 0.8.0.dev1

- Drop support for Python 2.7, 3.4, and 3.5.
- Add `defusedxml.ElementTree.fromstringlist()`

## defusedxml 0.7.0

Expand Down
13 changes: 13 additions & 0 deletions defusedxml/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,26 @@ def fromstring(text, forbid_dtd=False, forbid_entities=True, forbid_external=Tru
XML = fromstring


def fromstringlist(sequence, forbid_dtd=False, forbid_entities=True, forbid_external=True):
parser = DefusedXMLParser(
target=_TreeBuilder(),
forbid_dtd=forbid_dtd,
forbid_entities=forbid_entities,
forbid_external=forbid_external,
)
for text in sequence:
parser.feed(text)
return parser.close()


__all__ = [
"ParseError",
"XML",
"XMLParse",
"XMLParser",
"XMLTreeBuilder",
"fromstring",
"fromstringlist",
"iterparse",
"parse",
"tostring",
Expand Down
2 changes: 2 additions & 0 deletions defusedxml/cElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
XMLParser,
XMLTreeBuilder,
fromstring,
fromstringlist,
iterparse,
parse,
tostring,
Expand All @@ -35,6 +36,7 @@
"XMLParser",
"XMLTreeBuilder",
"fromstring",
"fromstringlist",
"iterparse",
"parse",
"tostring",
Expand Down
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def parseString(self, xmlstring, **kwargs):
tree = self.module.fromstring(xmlstring, **kwargs)
return self.module.tostring(tree)

def parseStringList(self, sequence, **kwargs):
tree = self.module.fromstringlist(sequence, **kwargs)
return self.module.tostring(tree)

def iterparse(self, source, **kwargs):
return list(self.module.iterparse(source, **kwargs))

Expand All @@ -203,6 +207,12 @@ def test_aliases(self):
assert self.module.XMLParser is parser
assert self.module.XMLParse is parser

def test_fromstringlist(self):
seq = ["<root>", '<tag id="one" />', '<tag id="two" />', "</root>"]
tree = self.module.fromstringlist(seq)
result = self.module.tostring(tree)
self.assertEqual(result, "".join(seq).encode("utf-8"))

def test_import_order(self):
from xml.etree import ElementTree as second_elementtree

Expand Down

0 comments on commit 1ac00c6

Please sign in to comment.