1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
"""
Decorator to mark functions and methods as recordable.
"""
from .package_globals import get_recorder
# Guard to ensure that only the outermost recordable call is recorded
# and nested calls ignored.
_outermost_call = True
def recordable(func):
"""A decorator that wraps a function into one that is recordable.
This will record the function only if the global recorder has been
set via a `set_recorder` function call.
"""
def _wrapper(*args, **kw):
"""A wrapper returned to replace the decorated function."""
global _outermost_call
# Boolean to specify if the method was recorded or not.
record = False
if _outermost_call:
# Get the recorder.
rec = get_recorder()
if rec is not None:
_outermost_call = False
# Record the method if recorder is available.
record = True
try:
result = rec.record_function(func, args, kw)
finally:
_outermost_call = True
if not record:
# If the method was not recorded, just call it.
result = func(*args, **kw)
return result
# Mimic the actual function.
_wrapper.__name__ = func.__name__
_wrapper.__doc__ = func.__doc__
_wrapper.__dict__.update(func.__dict__)
return _wrapper
|