reflect cachew changes of exception handling and temporary suppression
This commit is contained in:
parent
d3f2551560
commit
ced93e6942
12 changed files with 29 additions and 46 deletions
|
@ -1,40 +1,29 @@
|
|||
# TODO this probably belongs to cachew? or cachew.experimental
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def disable_cachew():
|
||||
'''
|
||||
NOTE: you need to use it before importing any function using @cachew.cachew
|
||||
'''
|
||||
# TODO not sure... maybe it should instead use some hook.. it's a ibt ugly do
|
||||
try:
|
||||
import cachew
|
||||
except ImportError:
|
||||
# nothing to disable
|
||||
return
|
||||
|
||||
@cachew.doublewrap
|
||||
def cachew_off(func=None, *args, **kwargs):
|
||||
return func
|
||||
old = cachew.cachew
|
||||
cachew.cachew = cachew_off
|
||||
return old
|
||||
from cachew import settings
|
||||
settings.ENABLE = False
|
||||
|
||||
|
||||
@contextmanager
|
||||
def disabled_cachew():
|
||||
try:
|
||||
import cachew
|
||||
except ModuleNotFoundError:
|
||||
# no need to disable anything
|
||||
except ImportError:
|
||||
# nothing to disable
|
||||
yield
|
||||
return
|
||||
old = disable_cachew()
|
||||
try:
|
||||
from cachew.extra import disabled_cachew
|
||||
with disabled_cachew():
|
||||
yield
|
||||
finally:
|
||||
cachew.cachew = old
|
||||
|
||||
|
||||
def cache_dir() -> Path:
|
||||
|
|
|
@ -202,7 +202,7 @@ if TYPE_CHECKING:
|
|||
|
||||
mcachew: McachewType
|
||||
|
||||
# TODO ugh. I think it needs doublewrap, otherwise @mcachew without args doesn't work
|
||||
# todo ugh. I think it needs doublewrap, otherwise @mcachew without args doesn't work
|
||||
def mcachew(*args, **kwargs): # type: ignore[no-redef]
|
||||
"""
|
||||
Stands for 'Maybe cachew'.
|
||||
|
@ -214,8 +214,6 @@ def mcachew(*args, **kwargs): # type: ignore[no-redef]
|
|||
warnings.warn('cachew library not found. You might want to install it to speed things up. See https://github.com/karlicoss/cachew')
|
||||
return lambda orig_func: orig_func
|
||||
else:
|
||||
import cachew.experimental
|
||||
cachew.experimental.enable_exceptions() # TODO do it only once?
|
||||
return cachew.cachew(*args, **kwargs)
|
||||
|
||||
|
||||
|
|
|
@ -105,18 +105,20 @@ def test_sort_res_by() -> None:
|
|||
# todo proper typevar?
|
||||
from datetime import datetime
|
||||
def set_error_datetime(e: Exception, dt: datetime) -> None:
|
||||
# at the moment, we're using isoformat() instead of datetime directly to make it cachew-friendly
|
||||
# once cachew preserves exception argument types, we can remove these hacks
|
||||
e.args = e.args + (dt.isoformat(), )
|
||||
e.args = e.args + (dt,)
|
||||
# todo not sure if should return new exception?
|
||||
|
||||
|
||||
# todo it might be problematic because might mess with timezones (when it's converted to string, it's converted to a shift)
|
||||
def extract_error_datetime(e: Exception) -> Optional[datetime]:
|
||||
from .common import fromisoformat
|
||||
import re
|
||||
# TODO FIXME meh. definitely need to preserve exception args types in cachew if possible..
|
||||
for x in reversed(e.args):
|
||||
m = re.search(r'\d{4}-\d\d-\d\d(T..:..:..)?(\.\d{6})?(\+.....)?', x)
|
||||
if isinstance(x, datetime):
|
||||
return x
|
||||
if not isinstance(x, str):
|
||||
continue
|
||||
m = re.search(r'\d{4}-\d\d-\d\d(...:..:..)?(\.\d{6})?(\+.....)?', x)
|
||||
if m is None:
|
||||
continue
|
||||
ss = m.group(0)
|
||||
|
@ -134,8 +136,8 @@ def test_datetime_errors() -> None:
|
|||
assert extract_error_datetime(e1) is None
|
||||
set_error_datetime(e1, dt=dt)
|
||||
assert extract_error_datetime(e1) == dt
|
||||
# test that cachew can handle it...
|
||||
e2 = RuntimeError(str(e1.args))
|
||||
|
||||
e2 = RuntimeError(f'something something {dt} something else')
|
||||
assert extract_error_datetime(e2) == dt
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue