Skip to content

Commit

Permalink
pkey: fix comparison with None
Browse files Browse the repository at this point in the history
A bug introduced by 54da670 (added in v2.6.4)
  • Loading branch information
ploxiln committed Jan 4, 2022
1 parent 70b9047 commit 8562683
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions paramiko/pkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def __str__(self):

def __cmp__(self, other):
# python-2 only, same purpose as __eq__()
return cmp(self.asbytes(), other.asbytes()) # noqa
other_asbytes = other.asbytes() if isinstance(other, PKey) else b""
return cmp(self.asbytes(), other_asbytes) # noqa

def __eq__(self, other):
"""
Expand All @@ -125,7 +126,7 @@ def __eq__(self, other):
:param .PKey other: key to compare to.
"""
return self.asbytes() == other.asbytes()
return isinstance(other, PKey) and self.asbytes() == other.asbytes()

def get_name(self):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def test_2_load_rsa(self):
key2 = RSAKey.from_private_key(s)
self.assertEqual(key, key2)

# ensure comparison does not raise exception
assert key != None # noqa: E711

def test_3_load_rsa_password(self):
key = RSAKey.from_private_key_file(_support('test_rsa_password.key'), 'television')
self.assertEqual('ssh-rsa', key.get_name())
Expand All @@ -186,6 +189,9 @@ def test_4_load_dss(self):
key2 = DSSKey.from_private_key(s)
self.assertEqual(key, key2)

# ensure comparison does not raise exception
assert key != None # noqa: E711

def test_5_load_dss_password(self):
key = DSSKey.from_private_key_file(_support('test_dss_password.key'), 'television')
self.assertEqual('ssh-dss', key.get_name())
Expand Down Expand Up @@ -298,6 +304,9 @@ def test_10_load_ecdsa_256(self):
key2 = ECDSAKey.from_private_key(s)
self.assertEqual(key, key2)

# ensure comparison does not raise exception
assert key != None # noqa: E711

def test_11_load_ecdsa_password_256(self):
key = ECDSAKey.from_private_key_file(_support('test_ecdsa_password_256.key'),
b'television')
Expand Down Expand Up @@ -501,6 +510,9 @@ def test_ed25519(self):
)
self.assertNotEqual(key1.asbytes(), key2.asbytes())

# ensure comparison does not raise exception
assert key1 != None # noqa: E711

@pytest.mark.skipif("not Ed25519Key.is_supported()")
def test_ed25519_compare(self):
# verify that the private & public keys compare equal
Expand Down

0 comments on commit 8562683

Please sign in to comment.