mopidy/mopidy/utils/deprecation.py
Stein Magnus Jodal 8f8fa4d414 core: Emit deprecation warnings
The warnings appear as warning level log messages if running Python on
the mopidy/ directory like this:

    python -W all mopidy -v

or:

    python -W all mopidy -o loglevels/py.warnings=warning

We don't suppress warnings when Pykka is the caller in general, but just
when Pykka is looking at all properties to create its actor proxies.

When a deprecated property is used from another Pykka actor, only the
stack for the current actor thread is available for inspection, so the
warning cannot show where the actual call site in the other actor thread
is. Though, if the warnings are made exceptions with:

    python -W error mopidy

then the stack traces will include the frames from all involved actor
threads, showing where the original call site is.
2015-02-11 00:29:52 +01:00

35 lines
956 B
Python

from __future__ import unicode_literals
import inspect
import warnings
def _is_pykka_proxy_creation():
stack = inspect.stack()
try:
calling_frame = stack[3]
except IndexError:
return False
else:
filename = calling_frame[1]
funcname = calling_frame[3]
return 'pykka' in filename and funcname == '_get_attributes'
def deprecated_property(
getter=None, setter=None, message='Property is deprecated'):
def deprecated_getter(*args):
if not _is_pykka_proxy_creation():
warnings.warn(message, DeprecationWarning, stacklevel=2)
return getter(*args)
def deprecated_setter(*args):
if not _is_pykka_proxy_creation():
warnings.warn(message, DeprecationWarning, stacklevel=2)
return setter(*args)
new_getter = getter and deprecated_getter
new_setter = setter and deprecated_setter
return property(new_getter, new_setter)