Skip to content

Commit

Permalink
[fix] py3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mosquito committed Aug 29, 2015
1 parent 1499c12 commit 9ace2b1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ __pycache__/

# Distribution / packaging
.Python
env/
env*/
build/
develop-eggs/
dist/
Expand Down
15 changes: 11 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 11 additions & 1 deletion tests/test_js_static.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# encoding: utf-8
import codecs
import os
from tornado.gen import coroutine
import tornado.web
Expand All @@ -11,6 +12,12 @@
from tornado.httpclient import AsyncHTTPClient


try:
unicode()
except NameError:
unicode = str


class Application(tornado.web.Application):
def __init__(self):
handlers = (
Expand All @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_log_thread_exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)()
4 changes: 2 additions & 2 deletions wsrpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
13 changes: 10 additions & 3 deletions wsrpc/websocket/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -37,9 +42,11 @@ def ping(obj, *args, **kwargs):
class ClientException(Exception):
pass


class ConnectionClosed(Exception):
pass


class PingTimeoutError(Exception):
pass

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9ace2b1

Please sign in to comment.