Skip to content

Commit e076757

Browse files
authored
Merge pull request #123 from TiagoMLucio/master
fix: XML pretty-printing crash for non-ASCII in httpops
2 parents 48610bf + 5085893 commit e076757

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

elmclient/httpops.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,13 @@ def _log_request( self, request, donotlogbody=False, intent=None, action=None ):
594594
if rawtext.startswith( '<?xml' ) or rawtext.startswith( '<rdf' ):
595595
# assume XML
596596
# print( f"is xml {rawtext[0:4]}" )
597-
tree = ET.fromstring( rawtext )
598-
ET.indent(tree, space=" " )
599-
rawtext = ET.tostring( tree )
597+
try:
598+
body_bytes = request.body if isinstance(request.body, bytes) else request.body.encode('utf-8')
599+
tree = ET.fromstring( body_bytes )
600+
ET.indent(tree, space=" " )
601+
rawtext = ET.tostring( tree, encoding='unicode' )
602+
except Exception:
603+
pass # keep rawtext as-is
600604
else:
601605
# print( f"not xml {rawtext[0:4]}" )
602606
pass
@@ -658,9 +662,12 @@ def _log_response( self, response, action=None ):
658662
if rawtext.startswith( '<?xml' ) or rawtext.startswith( '<rdf' ):
659663
# assume XML
660664
# print( f"is xml {rawtext[0:4]}" )
661-
tree = ET.fromstring( rawtext.encode() )
662-
ET.indent(tree, space=" " )
663-
rawtext = ET.tostring( tree ).decode()
665+
try:
666+
tree = ET.fromstring( response.content )
667+
ET.indent(tree, space=" " )
668+
rawtext = ET.tostring( tree, encoding='unicode' )
669+
except Exception:
670+
pass # keep rawtext as-is
664671
else:
665672
# print( f"not xml {rawtext[0:4]}" )
666673
pass

0 commit comments

Comments
 (0)