diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2019-06-21 15:19:41 -0400 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2019-06-21 15:19:53 -0400 |
commit | 7f5a6c3f6d15876b7a2a7b298de14d8d807efad2 (patch) | |
tree | 8a30a7f3fc1f0554d2b79501a344eaaf254ffc47 | |
parent | e780a781418c4cd6b6bed7ff3427393886d95d55 (diff) |
Enhancements to #1460
- Modern style string formatting please
- Make more sensible use of object attributes, where it can be done backwards incompatibly
- Gussy up the string output to my personal taste
- Add tests
-rw-r--r-- | paramiko/ssh_exception.py | 31 | ||||
-rw-r--r-- | tests/test_ssh_exception.py | 42 |
2 files changed, 61 insertions, 12 deletions
diff --git a/paramiko/ssh_exception.py b/paramiko/ssh_exception.py index fc71ae2a..77b9efda 100644 --- a/paramiko/ssh_exception.py +++ b/paramiko/ssh_exception.py @@ -58,14 +58,18 @@ class BadAuthenticationType(AuthenticationException): allowed_types = [] + # TODO 3.0: remove explanation kwarg def __init__(self, explanation, types): + # TODO 3.0: remove this supercall unless it's actually required for + # pickling (after fixing pickling) AuthenticationException.__init__(self, explanation, types) + self.explanation = explanation self.allowed_types = types def __str__(self): - msg = "Bad authentication type, allowed types: " - msg += ",".join(self.allowed_types) - return msg + return "{}; allowed types: {!r}".format( + self.explanation, self.allowed_types + ) class PartialAuthentication(AuthenticationException): @@ -80,9 +84,9 @@ class PartialAuthentication(AuthenticationException): self.allowed_types = types def __str__(self): - msg = "Partial authentication, allowed types: " - msg += ",".join(self.allowed_types) - return msg + return "Partial authentication; allowed types: {!r}".format( + self.allowed_types + ) class ChannelException(SSHException): @@ -97,9 +101,10 @@ class ChannelException(SSHException): def __init__(self, code, text): SSHException.__init__(self, code, text) self.code = code + self.text = text def __str__(self): - return "ChannelException: %s, %s" % self.args + return "ChannelException({!r}, {!r})".format(self.code, self.text) class BadHostKeyException(SSHException): @@ -120,9 +125,12 @@ class BadHostKeyException(SSHException): self.expected_key = expected_key def __str__(self): - return "Host key for server %s does not match: got %s, expected %s" % ( + msg = ( + "Host key for server {!r} does not match: got {!r}, expected {!r}" + ) # noqa + return msg.format( self.hostname, - self.got_key.get_base64(), + self.key.get_base64(), self.expected_key.get_base64(), ) @@ -137,11 +145,12 @@ class ProxyCommandFailure(SSHException): def __init__(self, command, error): SSHException.__init__(self, command, error) + self.command = command self.error = error def __str__(self): - return ( - "ProxyCommand (%s) returned non-zero exit status: %s" % self.args + return 'ProxyCommand("{}") returned nonzero exit status: {}'.format( + self.command, self.error ) diff --git a/tests/test_ssh_exception.py b/tests/test_ssh_exception.py index d9e0bd22..1628986a 100644 --- a/tests/test_ssh_exception.py +++ b/tests/test_ssh_exception.py @@ -1,7 +1,15 @@ import pickle import unittest -from paramiko.ssh_exception import NoValidConnectionsError +from paramiko import RSAKey +from paramiko.ssh_exception import ( + NoValidConnectionsError, + BadAuthenticationType, + PartialAuthentication, + ChannelException, + BadHostKeyException, + ProxyCommandFailure, +) class NoValidConnectionsErrorTest(unittest.TestCase): @@ -33,3 +41,35 @@ class NoValidConnectionsErrorTest(unittest.TestCase): ) exp = "Unable to connect to port 22 on 10.0.0.42, 127.0.0.1 or ::1" assert exp in str(exc) + + +class ExceptionStringDisplayTest(unittest.TestCase): + def test_BadAuthenticationType(self): + exc = BadAuthenticationType( + "Bad authentication type", ["ok", "also-ok"] + ) + expected = "Bad authentication type; allowed types: ['ok', 'also-ok']" + assert str(exc) == expected + + def test_PartialAuthentication(self): + exc = PartialAuthentication(["ok", "also-ok"]) + expected = "Partial authentication; allowed types: ['ok', 'also-ok']" + assert str(exc) == expected + + def test_BadHostKeyException(self): + got_key = RSAKey.generate(2048) + wanted_key = RSAKey.generate(2048) + exc = BadHostKeyException("myhost", got_key, wanted_key) + expected = "Host key for server 'myhost' does not match: got '{}', expected '{}'" # noqa + assert str(exc) == expected.format( + got_key.get_base64(), wanted_key.get_base64() + ) + + def test_ProxyCommandFailure(self): + exc = ProxyCommandFailure("man squid", 7) + expected = 'ProxyCommand("man squid") returned nonzero exit status: 7' + assert str(exc) == expected + + def test_ChannelException(self): + exc = ChannelException(17, "whatever") + assert str(exc) == "ChannelException(17, 'whatever')" |