580
579
self.fail("%r is an instance of %s rather than %s" % (
581
580
obj, obj.__class__, kls))
583
def callDeprecated(self, expected, callable, *args, **kwargs):
584
"""Assert that a callable is deprecated in a particular way.
582
def _capture_warnings(self, a_callable, *args, **kwargs):
583
"""A helper for callDeprecated and applyDeprecated.
586
:param expected: a list of the deprecation warnings expected, in order
587
:param callable: The callable to call
585
:param a_callable: A callable to call.
588
586
:param args: The positional arguments for the callable
589
587
:param kwargs: The keyword arguments for the callable
588
:return: A tuple (warnings, result). result is the result of calling
589
a_callable(*args, **kwargs).
591
591
local_warnings = []
592
592
def capture_warnings(msg, cls, stacklevel=None):
593
# we've hooked into a deprecation specific callpath,
594
# only deprecations should getting sent via it.
593
595
self.assertEqual(cls, DeprecationWarning)
594
596
local_warnings.append(msg)
595
method = symbol_versioning.warn
597
original_warning_method = symbol_versioning.warn
596
598
symbol_versioning.set_warning_method(capture_warnings)
598
result = callable(*args, **kwargs)
600
result = a_callable(*args, **kwargs)
600
symbol_versioning.set_warning_method(method)
601
self.assertEqual(expected, local_warnings)
602
symbol_versioning.set_warning_method(original_warning_method)
603
return (local_warnings, result)
605
def applyDeprecated(self, deprecation_format, a_callable, *args, **kwargs):
606
"""Call a deprecated callable without warning the user.
608
:param deprecation_format: The deprecation format that the callable
609
should have been deprecated with. This is the same type as the
610
parameter to deprecated_method/deprecated_function. If the
611
callable is not deprecated with this format, an assertion error
613
:param a_callable: A callable to call. This may be a bound method or
614
a regular function. It will be called with *args and **kwargs.
615
:param args: The positional arguments for the callable
616
:param kwargs: The keyword arguments for the callable
617
:return: The result of a_callable(*args, **kwargs)
619
call_warnings, result = self._capture_warnings(a_callable,
621
expected_first_warning = symbol_versioning.deprecation_string(
622
a_callable, deprecation_format)
623
if len(call_warnings) == 0:
624
self.fail("No assertion generated by call to %s" %
626
self.assertEqual(expected_first_warning, call_warnings[0])
629
def callDeprecated(self, expected, callable, *args, **kwargs):
630
"""Assert that a callable is deprecated in a particular way.
632
This is a very precise test for unusual requirements. The
633
applyDeprecated helper function is probably more suited for most tests
634
as it allows you to simply specify the deprecation format being used
635
and will ensure that that is issued for the function being called.
637
:param expected: a list of the deprecation warnings expected, in order
638
:param callable: The callable to call
639
:param args: The positional arguments for the callable
640
:param kwargs: The keyword arguments for the callable
642
call_warnings, result = self._capture_warnings(callable,
644
self.assertEqual(expected, call_warnings)
604
647
def _startLogFile(self):