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