Skip to content

Commit 22f8bb7

Browse files
a-detisteauvipy
andauthored
prefer newer unittest.mock when available (#284)
* prefer newer unittest.mock when available * Update test/test_client.py Signed-off-by: Asif Saif Uddin <auvipy@gmail.com> * flake8 --------- Signed-off-by: Asif Saif Uddin <auvipy@gmail.com> Co-authored-by: Asif Saif Uddin <auvipy@gmail.com>
1 parent 5c9fc11 commit 22f8bb7

13 files changed

Lines changed: 39 additions & 19 deletions

File tree

docs/sources/basics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ necessarily need a connected socket, in fact, you don't even need a socket at al
3434
>>> def data_source():
3535
>>> yield TextMessage(u'hello world')
3636
37-
>>> from mock import MagicMock
37+
>>> from unittest.mock import MagicMock
3838
>>> source = MagicMock(side_effect=data_source)
3939
>>> ws = EchoWebSocket(sock=source)
4040
>>> ws.send(u'hello there')

example/basic/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
cur_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
2626
index_path = os.path.join(cur_dir, 'index.html')
27-
index_page = file(index_path, 'r').read()
27+
index_page = open(index_path, 'r').read()
2828

2929
class ChatWebSocketHandler(WebSocket):
3030
def received_message(self, m):

example/droid_sensor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import print_function
3+
24
__doc__ = """
35
WebSocket client that pushes Android sensor metrics to the
46
websocket server it is connected to.
@@ -64,7 +66,7 @@ def run(self):
6466
continue
6567

6668
c = lambda rad: rad * 360.0 / math.pi
67-
print c(azimuth), c(pitch), c(roll), x, y, z
69+
print(c(azimuth), c(pitch), c(roll), x, y, z)
6870

6971
if self.client.terminated:
7072
break

example/droid_sensor_cherrypy_server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import print_function
3+
24
import os.path
35
import cherrypy
46

@@ -8,7 +10,7 @@
810
class BroadcastWebSocketHandler(WebSocket):
911
def received_message(self, m):
1012
cherrypy.engine.publish('websocket-broadcast', str(m))
11-
13+
1214
class Root(object):
1315
@cherrypy.expose
1416
def display(self):
@@ -61,15 +63,15 @@ def index(self):
6163
</body>
6264
</html>
6365
"""
64-
66+
6567
if __name__ == '__main__':
6668
cherrypy.config.update({
6769
'server.socket_host': '0.0.0.0',
6870
'server.socket_port': 9000,
6971
'tools.staticdir.root': os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
7072
}
7173
)
72-
print os.path.abspath(os.path.join(__file__, 'static'))
74+
print(os.path.abspath(os.path.join(__file__, 'static')))
7375
WebSocketPlugin(cherrypy.engine).subscribe()
7476
cherrypy.tools.websocket = WebSocketTool()
7577

example/websensors/templates/.cache/board.html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def render_body(context,**pageargs):
2222
# SOURCE LINE 1
2323
__M_writer(u'<!DOCTYPE html>\n<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->\n<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->\n<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n <title>Mobile remote control</title>\n <meta name="description" content="Remote control your webapp with your mobile device">\n <meta name="viewport" content="width=device-width, initial-scale=1">\n <meta name="twitter:card" content="Sylvain Hellegouarch\'s twitter account">\n <meta name="twitter:site" content="@lawouach">\n <meta name="twitter:url" content="http://www.defuze.org">\n\n <link rel="stylesheet" href="/static/vendors/initializr/css/normalize.min.css">\n <link rel="stylesheet" href="/static/vendors/initializr/css/main.css">\n <link rel="stylesheet" href="/static/css/style.css">\n\n <script src="/static/vendors/initializr/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>\n </head>\n <body>\n <!--[if lt IE 7]>\n <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>\n <![endif]-->\n\n <div class="main-container" id="arena">\n\t <canvas id="tools" width="70" height="600"></canvas>\n\t <canvas id="board" width="150" height="600"></canvas>\n </div>\n\n <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>\n\t<script type="application/javascript" src="/static/vendors/jcanvas/jcanvas.min.js"></script>\n <script type="application/javascript" src="/static/vendors/initializr/js/plugins.js"></script>\n <script type="application/javascript" src="/static/js/drawingboard.js"></script>\n <script type="application/javascript">\n\t $(document).ready(function() {\n\t $.board({board: "')
2424
# SOURCE LINE 38
25-
__M_writer(unicode(boardid))
25+
__M_writer(str(boardid))
2626
__M_writer(u'", participant: "')
27-
__M_writer(unicode(participantid))
27+
__M_writer(str(participantid))
2828
__M_writer(u'"});\n\t });\n </script>\n </body>\n</html>\n')
2929
return ''
3030
finally:

example/websensors/templates/.cache/index.html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def render_body(context,**pageargs):
2121
# SOURCE LINE 1
2222
__M_writer(u'<!DOCTYPE html>\n<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->\n<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->\n<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n <title>Shared drawing board</title>\n <meta name="description" content="Remote control your webapp with your mobile device">\n <meta name="viewport" content="width=device-width, initial-scale=1">\n <meta name="twitter:card" content="Sylvain Hellegouarch\'s twitter account">\n <meta name="twitter:site" content="@lawouach">\n <meta name="twitter:url" content="http://www.defuze.org">\n\n <link rel="stylesheet" href="/static/vendors/initializr/css/normalize.min.css">\n <link rel="stylesheet" href="/static/vendors/initializr/css/main.css">\n\n <script src="/static/vendors/initializr/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>\n </head>\n <body>\n <!--[if lt IE 7]>\n <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>\n <![endif]-->\n\n <div class="header-container">\n <header class="wrapper clearfix">\n <h1 class="title">shared drawing board</h1>\n </header>\n </div>\n\n <div class="main-container">\n <div class="main wrapper clearfix">\n\n <article>\n <section>\n\t <p>This demo will demonstrate a fun usage of WebSocket and Canvases.\n\t The idea is that you will share a drawing board with other\n\t users or devices and each time someone will draw something onto\n\t the canvas, all other participants will see the drawing in near real-time\n\t on their side too.\n\t </p>\n\t <p>Please use the link below to share a drawing board:</p>\n\t <p><a href="http://192.168.1.13:8080/board/')
2323
# SOURCE LINE 44
24-
__M_writer(unicode(boardid))
24+
__M_writer(str(boardid))
2525
__M_writer(u'">http://192.168.1.13:8080/board/')
26-
__M_writer(unicode(boardid))
26+
__M_writer(str(boardid))
2727
__M_writer(u'</a></p>\n </section>\n </article>\n\n </div> <!-- #main -->\n </div> <!-- #main-container -->\n\n <div class="footer-container">\n <footer class="wrapper">\n <p>&copy; 2014 | <a href="http://www.defuze.org/">Sylvain Hellegouarch</a></p>\n </footer>\n </div>\n\n <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>\n <script type="application/javascript" src="/static/vendors/initializr/js/plugins.js"></script>\n </body>\n</html>\n')
2828
return ''
2929
finally:

test/test_cherrypy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import time
55
import unittest
66

7-
from mock import MagicMock, call
7+
try:
8+
from unittest.mock import MagicMock, call
9+
except ImportError:
10+
from mock import MagicMock, call
811

912
import cherrypy
1013
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool

test/test_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import socket
55
import time
66
import unittest
7-
8-
from mock import MagicMock, patch
7+
try:
8+
from unittest.mock import MagicMock, patch
9+
except ImportError:
10+
from mock import MagicMock, patch
911

1012
from ws4py import WS_KEY
1113
from ws4py.exc import HandshakeError

test/test_manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
except ImportError:
99
from itertools import zip_longest
1010

11-
from mock import MagicMock, call, patch
11+
try:
12+
from unittest.mock import MagicMock, call, patch
13+
except ImportError:
14+
from mock import MagicMock, call, patch
1215

1316
from ws4py.manager import WebSocketManager, SelectPoller,\
1417
EPollPoller

test/test_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# -*- coding: utf-8 -*-
22
import unittest
33

4+
try:
5+
from unittest.mock import MagicMock
6+
except ImportError:
7+
from mock import MagicMock
8+
49
from ws4py import format_addresses
510
from ws4py.websocket import WebSocket
6-
from mock import MagicMock
11+
712

813
class WSUtilities(unittest.TestCase):
914
def test_format_address(self):
@@ -14,7 +19,7 @@ def test_format_address(self):
1419

1520
log = format_addresses(ws)
1621
self.assertEqual(log, "[Local => 127.0.0.1:52300 | Remote => 127.0.0.1:4800]")
17-
22+
1823
if __name__ == '__main__':
1924
suite = unittest.TestSuite()
2025
loader = unittest.TestLoader()

0 commit comments

Comments
 (0)