Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SL-18330: In XML formatter, avoid adding call stack depth. #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 8, 2023
Merged
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
33 changes: 16 additions & 17 deletions llsd/serde_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ def _elt(self, name, contents=None):
If 'contents' is omitted, write <name/>.
If 'contents' is bytes, write <name>contents</name>.
If 'contents' is str, write <name>contents.encode('utf8')</name>.
If 'contents' is callable, write <name>, call contents(), write </name>.
"""
if not contents:
self.stream.writelines([b"<", name, b" />"])
else:
self.stream.writelines([b"<", name, b">"])
if callable(contents):
contents()
else:
self.stream.write(_str_to_bytes(contents))
self.stream.writelines([b"</", name, b">"])
self.stream.writelines([b"<", name, b">",
_str_to_bytes(contents),
b"</", name, b">"])

def xml_esc(self, v):
"Escape string or unicode object v for xml output"
Expand Down Expand Up @@ -100,15 +96,16 @@ def _URI(self, v):
def _DATE(self, v):
return self._elt(b'date', _format_datestr(v))
def _ARRAY(self, v):
return self._elt(
b'array',
lambda: [self._generate(item) for item in v])
self.stream.write(b'<array>')
for item in v:
self._generate(item)
self.stream.write(b'</array>')
def _MAP(self, v):
return self._elt(
b'map',
lambda: [(self._elt(b'key', self.xml_esc(UnicodeType(key))),
self._generate(value))
for key, value in v.items()])
self.stream.write(b'<map>')
for key, value in v.items():
self._elt(b'key', self.xml_esc(UnicodeType(key)))
self._generate(value)
self.stream.write(b'</map>')

def _generate(self, something):
"Generate xml from a single python object."
Expand All @@ -127,8 +124,10 @@ def _write(self, something):

:param something: A python object (typically a dict) to be serialized.
"""
self.stream.write(b'<?xml version="1.0" ?>')
self._elt(b"llsd", lambda: self._generate(something))
self.stream.write(b'<?xml version="1.0" ?>'
b'<llsd>')
self._generate(something)
self.stream.write(b'</llsd>')


class LLSDXMLPrettyFormatter(LLSDXMLFormatter):
Expand Down
14 changes: 14 additions & 0 deletions tests/llsd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,20 @@ def testMap(self):
map_within_map_xml)
self.assertXMLRoundtrip({}, blank_map_xml)

def testDeepMap(self):
"""
Test that formatting a deeply nested map does not cause a RecursionError
"""

test_map = {"foo":"bar", "depth":0, "next":None}
max_depth = 200
for depth in range(max_depth):
test_map = {"foo":"bar", "depth":depth, "next":test_map}

# this should not throw an exception.
test_xml = self.llsd.as_xml(test_map)


def testBinary(self):
"""
Test the parse and serialization of input type : binary.
Expand Down