本文整理匯總了Python中warnings.warn_explicit方法的典型用法代碼示例。如果您正苦於以下問題:Python warnings.warn_explicit方法的具體用法?Python warnings.warn_explicit怎麽用?Python warnings.warn_explicit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類warnings
的用法示例。
在下文中一共展示了warnings.warn_explicit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: deprecated
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def deprecated(func):
"""This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used.
"""
@functools.wraps(func)
def new_func(*args, **kwargs):
warnings.simplefilter("always", DeprecationWarning) # turn off filter
warnings.warn_explicit(
"Call to deprecated function {}.".format(func.__name__),
category=DeprecationWarning,
filename=func.__code__.co_filename,
lineno=func.__code__.co_firstlineno + 1,
)
warnings.simplefilter("default", DeprecationWarning) # reset filter
logger.warn(
"%s - DeprecationWarning: Call to deprecated function %s. %s:%s"
% (datetime.datetime.now(), func.__name__, func.__code__.co_filename, func.__code__.co_firstlineno + 1)
)
return func(*args, **kwargs)
return new_func
示例2: __call__
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def __call__(self, func):
msg = "Call to deprecated function {}".format(func.__name__)
if self.use_instead:
msg += '; use ' + self.use_instead + ' instead'
def wrapper(*args, **kwargs):
fingerprint = (func.__name__, func.__code__.co_filename, func.__code__.co_firstlineno)
if fingerprint not in self.seen:
warnings.warn_explicit(
msg,
category=DeprecationWarning,
filename=func.__code__.co_filename,
lineno=func.__code__.co_firstlineno + 1)
self.seen.update([fingerprint])
return func(*args, **kwargs)
wrapper.__doc__ = "Deprecated"
if self.use_instead:
wrapper.__doc__ += '; use ' + self.use_instead + ' instead'
return wrapper
示例3: handle_old_config
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def handle_old_config(self, filename):
warnings.warn_explicit(self.old_warning, ConfigDeprecationWarning,
filename, 0)
options = self.get_section('options')
if not self.has_section('general'):
self.add_section('general')
for key, value in list(options.items()):
if key in self.old_settings:
section, setting = self.old_settings[key]
if not self.has_section(section):
self.add_section(section)
else:
section = 'general'
setting = key
if not self.has_option(section, setting):
self.set(section, setting, value)
self.remove_section('options')
示例4: handle_old_config
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def handle_old_config(self, filename):
warnings.warn_explicit(self.old_warning, ConfigDeprecationWarning,
filename, 0)
options = self.get_section('options')
if not self.has_section('general'):
self.add_section('general')
for key, value in options.items():
if key in self.old_settings:
section, setting = self.old_settings[key]
if not self.has_section(section):
self.add_section(section)
else:
section = 'general'
setting = key
if not self.has_option(section, setting):
self.set(section, setting, value)
self.remove_section('options')
示例5: _unjelly_instance
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def _unjelly_instance(self, rest):
"""
(internal) Unjelly an instance.
Called to handle the deprecated I{instance} token.
@param rest: The s-expression representing the instance.
@return: The unjellied instance.
"""
warnings.warn_explicit(
"Unjelly support for the instance atom is deprecated since "
"Twisted 15.0.0. Upgrade peer for modern instance support.",
category=DeprecationWarning, filename="", lineno=0)
clz = self.unjelly(rest[0])
if not _PY3 and type(clz) is not _OldStyleClass:
raise InsecureJelly("Legacy 'instance' found with new-style class")
return self._genericUnjelly(clz, rest[1])
示例6: show_param_warnings
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def show_param_warnings(app, exception):
import inspect
for (fname, fun), params in param_warnings.items():
_, line = inspect.getsourcelines(fun)
file_name = inspect.getsourcefile(fun)
params_str = '\n'.join(f'\t{n}: {t}' for n, t in params)
warnings.warn_explicit(
f'\nParameters in `{fname}` have types in docstring.\n'
f'Replace them with type annotations.\n{params_str}',
UserWarning,
file_name,
line,
)
if param_warnings:
raise RuntimeError('Encountered text parameter type. Use annotations.')
示例7: deprecated
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def deprecated(version, version_removed):
'''This is a decorator which can be used to mark functions
as deprecated.
It will result in a warning being emitted when the function is used.'''
def __wrapper(func, *args, **kwargs):
'''Warn the user, and then proceed.'''
code = six.get_function_code(func)
warnings.warn_explicit(
"{:s}.{:s}\n\tDeprecated as of JAMS version {:s}."
"\n\tIt will be removed in JAMS version {:s}."
.format(func.__module__, func.__name__,
version, version_removed),
category=DeprecationWarning,
filename=code.co_filename,
lineno=code.co_firstlineno + 1
)
return func(*args, **kwargs)
return decorator(__wrapper)
示例8: handle_error
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def handle_error(self, wrapper, exception, traceback_):
if hasattr(wrapper, "gccxml_definition"):
definition = wrapper.gccxml_definition
elif hasattr(wrapper, "main_wrapper"):
try:
definition = wrapper.main_wrapper.gccxml_definition
except AttributeError:
definition = None
else:
definition = None
if definition is None:
print("exception %r in wrapper %s" % (exception, wrapper), file=sys.stderr)
else:
warnings.warn_explicit("exception %r in wrapper for %s"
% (exception, definition),
WrapperWarning, definition.location.file_name,
definition.location.line)
return True
示例9: _get_calldef_exceptions
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def _get_calldef_exceptions(self, calldef):
retval = []
for decl in calldef.exceptions:
traits = ctypeparser.TypeTraits(normalize_name(decl.partial_decl_string))
if traits.type_is_reference:
name = str(traits.target)
else:
name = str(traits.ctype)
exc = self.type_registry.root_module.get(name, None)
if isinstance(exc, CppException):
retval.append(exc)
else:
warnings.warn_explicit("Thrown exception '%s' was not previously detected as an exception class."
" PyBindGen bug?"
% (normalize_name(decl.partial_decl_string)),
WrapperWarning, calldef.location.file_name, calldef.location.line)
return retval
示例10: test_once
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def test_once(self):
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("once", category=UserWarning)
message = UserWarning("FilterTests.test_once")
self.module.warn_explicit(message, UserWarning, "__init__.py",
42)
self.assertEqual(w[-1].message, message)
del w[:]
self.module.warn_explicit(message, UserWarning, "__init__.py",
13)
self.assertEqual(len(w), 0)
self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
42)
self.assertEqual(len(w), 0)
示例11: deprecated
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def deprecated(func):
'''This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used.'''
@functools.wraps(func)
def new_func(*args, **kwargs):
warnings.warn_explicit(
"Call to deprecated function {}.".format(func.__name__),
category=DeprecationWarning,
filename=func.func_code.co_filename,
lineno=func.func_code.co_firstlineno + 1
)
return func(*args, **kwargs)
return new_func
示例12: test_once
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def test_once(self):
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.resetwarnings()
self.module.filterwarnings("once", category=UserWarning)
message = UserWarning("FilterTests.test_once")
self.module.warn_explicit(message, UserWarning, "test_warnings.py",
42)
self.assertEqual(w[-1].message, message)
del w[:]
self.module.warn_explicit(message, UserWarning, "test_warnings.py",
13)
self.assertEqual(len(w), 0)
self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
42)
self.assertEqual(len(w), 0)
示例13: deprecated
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def deprecated(alternative=None):
"""This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used."""
def real_decorator(func):
@functools.wraps(func)
def new_func(*args, **kwargs):
msg = "Call to deprecated function {0}.".format(func.__name__)
if alternative:
msg += " Use {0} instead".format(alternative)
warnings.warn_explicit(
msg,
category=DeprecationWarning,
filename=compat.get_function_code(func).co_filename,
lineno=compat.get_function_code(func).co_firstlineno + 1,
)
return func(*args, **kwargs)
return new_func
return real_decorator
示例14: warn
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def warn(msg, stacklevel=3):
"""Issue a warning.
If msg is a string, :class:`.exc.SAWarning` is used as
the category.
.. note::
This function is swapped out when the test suite
runs, with a compatible version that uses
warnings.warn_explicit, so that the warnings registry can
be controlled.
"""
if isinstance(msg, compat.string_types):
warnings.warn(msg, exc.SAWarning, stacklevel=stacklevel)
else:
warnings.warn(msg, stacklevel=stacklevel)
示例15: deprecated
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import warn_explicit [as 別名]
def deprecated(func):
'''This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used.'''
@functools.wraps(func)
def new_func(*args, **kwargs):
warnings.warn_explicit(
"Call to deprecated function {}.".format(func.__name__),
category=DeprecationWarning,
filename=func.func_code.co_filename,
lineno=func.func_code.co_firstlineno + 1
)
return func(*args, **kwargs)
return new_func