From 5cb6111defc9ed36a0c0f50a2ecff214e8cfafaf Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 27 Aug 2021 10:16:26 +1000 Subject: [PATCH] Raise warning rather than print --- CHANGELOG.md | 1 + setup.py | 2 +- winrm/__init__.py | 7 ++++--- winrm/tests/test_session.py | 12 ++++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb2377e5..1488e946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/setup.py b/setup.py index 1189a89e..752bb385 100644 --- a/setup.py +++ b/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 diff --git a/winrm/__init__.py b/winrm/__init__.py index 5b247e2d..c3ee260a 100644 --- a/winrm/__init__.py +++ b/winrm/__init__.py @@ -2,6 +2,7 @@ import re from base64 import b64encode import xml.etree.ElementTree as ET +import warnings from winrm.protocol import Protocol @@ -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 diff --git a/winrm/tests/test_session.py b/winrm/tests/test_session.py index 8522b436..875d493f 100644 --- a/winrm/tests/test_session.py +++ b/winrm/tests/test_session.py @@ -1,3 +1,5 @@ +import pytest + from winrm import Session @@ -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\ndasf' + + with pytest.warns(UserWarning, match="There was a problem converting the Powershell error message"): + actual = s._clean_error_msg(msg) + + assert actual == msg