Skip to content

Commit

Permalink
Raise warning rather than print
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 committed Aug 27, 2021
1 parent 0c62eb1 commit 5cb6111
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Version 0.4.3
- Fix invalid regex escape sequences.
- Decoding CLIXML failures for `run_ps` will create a `UserWarning` rather than printing the warning.

### Version 0.4.2
- Dropped Python 3.5 from support matrix as it is EOL.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

__version__ = '0.4.2'
__version__ = '0.4.3'
project_name = 'pywinrm'

# PyPi supports only reStructuredText, so pandoc should be installed
Expand Down
7 changes: 4 additions & 3 deletions winrm/__init__.py
Expand Up @@ -2,6 +2,7 @@
import re
from base64 import b64encode
import xml.etree.ElementTree as ET
import warnings

from winrm.protocol import Protocol

Expand Down Expand Up @@ -79,9 +80,9 @@ def _clean_error_msg(self, msg):
except Exception as e:
# if any of the above fails, the msg was not true xml
# print a warning and return the original string
# TODO do not print, raise user defined error instead
print("Warning: there was a problem converting the Powershell"
" error message: %s" % (e))
warnings.warn(
"There was a problem converting the Powershell error "
"message: %s" % (e))
else:
# if new_msg was populated, that's our error message
# otherwise the original error message will be used
Expand Down
12 changes: 12 additions & 0 deletions winrm/tests/test_session.py
@@ -1,3 +1,5 @@
import pytest

from winrm import Session


Expand Down Expand Up @@ -78,3 +80,13 @@ def test_decode_clixml_no_errors():
expected = msg
actual = s._clean_error_msg(msg)
assert actual == expected


def test_decode_clixml_invalid_xml():
s = Session('windows-host.example.com', auth=('john.smith', 'secret'))
msg = b'#< CLIXML\r\n<in >dasf<?dsfij>'

with pytest.warns(UserWarning, match="There was a problem converting the Powershell error message"):
actual = s._clean_error_msg(msg)

assert actual == msg

0 comments on commit 5cb6111

Please sign in to comment.