示例#1
0
文件: tcp.py 项目: relaunched/twisted
 def _dataReceived(self, data):
     if not data:
         return main.CONNECTION_DONE
     rval = self.protocol.dataReceived(data)
     if rval is not None:
         offender = self.protocol.dataReceived
         warningFormat = "Returning a value other than None from %(fqpn)s is " "deprecated since %(version)s."
         warningString = deprecate.getDeprecationWarningString(
             offender, versions.Version("Twisted", 11, 0, 0), format=warningFormat
         )
         deprecate.warnAboutFunction(offender, warningString)
     return rval
示例#2
0
 def _dataReceived(self, data):
     if not data:
         return main.CONNECTION_DONE
     rval = self.protocol.dataReceived(data)
     if rval is not None:
         offender = self.protocol.dataReceived
         warningFormat = (
             'Returning a value other than None from %(fqpn)s is '
             'deprecated since %(version)s.')
         warningString = deprecate.getDeprecationWarningString(
             offender, versions.Version('Twisted', 11, 0, 0),
             format=warningFormat)
         deprecate.warnAboutFunction(offender, warningString)
     return rval
示例#3
0
    def _getSkipReason(self, method, skip):
        """
        Return the reason to use for skipping a test method.

        @param method: The method which produced the skip.
        @param skip: A L{SkipTest} instance raised by C{method}.
        """
        if len(skip.args) > 0:
            return skip.args[0]

        warnAboutFunction(
            method, "Do not raise unittest.SkipTest with no arguments! Give a reason " "for skipping tests!"
        )
        return skip
示例#4
0
    def _getSkipReason(self, method, skip):
        """
        Return the reason to use for skipping a test method.

        @param method: The method which produced the skip.
        @param skip: A L{unittest.SkipTest} instance raised by C{method}.
        """
        if len(skip.args) > 0:
            return skip.args[0]

        warnAboutFunction(
            method,
            "Do not raise unittest.SkipTest with no arguments! Give a reason "
            "for skipping tests!")
        return skip
 def test_warning(self):
     """
     L{deprecate.warnAboutFunction} emits a warning the file and line number
     of which point to the beginning of the implementation of the function
     passed to it.
     """
     def aFunc():
         pass
     deprecate.warnAboutFunction(aFunc, 'A Warning Message')
     warningsShown = self.flushWarnings()
     filename = __file__
     if filename.lower().endswith('.pyc'):
         filename = filename[:-1]
     self.assertSamePath(
         FilePath(warningsShown[0]["filename"]), FilePath(filename))
     self.assertEqual(warningsShown[0]["message"], "A Warning Message")
示例#6
0
 def test_warning(self):
     """
     L{deprecate.warnAboutFunction} emits a warning the file and line number
     of which point to the beginning of the implementation of the function
     passed to it.
     """
     def aFunc():
         pass
     deprecate.warnAboutFunction(aFunc, 'A Warning Message')
     warningsShown = self.flushWarnings()
     filename = __file__
     if filename.lower().endswith('.pyc'):
         filename = filename[:-1]
     self.assertSamePath(
         FilePath(warningsShown[0]["filename"]), FilePath(filename))
     self.assertEqual(warningsShown[0]["message"], "A Warning Message")
示例#7
0
class Connection(_TLSConnectionMixin, abstract.FileDescriptor, _SocketCloser,
                 _AbortingMixin):
    """
    Superclass of all socket-based FileDescriptors.

    This is an abstract superclass of all objects which represent a TCP/IP
    connection based socket.

    @ivar logstr: prefix used when logging events related to this connection.
    @type logstr: C{str}
    """
    implements(interfaces.ITCPTransport, interfaces.ISystemHandle)

    def __init__(self, skt, protocol, reactor=None):
        abstract.FileDescriptor.__init__(self, reactor=reactor)
        self.socket = skt
        self.socket.setblocking(0)
        self.fileno = skt.fileno
        self.protocol = protocol

    def getHandle(self):
        """Return the socket for this connection."""
        return self.socket

    def doRead(self):
        """Calls self.protocol.dataReceived with all available data.

        This reads up to self.bufferSize bytes of data from its socket, then
        calls self.dataReceived(data) to process it.  If the connection is not
        lost through an error in the physical recv(), this function will return
        the result of the dataReceived call.
        """
        try:
            data = self.socket.recv(self.bufferSize)
        except socket.error, se:
            if se.args[0] == EWOULDBLOCK:
                return
            else:
                return main.CONNECTION_LOST
        if not data:
            return main.CONNECTION_DONE
        rval = self.protocol.dataReceived(data)
        if rval is not None:
            offender = self.protocol.dataReceived
            warningFormat = (
                'Returning a value other than None from %(fqpn)s is '
                'deprecated since %(version)s.')
            warningString = deprecate.getDeprecationWarningString(
                offender,
                versions.Version('Twisted', 11, 0, 0),
                format=warningFormat)
            deprecate.warnAboutFunction(offender, warningString)
        return rval
