diff options
| author | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-02 00:49:57 +0200 |
|---|---|---|
| committer | Kostis Anagnostopoulos <ankostis@gmail.com> | 2016-10-24 15:17:00 +0200 |
| commit | 2ec8c99e52290b88a6816df7cef0eff8df81c0b7 (patch) | |
| tree | 17e47874d16d9de8cf5edf7e6dad8809821a0a5f /gitdb/util.py | |
| parent | 941b6c7eff72de618bb34eeb5983ed2795988a32 (diff) | |
| download | gitdb-2ec8c99e52290b88a6816df7cef0eff8df81c0b7.tar.gz | |
feat(io): Retrofit streams as context-managers.
+ feat(util): add logger.
+ feat(util): add suppress-ex context-handler (from PY3 sources).
Diffstat (limited to 'gitdb/util.py')
| -rw-r--r-- | gitdb/util.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/gitdb/util.py b/gitdb/util.py index 242be44..821fc9b 100644 --- a/gitdb/util.py +++ b/gitdb/util.py @@ -15,6 +15,7 @@ from smmap import ( SlidingWindowMapManager, SlidingWindowMapBuffer ) +import logging # initialize our global memory manager instance # Use it to free cached (and unused) resources. @@ -75,6 +76,8 @@ from gitdb.const import ( #} END Aliases +log = logging.getLogger(__name__) + #{ compatibility stuff ... @@ -218,6 +221,40 @@ def to_bin_sha(sha): #{ Utilities +## Copied from python std-lib. +class suppress: + """Context manager to suppress specified exceptions + + After the exception is suppressed, execution proceeds with the next + statement following the with statement. + + with suppress(FileNotFoundError): + os.remove(somefile) + # Execution still resumes here if the file was already removed + """ + + def __init__(self, *exceptions): + self._exceptions = exceptions + + def __enter__(self): + pass + + def __exit__(self, exctype, excinst, exctb): + # Unlike isinstance and issubclass, CPython exception handling + # currently only looks at the concrete type hierarchy (ignoring + # the instance and subclass checking hooks). While Guido considers + # that a bug rather than a feature, it's a fairly hard one to fix + # due to various internal implementation details. suppress provides + # the simpler issubclass based semantics, rather than trying to + # exactly reproduce the limitations of the CPython interpreter. + # + # See http://bugs.python.org/issue12029 for more details + supp = exctype is not None and issubclass(exctype, self._exceptions) + if supp: + log.debug("Suppressed exception: %s(%s)", exctype, excinst, exc_info=1) + return supp + + class LazyMixin(object): """ |
