Jeffrey Gelens avatar Jeffrey Gelens committed c32ee0e

Splitted the Python fixes and exceptions in separate modules

Comments (0)

Files changed (3)

geventwebsocket/exceptions.py

+from socket import error as socket_error
+
+
+class WebSocketError(socket_error):
+    pass
+
+
+class FrameTooLargeException(WebSocketError):
+    pass

geventwebsocket/python_fixes.py

+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.closed:
+                return
+            RawIOBase.close(self)
+            self._sock = None
+
+    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

geventwebsocket/websocket.py

 import sys
-from socket import error as socket_error
+import struct
+
 from errno import EINTR
-import struct
 from gevent.coros import Semaphore
 
-
-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.closed:
-                return
-            RawIOBase.close(self)
-            self._sock = None
-
-    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
-
-
-class WebSocketError(socket_error):
-    pass
-
-
-class FrameTooLargeException(WebSocketError):
-    pass
+from python_fixes import makefile, is_closed
+from exceptions import FrameTooLargeException, WebSocketError
 
 
 class WebSocket(object):
-
     def _encode_text(self, s):
         if isinstance(s, unicode):
             return s.encode('utf-8')
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.