Skip to content

Commit

Permalink
Merge pull request #330 from jborean93/stderr-warning
Browse files Browse the repository at this point in the history
Raise warning rather than print
  • Loading branch information
jborean93 authored Aug 27, 2021
2 parents 0c62eb1 + 5cb6111 commit 9fe56fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 9fe56fd

Please sign in to comment.