stats: use try/catch for contextmanager

This commit is contained in:
Sean Breckenridge 2022-05-01 15:47:44 -07:00
parent 301ce5f35e
commit bba07d8265
2 changed files with 10 additions and 7 deletions

View file

@ -219,7 +219,7 @@ def modules_check(*, verbose: bool, list_all: bool, quick: bool, for_modules: Li
import contextlib import contextlib
from .common import with_quick_stats from .common import quick_stats
from .util import get_stats, HPIModule from .util import get_stats, HPIModule
from .stats import guess_stats from .stats import guess_stats
from .error import warn_my_config_import_error from .error import warn_my_config_import_error
@ -263,7 +263,7 @@ def modules_check(*, verbose: bool, list_all: bool, quick: bool, for_modules: Li
# todo point to a readme on the module structure or something? # todo point to a readme on the module structure or something?
continue continue
quick_context = with_quick_stats() if quick else contextlib.nullcontext() quick_context = quick_stats() if quick else contextlib.nullcontext()
try: try:
kwargs = {} kwargs = {}

View file

@ -427,7 +427,7 @@ def warn_if_empty(f):
# global state that turns on/off quick stats # global state that turns on/off quick stats
# can use the 'with_quick_stats' contextmanager # can use the 'quick_stats' contextmanager
# to enable/disable this in cli so that module 'stats' # to enable/disable this in cli so that module 'stats'
# functions don't have to implement custom 'quick' logic # functions don't have to implement custom 'quick' logic
QUICK_STATS = False QUICK_STATS = False
@ -437,11 +437,14 @@ QUICK_STATS = False
# elsewhere -- can use this decorator instead of editing # elsewhere -- can use this decorator instead of editing
# the global state directly # the global state directly
@contextmanager @contextmanager
def with_quick_stats(): def quick_stats():
global QUICK_STATS global QUICK_STATS
prev, QUICK_STATS = QUICK_STATS, True prev = QUICK_STATS
yield try:
QUICK_STATS = prev QUICK_STATS = True
yield
finally:
QUICK_STATS = prev
C = TypeVar('C') C = TypeVar('C')