gevent-websocket / geventwebsocket / python_fixes.py

import sys


if sys.version_info[:2] == (2, 7):
    # Python 2.7 has a working BufferedReader but socket.makefile() does not
    # use it.
    # Python 2.6's BufferedReader is broken (TypeError: recv_into() argument
    # 1 must be pinned buffer, not bytearray).
    from io import BufferedReader, RawIOBase

    class SocketIO(RawIOBase):
        def __init__(self, sock):
            RawIOBase.__init__(self)
            self._sock = sock

        def readinto(self, b):
            self._checkClosed()
            while True:
                try:
                    return self._sock.recv_into(b)
                except socket_error as ex:
                    if ex.args[0] == EINTR:
                        continue
                    raise

        def readable(self):
            return self._sock is not None

        @property
        def closed(self):
            return self._sock is None

        def fileno(self):
            self._checkClosed()
            return self._sock.fileno()

        @property
        def name(self):
            if not self.closed:
                return self.fileno()
            else:
                return -1

        def close(self):
            if self._sock is None:
                return
            else:
                self._sock.close()
                self._sock = None
                RawIOBase.close(self)

    def makefile(socket):
        return BufferedReader(SocketIO(socket))

else:
    def makefile(socket):
        # XXX on python3 enable buffering
        return socket.makefile()


if sys.version_info[:2] < (2, 7):
    def is_closed(fobj):
        return fobj._sock is None
else:
    def is_closed(fobj):
        return fobj.closed
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.