Commit d65af447 authored by Jason Madden's avatar Jason Madden

Fix test__pywsgi.py under Python 3. fix lint.

parent 81323b18
[pep8] [pep8]
ignore=E702,E265,E402,E731,E266,E261,W503 ignore=E702,E265,E402,E731,E266,E261,W503,E129
max_line_length=160 max_line_length=160
exclude=.tox,.git,build,2.6,2.7,2.7pypy,3.3,test_support.py,test_queue.py,patched_tests_setup.py,test_threading_2.py,lock_tests.py,_sslgte279.py exclude=.tox,.git,build,2.6,2.7,2.7pypy,3.3,test_support.py,test_queue.py,patched_tests_setup.py,test_threading_2.py,lock_tests.py,_sslgte279.py
...@@ -27,6 +27,7 @@ def _get_memory(string, offset): ...@@ -27,6 +27,7 @@ def _get_memory(string, offset):
timeout_default = object() timeout_default = object()
class _wrefsocket(_socket.socket): class _wrefsocket(_socket.socket):
# Plain stdlib socket.socket objects subclass _socket.socket # Plain stdlib socket.socket objects subclass _socket.socket
# and add weakref ability. The ssl module, for one, counts on this. # and add weakref ability. The ssl module, for one, counts on this.
...@@ -34,11 +35,12 @@ class _wrefsocket(_socket.socket): ...@@ -34,11 +35,12 @@ class _wrefsocket(_socket.socket):
# monkey patched to be the object from this module), but we still # monkey patched to be the object from this module), but we still
# need to make sure what we do create can be weakrefd. # need to make sure what we do create can be weakrefd.
__slots__ = ["__weakref__",] __slots__ = ["__weakref__", ]
_closedsocket = _wrefsocket() _closedsocket = _wrefsocket()
_closedsocket.close() _closedsocket.close()
class socket(object): class socket(object):
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None): def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):
...@@ -416,6 +418,7 @@ if hasattr(_socket, "socketpair"): ...@@ -416,6 +418,7 @@ if hasattr(_socket, "socketpair"):
b = socket(family, type, proto, b.detach()) b = socket(family, type, proto, b.detach())
return a, b return a, b
# PyPy needs drop and reuse # PyPy needs drop and reuse
def _do_reuse_or_drop(socket, methname): def _do_reuse_or_drop(socket, methname):
try: try:
...@@ -427,6 +430,7 @@ def _do_reuse_or_drop(socket, methname): ...@@ -427,6 +430,7 @@ def _do_reuse_or_drop(socket, methname):
from io import BytesIO from io import BytesIO
class _fileobject(object): class _fileobject(object):
"""Faux file object attached to a socket object.""" """Faux file object attached to a socket object."""
...@@ -500,7 +504,7 @@ class _fileobject(object): ...@@ -500,7 +504,7 @@ class _fileobject(object):
view = memoryview(data) view = memoryview(data)
try: try:
while write_offset < data_size: while write_offset < data_size:
self._sock.sendall(view[write_offset:write_offset+buffer_size]) self._sock.sendall(view[write_offset:write_offset + buffer_size])
write_offset += buffer_size write_offset += buffer_size
finally: finally:
if write_offset < data_size: if write_offset < data_size:
...@@ -519,8 +523,7 @@ class _fileobject(object): ...@@ -519,8 +523,7 @@ class _fileobject(object):
return return
self._wbuf.append(data) self._wbuf.append(data)
self._wbuf_len += len(data) self._wbuf_len += len(data)
if (self._wbufsize == 0 or if (self._wbufsize == 0 or (self._wbufsize == 1 and b'\n' in data) or
(self._wbufsize == 1 and b'\n' in data) or
(self._wbufsize > 1 and self._wbuf_len >= self._wbufsize)): (self._wbufsize > 1 and self._wbuf_len >= self._wbufsize)):
self.flush() self.flush()
...@@ -530,8 +533,7 @@ class _fileobject(object): ...@@ -530,8 +533,7 @@ class _fileobject(object):
lines = filter(None, map(str, list)) lines = filter(None, map(str, list))
self._wbuf_len += sum(map(len, lines)) self._wbuf_len += sum(map(len, lines))
self._wbuf.extend(lines) self._wbuf.extend(lines)
if (self._wbufsize <= 1 or if (self._wbufsize <= 1 or self._wbuf_len >= self._wbufsize):
self._wbuf_len >= self._wbufsize):
self.flush() self.flush()
def read(self, size=-1): def read(self, size=-1):
......
...@@ -154,7 +154,6 @@ def patch_thread(threading=True, _threading_local=True, Event=False): ...@@ -154,7 +154,6 @@ def patch_thread(threading=True, _threading_local=True, Event=False):
def join(timeout=None): def join(timeout=None):
if threading.current_thread() is main_thread: if threading.current_thread() is main_thread:
raise RuntimeError("Cannot join current thread") raise RuntimeError("Cannot join current thread")
self = _greenlet
if _greenlet.dead or not main_thread.is_alive(): if _greenlet.dead or not main_thread.is_alive():
return return
elif timeout: elif timeout:
......
...@@ -128,7 +128,7 @@ class Input(object): ...@@ -128,7 +128,7 @@ class Input(object):
length -= datalen length -= datalen
if length == 0: if length == 0:
break break
if use_readline and data[-1] == b"\n": if use_readline and data[-1] == b"\n"[0]:
break break
else: else:
line = rfile.readline() line = rfile.readline()
...@@ -339,9 +339,11 @@ class WSGIHandler(object): ...@@ -339,9 +339,11 @@ class WSGIHandler(object):
def handle_one_request(self): def handle_one_request(self):
if self.rfile.closed: if self.rfile.closed:
return return
try: try:
self.requestline = self.read_requestline() self.requestline = self.read_requestline()
# Account for old subclasses that haven't done this
if PY3 and isinstance(self.requestline, bytes):
self.requestline = self.requestline.decode('latin-1')
except socket.error: except socket.error:
# "Connection reset by peer" or other socket errors aren't interesting here # "Connection reset by peer" or other socket errors aren't interesting here
return return
......
...@@ -24,12 +24,14 @@ def create_connection(address): ...@@ -24,12 +24,14 @@ def create_connection(address):
conn.connect(address) conn.connect(address)
return conn return conn
def readline(conn): def readline(conn):
f = conn.makefile() f = conn.makefile()
line = f.readline() line = f.readline()
f.close() f.close()
return line return line
class Test(greentest.TestCase): class Test(greentest.TestCase):
def test(self): def test(self):
......
...@@ -26,6 +26,7 @@ import sys ...@@ -26,6 +26,7 @@ import sys
PYPY = hasattr(sys, 'pypy_version_info') PYPY = hasattr(sys, 'pypy_version_info')
PY3 = sys.version_info[0] >= 3 PY3 = sys.version_info[0] >= 3
def _write_to_closed(f, s): def _write_to_closed(f, s):
try: try:
r = f.write(s) r = f.write(s)
......
from __future__ import print_function from __future__ import print_function
import sys import sys
if not sys.argv[1:]: if not sys.argv[1:]:
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
p = Popen([sys.executable, __file__, 'subprocess'], stdin=PIPE, stdout=PIPE, stderr=PIPE) p = Popen([sys.executable, __file__, 'subprocess'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
......
...@@ -16,6 +16,7 @@ lsof_command = 'lsof -p %s > %s' % (pid, tmpname) ...@@ -16,6 +16,7 @@ lsof_command = 'lsof -p %s > %s' % (pid, tmpname)
import sys import sys
PY3 = sys.version_info[0] >= 3 PY3 = sys.version_info[0] >= 3
def get_open_files(): def get_open_files():
if os.system(lsof_command): if os.system(lsof_command):
raise OSError('lsof failed') raise OSError('lsof failed')
......
This diff is collapsed.
...@@ -85,37 +85,22 @@ class TestCase(greentest.TestCase): ...@@ -85,37 +85,22 @@ class TestCase(greentest.TestCase):
sock.connect((self.server.server_host, self.server.server_port)) sock.connect((self.server.server_host, self.server.server_port))
if PY3: if PY3:
kwargs = {'buffering': bufsize, 'mode': 'rb'} # Under Python3, you can't read and write to the same
# makefile() opened in r, and r+ is not allowed
kwargs = {'buffering': bufsize, 'mode': 'rwb'}
else: else:
kwargs = {'bufsize': bufsize} kwargs = {'bufsize': bufsize}
rconn = sock.makefile(**kwargs) rconn = sock.makefile(**kwargs)
if PY3: if PY3:
# Under Python3, you can't read and write to the same
# makefile() opened in r, and r+ is not allowed
kwargs['mode'] = 'wb'
wconn = sock.makefile(**kwargs)
rconn._sock = sock rconn._sock = sock
_rconn_close = rconn.close
def write(data):
if isinstance(data, str):
data = data.encode('ascii')
return wconn.write(data)
def flush():
return wconn.flush()
def close():
_rconn_close()
wconn.close()
rconn.write = write
rconn.flush = flush
rconn.close = close
rconn._sock.settimeout(timeout) rconn._sock.settimeout(timeout)
sock.close() sock.close()
return rconn return rconn
def send_request(self, url='/', timeout=0.1, bufsize=1): def send_request(self, url='/', timeout=0.1, bufsize=1):
conn = self.makefile(timeout=timeout, bufsize=bufsize) conn = self.makefile(timeout=timeout, bufsize=bufsize)
conn.write('GET %s HTTP/1.0\r\n\r\n' % url) conn.write(('GET %s HTTP/1.0\r\n\r\n' % url).encode('latin-1'))
conn.flush() conn.flush()
return conn return conn
...@@ -141,7 +126,7 @@ class TestCase(greentest.TestCase): ...@@ -141,7 +126,7 @@ class TestCase(greentest.TestCase):
def assertNotAccepted(self): def assertNotAccepted(self):
conn = self.makefile() conn = self.makefile()
conn.write('GET / HTTP/1.0\r\n\r\n') conn.write(b'GET / HTTP/1.0\r\n\r\n')
conn.flush() conn.flush()
result = '' result = ''
try: try:
......
...@@ -51,7 +51,7 @@ class Settings: ...@@ -51,7 +51,7 @@ class Settings:
@staticmethod @staticmethod
def assert500(self): def assert500(self):
conn = self.makefile() conn = self.makefile()
conn.write('GET / HTTP/1.0\r\n\r\n') conn.write(b'GET / HTTP/1.0\r\n\r\n')
result = conn.read() result = conn.read()
assert result.startswith(internal_error_start), (result, internal_error_start) assert result.startswith(internal_error_start), (result, internal_error_start)
assert result.endswith(internal_error_end), (result, internal_error_end) assert result.endswith(internal_error_end), (result, internal_error_end)
...@@ -61,7 +61,7 @@ class Settings: ...@@ -61,7 +61,7 @@ class Settings:
@staticmethod @staticmethod
def assert503(self): def assert503(self):
conn = self.makefile() conn = self.makefile()
conn.write('GET / HTTP/1.0\r\n\r\n') conn.write(b'GET / HTTP/1.0\r\n\r\n')
result = conn.read() result = conn.read()
assert result == internal_error503, (result, internal_error503) assert result == internal_error503, (result, internal_error503)
......
...@@ -5,5 +5,4 @@ if sys.version_info[0] == 2: ...@@ -5,5 +5,4 @@ if sys.version_info[0] == 2:
gevent.monkey.patch_all() gevent.monkey.patch_all()
import gevent import gevent
assert threading._sleep is gevent.sleep, threading._sleep assert threading._sleep is gevent.sleep, threading._sleep
...@@ -81,7 +81,6 @@ if PYPY: ...@@ -81,7 +81,6 @@ if PYPY:
if PY3: if PY3:
# No idea / TODO # No idea / TODO
FAILING_TESTS += ''' FAILING_TESTS += '''
test__pywsgi.py
FLAKY test__socket_dns.py FLAKY test__socket_dns.py
'''.strip().split('\n') '''.strip().split('\n')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment