From 9ace2b141f98ec1f2def90e8e4be0ec01dcaf65f Mon Sep 17 00:00:00 2001 From: Dmitry Orlov Date: Sun, 30 Aug 2015 01:40:02 +0300 Subject: [PATCH] [fix] py3 support --- .gitignore | 2 +- tests/__init__.py | 15 +++++++++++---- tests/test_js_static.py | 12 +++++++++++- tests/test_log_thread_exc.py | 10 +++++++--- wsrpc/__init__.py | 4 ++-- wsrpc/websocket/handler.py | 13 ++++++++++--- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index db4561e..5f8eadc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ __pycache__/ # Distribution / packaging .Python -env/ +env*/ build/ develop-eggs/ dist/ diff --git a/tests/__init__.py b/tests/__init__.py index 9196253..1ad0c12 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,14 +1,21 @@ #!/usr/bin/env python # encoding: utf-8 +from __future__ import absolute_import + from tornado.gen import Return, Future, sleep, coroutine import tornado.web -import exceptions + +try: + import exceptions +except ImportError: + import builtins as exceptions + from tornado import testing, websocket from tornado.httpserver import HTTPServer from wsrpc import WebSocket, WebSocketThreaded -import async -import sync +from .async import TestRoute as TestAsyncRoute +from .sync import TestRoute as TestSyncRoute try: import ujson as json @@ -87,7 +94,7 @@ def _call_coro(self, data): ) def call(self, func, **kwargs): - assert isinstance(func, (basestring, unicode)) + assert isinstance(func, str) serial = self._get_serial() diff --git a/tests/test_js_static.py b/tests/test_js_static.py index 9e3cbb1..f45a228 100644 --- a/tests/test_js_static.py +++ b/tests/test_js_static.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 +import codecs import os from tornado.gen import coroutine import tornado.web @@ -11,6 +12,12 @@ from tornado.httpclient import AsyncHTTPClient +try: + unicode() +except NameError: + unicode = str + + class Application(tornado.web.Application): def __init__(self): handlers = ( @@ -33,7 +40,10 @@ def setUp(self): def fetch(self, filename): response = yield AsyncHTTPClient().fetch("http://localhost:{0.port}/static/{1}".format(self, filename)) self.assertTrue(response.code, 200) - self.assertEqual(response.body, open(os.path.join(self.static_path, filename)).read()) + self.assertEqual( + response.body.decode('utf-8'), + codecs.open(os.path.join(self.static_path, filename), 'r', 'utf-8').read() + ) @gen_test def test_wsrpc_js(self): diff --git a/tests/test_log_thread_exc.py b/tests/test_log_thread_exc.py index f7bf21b..ca06857 100644 --- a/tests/test_log_thread_exc.py +++ b/tests/test_log_thread_exc.py @@ -4,8 +4,12 @@ from wsrpc.websocket.common import log_thread_exceptions +class TestExc(Exception): + pass + + def exc_func(): - raise Exception("Test") + raise TestExc("Test") def test(): @@ -16,8 +20,8 @@ class TestLogThreadException(AsyncTestCase): def test_exc(self): try: log_thread_exceptions(exc_func)() - except Exception as e: - self.assertEqual(e.message, "Test") + except TestExc: + pass def test_func(self): log_thread_exceptions(test)() diff --git a/wsrpc/__init__.py b/wsrpc/__init__.py index 290ad31..89ef3da 100644 --- a/wsrpc/__init__.py +++ b/wsrpc/__init__.py @@ -2,8 +2,8 @@ # encoding: utf-8 import os.path import tornado.web -from websocket import WebSocketRoute, WebSocket, WebSocketThreaded -from websocket.route import decorators +from .websocket import WebSocketRoute, WebSocket, WebSocketThreaded +from .websocket.route import decorators STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') diff --git a/wsrpc/websocket/handler.py b/wsrpc/websocket/handler.py index a201b37..5e661ae 100644 --- a/wsrpc/websocket/handler.py +++ b/wsrpc/websocket/handler.py @@ -23,7 +23,12 @@ except ImportError: import json -from tools import iteritems, Lazy +from .tools import iteritems, Lazy + +try: + unicode() +except NameError: + unicode = str global_log = logging.getLogger("wsrpc") @@ -37,9 +42,11 @@ def ping(obj, *args, **kwargs): class ClientException(Exception): pass + class ConnectionClosed(Exception): pass + class PingTimeoutError(Exception): pass @@ -213,7 +220,7 @@ def on_close(self): @tornado.gen.coroutine def on_message(self, message): - log.debug(u'Client %s send message: "%s"', self.id, message) + log.debug('Client %s send message: "%s"', self.id, message) # deserialize message data = self._data_load(message) @@ -274,7 +281,7 @@ def _prepare_args(self, args): arguments = [] kwargs = {} - if isinstance(args, types.NoneType): + if isinstance(args, type(None)): return arguments, kwargs if isinstance(args, list):