Skip to content
Snippets Groups Projects
Commit b6a011cc authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

recast and silencers added

parent c81c3c3d
No related branches found
No related tags found
No related merge requests found
## v2.0.14
* Dodano `rethrow_as`
## v2.0.13
* Python 3.4 znów wspierany
......
......@@ -9,10 +9,13 @@ from .monitor import Monitor, RMonitor
from .structures import TimeBasedHeap, CallableGroup, Heap
from .typecheck import typed, List, Tuple, Dict, NewType, Callable, Sequence, \
TypeVar, Generic, Mapping, Iterable, Union, Any, Optional, CallSignature, Number
from .recast_exceptions import rethrow_as, silence_excs
__all__ = [
'TimeBasedHeap', 'Heap', 'CallableGroup',
'Monitor', 'RMonitor', 'merge_dicts',
'typed', 'List', 'Tuple', 'Dict', 'NewType', 'Callable', 'Sequence',
'TypeVar', 'Generic', 'Mapping', 'Iterable', 'Union', 'Any', 'Optional', 'CallSignature', 'Number'
'TypeVar', 'Generic', 'Mapping', 'Iterable', 'Union', 'Any', 'Optional', 'CallSignature', 'Number',
'rethrow_as', 'silence_excs'
]
# coding=UTF-8
from __future__ import print_function, absolute_import, division
import six
import logging
import functools
logger = logging.getLogger(__name__)
__all__ = [
'silence',
'rethrow_as'
]
def silence_excs(*exc_types):
return rethrow_as(*[(t, None) for t in exc_types])
class rethrow_as(object):
"""Decorator + context manager"""
def __init__(self, *pairs, exception_preprocessor=repr):
self.to_catch = tuple(p[0] for p in pairs)
self.pairs = pairs
self.exception_preprocessor = exception_preprocessor
def __call__(self, fun):
@functools.wraps(fun)
def inner(*args, **kwargs):
with self:
return fun(*args, **kwargs)
return inner
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if not isinstance(exc_val, self.to_catch):
return
for from_, to in self.pairs:
if isinstance(exc_val, from_):
if to is None:
return True
else:
raise to(self.exception_preprocessor(exc_val))
[metadata]
name = satella
version = 2.0.13
version = 2.0.14rc1
description-file = README.md
author = Piotr Maślanka
author_email = piotrm@smok.co
......
# coding=UTF-8
from __future__ import print_function, absolute_import, division
import six
import unittest
from satella.coding import rethrow_as, silence_excs
class TestStuff(unittest.TestCase):
def test_silencer(self):
with silence_excs(TypeError):
raise TypeError()
def test_rethrow(self):
try:
with rethrow_as((ValueError, NameError)):
raise ValueError()
except NameError:
return
self.fail()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment