Enhance __repr__ method of Principal

`__repr__` now returns more descriptive string containing the actual principal
name while keeping the ability to reconstruct the object from it.

This makes principal names visible in debug logs, easing troubleshooting a
bit.

https://fedorahosted.org/freeipa/ticket/6505

Reviewed-By: Christian Heimes <cheimes@redhat.com>
This commit is contained in:
Martin Babinsky 2016-11-28 10:22:26 +01:00
parent 0e093f938d
commit 38cc40ddb5
2 changed files with 6 additions and 0 deletions

View File

@ -181,3 +181,7 @@ class Principal(object):
principal_string = u'@'.join([principal_string, realm]) principal_string = u'@'.join([principal_string, realm])
return principal_string return principal_string
def __repr__(self):
return "{0.__module__}.{0.__name__}('{1}')".format(
self.__class__, self)

View File

@ -82,6 +82,8 @@ def test_principals(valid_principal):
assert getattr(princ, name) == value assert getattr(princ, name) == value
assert unicode(princ) == principal_name assert unicode(princ) == principal_name
assert repr(princ) == "ipapython.kerberos.Principal('{}')".format(
principal_name)
def test_multiple_unescaped_ats_raise_error(): def test_multiple_unescaped_ats_raise_error():