示例#8
0
class Connection(abstract.FileDescriptor, _SocketCloser):
    """
    Superclass of all socket-based FileDescriptors.

    This is an abstract superclass of all objects which represent a TCP/IP
    connection based socket.

    @ivar logstr: prefix used when logging events related to this connection.
    @type logstr: C{str}
    """

    implements(interfaces.ITCPTransport, interfaces.ISystemHandle)

    TLS = 0

    def __init__(self, skt, protocol, reactor=None):
        abstract.FileDescriptor.__init__(self, reactor=reactor)
        self.socket = skt
        self.socket.setblocking(0)
        self.fileno = skt.fileno
        self.protocol = protocol

    if SSL:
        _tlsWaiting = None
        def startTLS(self, ctx, extra):
            assert not self.TLS
            if self.dataBuffer or self._tempDataBuffer:
                # pre-TLS bytes are still being written.  Starting TLS now
                # will do the wrong thing.  Instead, mark that we're trying
                # to go into the TLS state.
                self._tlsWaiting = _TLSDelayed([], ctx, extra)
                return False

            self.stopReading()
            self.stopWriting()
            self._startTLS()
            self.socket = SSL.Connection(ctx.getContext(), self.socket)
            self.fileno = self.socket.fileno
            self.startReading()
            return True


        def _startTLS(self):
            self.TLS = 1
            self.__class__ = _getTLSClass(self.__class__)


        def write(self, bytes):
            if self._tlsWaiting is not None:
                self._tlsWaiting.bufferedData.append(bytes)
            else:
                abstract.FileDescriptor.write(self, bytes)


        def writeSequence(self, iovec):
            if self._tlsWaiting is not None:
                self._tlsWaiting.bufferedData.extend(iovec)
            else:
                abstract.FileDescriptor.writeSequence(self, iovec)


        def doWrite(self):
            result = abstract.FileDescriptor.doWrite(self)
            if self._tlsWaiting is not None:
                if not self.dataBuffer and not self._tempDataBuffer:
                    waiting = self._tlsWaiting
                    self._tlsWaiting = None
                    self.startTLS(waiting.context, waiting.extra)
                    self.writeSequence(waiting.bufferedData)
            return result


    def getHandle(self):
        """Return the socket for this connection."""
        return self.socket


    def doRead(self):
        """Calls self.protocol.dataReceived with all available data.

        This reads up to self.bufferSize bytes of data from its socket, then
        calls self.dataReceived(data) to process it.  If the connection is not
        lost through an error in the physical recv(), this function will return
        the result of the dataReceived call.
        """
        try:
            data = self.socket.recv(self.bufferSize)
        except socket.error, se:
            if se.args[0] == EWOULDBLOCK:
                return
            else:
                return main.CONNECTION_LOST
        if not data:
            return main.CONNECTION_DONE
        rval = self.protocol.dataReceived(data)
        if rval is not None:
            offender = self.protocol.dataReceived
            warningFormat = (
                'Returning a value other than None from %(fqpn)s is '
                'deprecated since %(version)s.')
            warningString = deprecate.getDeprecationWarningString(
                offender, versions.Version('Twisted', 11, 0, 0),
                format=warningFormat)
            deprecate.warnAboutFunction(offender, warningString)
        return rval