Skip to content

Commit 5b1c5c5

Browse files
Squashed 'tools/third_party/pywebsocket3/' changes from d1958b2ca7..9db3d29db4
9db3d29db4 Bump version number to reflect functional changes (#25) c6aea56f8b Stop wrapping os.popen3 (#24) 353b4aaf8a Give up expecting echo_client to be unicode safe (#23) 9ae2b3c90b Make tests work with python 2 on Windows (#20) e894c1b2ad Update the URL in setup.py (#19) f76e23c094 Fix incorrect use of "is" for integer comparison (#21) 3ab411d97f Fix version comparisons (#22) git-subtree-dir: tools/third_party/pywebsocket3 git-subtree-split: 9db3d29db4f3e3efb1722c0210bc88500ab00611
1 parent eff7017 commit 5b1c5c5

File tree

8 files changed

+62
-34
lines changed

8 files changed

+62
-34
lines changed

example/cgi-bin/hi.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
3+
print('Content-Type: text/plain')
4+
print('')
5+
print('Hi from hi.py')

example/echo_client.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,14 @@ def _do_closing_handshake(self):
599599

600600

601601
def main():
602+
# Force Python 2 to use the locale encoding, even when the output is not a
603+
# tty. This makes the behaviour the same as Python 3. The encoding won't
604+
# necessarily support all unicode characters. This problem is particularly
605+
# prevalent on Windows.
602606
if six.PY2:
603-
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
607+
import locale
608+
encoding = locale.getpreferredencoding()
609+
sys.stdout = codecs.getwriter(encoding)(sys.stdout)
604610

605611
parser = argparse.ArgumentParser()
606612
# We accept --command_line_flag style flags which is the same as Google
@@ -636,7 +642,7 @@ def main():
636642
'--message',
637643
dest='message',
638644
type=six.text_type,
639-
default=u'Hello,\u65e5\u672c',
645+
default=u'Hello,<>',
640646
help=('comma-separated messages to send. '
641647
'%s will force close the connection from server.' %
642648
_GOODBYE_MESSAGE))

mod_pywebsocket/standalone.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ def _parse_args_and_config(args):
403403
def _main(args=None):
404404
"""You can call this function from your own program, but please note that
405405
this function has some side-effects that might affect your program. For
406-
example, util.wrap_popen3_for_win use in this method replaces implementation
407-
of os.popen3.
406+
example, it changes the current directory.
408407
"""
409408

410409
options, args = _parse_args_and_config(args=args)
@@ -427,7 +426,6 @@ def _main(args=None):
427426
# full path of third_party/cygwin/bin.
428427
if 'CYGWIN_PATH' in os.environ:
429428
cygwin_path = os.environ['CYGWIN_PATH']
430-
util.wrap_popen3_for_win(cygwin_path)
431429

432430
def __check_script(scriptpath):
433431
return util.get_script_interp(scriptpath, cygwin_path)

mod_pywebsocket/util.py

-19
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,6 @@ def get_script_interp(script_path, cygwin_path=None):
9797
return None
9898

9999

100-
def wrap_popen3_for_win(cygwin_path):
101-
"""Wrap popen3 to support #!-script on Windows.
102-
103-
Args:
104-
cygwin_path: path for cygwin binary if command path is needed to be
105-
translated. None if no translation required.
106-
"""
107-
__orig_popen3 = os.popen3
108-
109-
def __wrap_popen3(cmd, mode='t', bufsize=-1):
110-
cmdline = cmd.split(' ')
111-
interp = get_script_interp(cmdline[0], cygwin_path)
112-
if interp:
113-
cmd = interp + ' ' + cmd
114-
return __orig_popen3(cmd, mode, bufsize)
115-
116-
os.popen3 = __wrap_popen3
117-
118-
119100
def hexify(s):
120101
return ' '.join(['%02x' % x for x in six.iterbytes(s)])
121102

setup.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
# This is used since python_requires field is not recognized with
4545
# pip version 9.0.0 and earlier
46-
if sys.version < '2.7':
46+
if sys.hexversion < 0x020700f0:
4747
print('%s requires Python 2.7 or later.' % _PACKAGE_NAME, file=sys.stderr)
4848
sys.exit(1)
4949

