From 8ae5ec7f3774d81a1c3d79c2a05649de65b8658c Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 15 Jul 2015 10:42:31 +0200 Subject: Simplify exception handling in pkihelper Several methods except OSError before they except shutil.Error. In Python 3 the second except clause will be ignored because in Python 3 shutil.Error is a subclass of OSError. The body of the except clauses only differs in the logging message. A single except clause with an isinstance() check has the same effect. --- .../python/pki/server/deployment/pkihelper.py | 56 ++++++++++------------ 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py index b6ee61b27..cf04e68bc 100644 --- a/base/server/python/pki/server/deployment/pkihelper.py +++ b/base/server/python/pki/server/deployment/pkihelper.py @@ -1460,14 +1460,12 @@ class Directory: dir_perms, file_perms, symlink_perms, dir_acls, file_acls, symlink_acls, recursive_flag, critical_failure) - except OSError as exc: - config.pki_log.error(log.PKI_OSERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) - if critical_failure: - raise - except shutil.Error as exc: - config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) + except (shutil.Error, OSError) as exc: + if isinstance(exc, shutil.Error): + msg = log.PKI_SHUTIL_ERROR_1 + else: + msg = log.PKI_OSERROR_1 + config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2) if critical_failure: raise return @@ -1665,14 +1663,12 @@ class File: record.permissions = perms record.acls = acls self.manifest_db.append(record) - except OSError as exc: - config.pki_log.error(log.PKI_OSERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) - if critical_failure: - raise - except shutil.Error as exc: - config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) + except (shutil.Error, OSError) as exc: + if isinstance(exc, shutil.Error): + msg = log.PKI_SHUTIL_ERROR_1 + else: + msg = log.PKI_OSERROR_1 + config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2) if critical_failure: raise return @@ -1725,14 +1721,12 @@ class File: record.permissions = perms record.acls = acls self.manifest_db.append(record) - except OSError as exc: - config.pki_log.error(log.PKI_OSERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) - if critical_failure: - raise - except shutil.Error as exc: - config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) + except (shutil.Error, OSError) as exc: + if isinstance(exc, shutil.Error): + msg = log.PKI_SHUTIL_ERROR_1 + else: + msg = log.PKI_OSERROR_1 + config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2) if critical_failure: raise return @@ -1797,14 +1791,12 @@ class File: record.permissions = perms record.acls = acls self.manifest_db.append(record) - except OSError as exc: - config.pki_log.error(log.PKI_OSERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) - if critical_failure: - raise - except shutil.Error as exc: - config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc, - extra=config.PKI_INDENTATION_LEVEL_2) + except (shutil.Error, OSError) as exc: + if isinstance(exc, shutil.Error): + msg = log.PKI_SHUTIL_ERROR_1 + else: + msg = log.PKI_OSERROR_1 + config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2) if critical_failure: raise return -- cgit