def _getProtocol(self): getWarningMethod()( "Resolver.protocol is deprecated; use Resolver.queryUDP instead.", PendingDeprecationWarning, stacklevel=0) self.protocol = dns.DNSDatagramProtocol(self) return self.protocol
def _callProcessExited(self, reason): default = object() processExited = getattr(self.proto, 'processExited', default) if processExited is default: getWarningMethod()( _missingProcessExited % (qual(self.proto.__class__),), DeprecationWarning, stacklevel=0) else: processExited(Failure(reason))
def _callProcessExited(self, reason): default = object() processExited = getattr(self.proto, 'processExited', default) if processExited is default: getWarningMethod()(_missingProcessExited % (qual(self.proto.__class__), ), DeprecationWarning, stacklevel=0) else: processExited(Failure(reason))
def _callProcessExited(self, reason): default = object() processExited = getattr(self.proto, 'processExited', default) if processExited is default: getWarningMethod()( _missingProcessExited % (qual(self.proto.__class__),), DeprecationWarning, stacklevel=0) else: try: processExited(Failure(reason)) except BaseException: err(None, "unexpected error in processExited")
def test_callProcessExitedMissing(self): """ L{BaseProcess._callProcessExited} emits a L{DeprecationWarning} if the object referred to by its C{proto} attribute has no C{processExited} method. """ class FakeProto: pass reason = object() process = BaseProcess(FakeProto()) self.addCleanup(setWarningMethod, getWarningMethod()) warnings = [] def collect(message, category, stacklevel): warnings.append((message, category, stacklevel)) setWarningMethod(collect) process._callProcessExited(reason) [(message, category, stacklevel)] = warnings self.assertEqual( message, "Since Twisted 8.2, IProcessProtocol.processExited is required. " "%s.%s must implement it." % (FakeProto.__module__, FakeProto.__name__)) self.assertIs(category, DeprecationWarning) # The stacklevel doesn't really make sense for this kind of # deprecation. Requiring it to be 0 will at least avoid pointing to # any part of Twisted or a random part of the application's code, which # I think would be more misleading than having it point inside the # warning system itself. -exarkun self.assertEqual(stacklevel, 0)
def test_callProcessExitedMissing(self): """ L{BaseProcess._callProcessExited} emits a L{DeprecationWarning} if the object referred to by its C{proto} attribute has no C{processExited} method. """ class FakeProto: pass reason = object() process = BaseProcess(FakeProto()) self.addCleanup(setWarningMethod, getWarningMethod()) warnings = [] def collect(message, category, stacklevel): warnings.append((message, category, stacklevel)) setWarningMethod(collect) process._callProcessExited(reason) [(message, category, stacklevel)] = warnings self.assertEqual( message, "Since Twisted 8.2, IProcessProtocol.processExited is required. " "%s.%s must implement it." % ( FakeProto.__module__, FakeProto.__name__)) self.assertIdentical(category, DeprecationWarning) # The stacklevel doesn't really make sense for this kind of # deprecation. Requiring it to be 0 will at least avoid pointing to # any part of Twisted or a random part of the application's code, which # I think would be more misleading than having it point inside the # warning system itself. -exarkun self.assertEqual(stacklevel, 0)
def test_resolverProtocol(self): """ Reading L{client.Resolver.protocol} causes a deprecation warning to be emitted and evaluates to an instance of L{DNSDatagramProtocol}. """ resolver = client.Resolver(servers=[('example.com', 53)]) self.addCleanup(setWarningMethod, getWarningMethod()) warnings = [] setWarningMethod(lambda message, category, stacklevel: warnings.append( (message, category, stacklevel))) protocol = resolver.protocol self.assertIsInstance(protocol, dns.DNSDatagramProtocol) self.assertEqual( warnings, [("Resolver.protocol is deprecated; use " "Resolver.queryUDP instead.", PendingDeprecationWarning, 0)]) self.assertIdentical(protocol, resolver.protocol)
def _captureDeprecationWarnings(self, f, *args, **kwargs): """ Call C{f} and capture all deprecation warnings. """ warnings = [] def accumulateDeprecations(message, category, stacklevel): self.assertEqual(DeprecationWarning, category) self.assertEqual(stacklevel, 2) warnings.append(message) originalMethod = deprecate.getWarningMethod() deprecate.setWarningMethod(accumulateDeprecations) try: result = f(*args, **kwargs) finally: deprecate.setWarningMethod(originalMethod) return (warnings, result)
def _classDeprecation(self, cls, methodName): test = cls() result = reporter.TestResult() original = deprecate.getWarningMethod() self.addCleanup(deprecate.setWarningMethod, original) warnings = [] # XXX deprecate.collectWarnings()? Maybe with support for filtering. deprecate.setWarningMethod( lambda message, category, stacklevel: warnings.append( (message, category, stacklevel))) test(result) message, category, stacklevel = warnings[0] self.assertIdentical(category, DeprecationWarning) self.assertEqual( message, methodName + ", deprecated since Twisted 8.2.0, was overridden " "by " + cls.__module__ + "." + cls.__name__ + ". Use " + methodName.replace('Class', '') + " instead.")
def test_resolverProtocol(self): """ Reading L{client.Resolver.protocol} causes a deprecation warning to be emitted and evaluates to an instance of L{DNSDatagramProtocol}. """ resolver = client.Resolver(servers=[('example.com', 53)]) self.addCleanup(setWarningMethod, getWarningMethod()) warnings = [] setWarningMethod( lambda message, category, stacklevel: warnings.append((message, category, stacklevel))) protocol = resolver.protocol self.assertIsInstance(protocol, dns.DNSDatagramProtocol) self.assertEqual( warnings, [("Resolver.protocol is deprecated; use " "Resolver.queryUDP instead.", PendingDeprecationWarning, 0)]) self.assertIdentical(protocol, resolver.protocol)
def _classDeprecation(self, cls, methodName): test = cls() result = reporter.TestResult() original = deprecate.getWarningMethod() self.addCleanup(deprecate.setWarningMethod, original) warnings = [] # XXX deprecate.collectWarnings()? Maybe with support for filtering. deprecate.setWarningMethod( lambda message, category, stacklevel: warnings.append(( message, category, stacklevel))) test(result) message, category, stacklevel = warnings[0] self.assertIdentical(category, DeprecationWarning) self.assertEqual( message, methodName + ", deprecated since Twisted 8.2.0, was overridden " "by " + cls.__module__ + "." + cls.__name__ + ". Use " + methodName.replace('Class', '') + " instead.")
def setupWorkerTransition(): """Hook Twisted deprecation machinery to use custom warning class for Worker API deprecation warnings.""" default_warn_method = getWarningMethod() def custom_warn_method(message, category, stacklevel): if stacklevel is not None: stacklevel += 1 if _WORKER_WARNING_MARK in message: # Message contains our mark - it's Worker API Renaming warning, # issue it appropriately. message = message.replace(_WORKER_WARNING_MARK, "") warnings.warn(DeprecatedWorkerNameWarning(message), message, stacklevel) else: # Other's warning message default_warn_method(message, category, stacklevel) setWarningMethod(custom_warn_method)
def setupWorkerTransition(): """Hook Twisted deprecation machinery to use custom warning class for Worker API deprecation warnings.""" default_warn_method = getWarningMethod() def custom_warn_method(message, category, stacklevel): if stacklevel is not None: stacklevel += 1 if _WORKER_WARNING_MARK in message: # Message contains our mark - it's Worker API Renaming warning, # issue it appropriately. message = message.replace(_WORKER_WARNING_MARK, "") warnings.warn( DeprecatedWorkerNameWarning(message), message, stacklevel) else: # Other's warning message default_warn_method(message, category, stacklevel) setWarningMethod(custom_warn_method)