@@ -66,9 +66,8 @@
6666
packages=[_PACKAGE_NAME, _PACKAGE_NAME + '.handshake'],
6767
python_requires='>=2.7',
6868
install_requires=['six'],
69-
#TODO(suzukikeita): Update this to new Github URL
70-
url='http://code.google.com/p/pywebsocket/',
71-
version='3.0.0',
69+
url='https://github.com/GoogleChromeLabs/pywebsocket3',
70+
version='3.0.1',
7271
)
7372

7473
# vi:sts=4 sw=4 et

test/client_for_testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def handshake(self, socket):
303303
self._options.server_port,
304304
self._options.use_tls))
305305

306-
if self._options.version is 8:
306+
if self._options.version == 8:
307307
fields.append(_sec_origin_header(self._options.origin))
308308
else:
309309
fields.append(_origin_header(self._options.origin))

test/test_endtoend.py

+43-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"""
3333

3434
from __future__ import absolute_import
35+
from six.moves import urllib
36+
import locale
3537
import logging
3638
import os
3739
import signal
@@ -153,8 +155,9 @@ def setUp(self):
153155
# TODO(tyoshino): Use tearDown to kill the server.
154156

155157
def _run_python_command(self, commandline, stdout=None, stderr=None):
158+
close_fds = True if sys.platform != 'win32' else None
156159
return subprocess.Popen([sys.executable] + commandline,
157-
close_fds=True,
160+
close_fds=close_fds,
158161
stdout=stdout,
159162
stderr=stderr)
160163

@@ -632,7 +635,14 @@ def setUp(self):
632635

633636
def _check_example_echo_client_result(self, expected, stdoutdata,
634637
stderrdata):
635-
actual = stdoutdata.decode("utf-8")
638+
actual = stdoutdata.decode(locale.getpreferredencoding())
639+
640+
# In Python 3 on Windows we get "\r\n" terminators back from
641+
# the subprocess and we need to replace them with "\n" to get
642+
# a match. This is a bit of a hack, but avoids platform- and
643+
# version- specific code.
644+
actual = actual.replace('\r\n', '\n')
645+
636646
if actual != expected:
637647
raise Exception('Unexpected result on example echo client: '
638648
'%r (expected) vs %r (actual)' %
@@ -654,8 +664,8 @@ def test_example_echo_client(self):
654664
# Expected output for the default messages.
655665
default_expectation = (u'Send: Hello\n'
656666
u'Recv: Hello\n'
657-
u'Send: \u65e5\u672c\n'
658-
u'Recv: \u65e5\u672c\n'
667+
u'Send: <>\n'
668+
u'Recv: <>\n'
659669
u'Send close\n'
660670
u'Recv ack\n')
661671

@@ -693,6 +703,35 @@ def test_example_echo_client(self):
693703
self._close_server(server)
694704

695705

706+
class EndToEndTestWithCgi(EndToEndTestBase):
707+
def setUp(self):
708+
EndToEndTestBase.setUp(self)
709+
710+
def test_cgi(self):
711+
"""Verifies that CGI scripts work."""
712+
713+
server = self._run_server(extra_args=['--cgi-paths', '/cgi-bin'])
714+
time.sleep(_SERVER_WARMUP_IN_SEC)
715+
716+
url = 'http://localhost:%d/cgi-bin/hi.py' % self._options.server_port
717+
718+
# urlopen() in Python 2.7 doesn't support "with".
719+
try:
720+
f = urllib.request.urlopen(url)
721+
except:
722+
self._close_server(server)
723+
raise
724+
725+
try:
726+
self.assertEqual(f.getcode(), 200)
727+
self.assertEqual(f.info().get('Content-Type'), 'text/plain')
728+
body = f.read()
729+
self.assertEqual(body.rstrip(b'\r\n'), b'Hi from hi.py')
730+
finally:
731+
f.close()
732+
self._close_server(server)
733+
734+
696735
if __name__ == '__main__':
697736
unittest.main()
698737

test/test_http_header_util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_parse_invalid_uri(self):
7979

8080
host, port, resource = http_header_util.parse_uri(
8181
'ws://localhost:-1/ws')
82-
if sys.version >= '3.6':
82+
if sys.hexversion >= 0x030600f0:
8383
self.assertEqual(None, resource)
8484
else:
8585
self.assertEqual('localhost', host)

0 commit comments

Comments
 (0)