Skip to content
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

Exception on a valid XML with non-ASCII character #333

Open
Ferenc- opened this issue Sep 29, 2021 · 0 comments
Open

Exception on a valid XML with non-ASCII character #333

Ferenc- opened this issue Sep 29, 2021 · 0 comments

Comments

@Ferenc-
Copy link

Ferenc- commented Sep 29, 2021

Run the example PS script on a remote machine that is not set to English, but a non-ASCII language. Like in this case German:

Warning: there was a problem converting the Powershell error message: not well-formed (invalid token): line 1, column 205
b'#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Module werden f\x81r erstmalige Verwendung vorbereitet.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>'

The XML part of the message is a valid XML. Despite what the comment here assumes:

            except Exception as e:
                # if any of the above fails, the msg was not true xml

This seems like a false assumption, just like that we can call fromstring like this:

root = ET.fromstring(msg_xml)

when msg_xml is not a string, but an undecoded byte array, that ElementTree can't always deal with.

My suggestion is to decode it into a string first. In this case CP 437 definitely works, and I have seen that the pywinrm codebase
uses CP 437 as a default codepage and also for encoding input, so I would risk going with that by default:

string_xml = msg_xml.decode('437')
root = ET.fromstring(string_xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant