added makeGreenFile method to greenio.GreenSocket which creates GreenFile instance instead of standard socket._fileobject (like makefile() does), Converted all the old eventled code and tests to use makeGreenFile() this fixes a huge number of failing tests

This commit is contained in:
Denis Bilenko
2008-12-10 19:33:48 +06:00
parent bd2444f8bf
commit f1cc452dea
7 changed files with 34 additions and 39 deletions

View File

@@ -76,7 +76,7 @@ class SocketConsole(greenlib.GreenletContext):
def backdoor((conn, addr), locals=None):
host, port = addr
print "backdoor to %s:%s" % (host, port)
fl = conn.makefile("rw")
fl = conn.makeGreenFile("rw")
fl.newlines = '\n'
ctx = SocketConsole(fl)
ctx.register()

View File

@@ -123,7 +123,6 @@ def socket_send(descriptor, data):
except util.SSL.WantReadError:
return 0
# winsock sometimes throws ENOTCONN
SOCKET_CLOSED = (errno.ECONNRESET, errno.ENOTCONN, errno.ESHUTDOWN)
def socket_recv(descriptor, buflen):
@@ -318,15 +317,11 @@ class GreenSocket(object):
fn = self.listen = self.fd.listen
return fn(*args, **kw)
def old_makefile(self, *args, **kw):
self._refcount.increment()
new_sock = type(self)(self.fd, self._refcount)
return GreenFile(new_sock)
def makefile(self, mode='r', bufsize=-1):
return socket._fileobject(self.dup(), mode, bufsize)
# the following has problems, e.g. it doesn't recognise '\n' as a delimeter
#return GreenFile(self.dup())
def makeGreenFile(self, mode='r', bufsize=-1):
return GreenFile(self.dup())
recv = higher_order_recv(socket_recv)
@@ -522,7 +517,7 @@ class GreenPipeSocket(GreenSocket):
send = higher_order_send(file_send)
class GreenPipe(GreenFile):
class GreenPipe(GreenFile):
def __init__(self, fd):
set_nonblocking(fd)
self.fd = GreenPipeSocket(fd)

View File

@@ -416,7 +416,7 @@ class Timeout(RuntimeError):
class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
self.rfile = self.wfile = request.makefile()
self.rfile = self.wfile = request.makeGreenFile()
self.is_secure = request.is_secure
request.close() # close this now so that when rfile and wfile are closed, the socket gets closed
self.client_address = client_address

View File

@@ -65,7 +65,7 @@ class TestApi(tests.TestCase):
def accept_once(listenfd):
try:
conn, addr = listenfd.accept()
fd = conn.makefile()
fd = conn.makeGreenFile()
conn.close()
fd.write('hello\n')
fd.close()
@@ -76,7 +76,7 @@ class TestApi(tests.TestCase):
api.spawn(accept_once, server)
client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
fd = client.makefile()
fd = client.makeGreenFile()
client.close()
assert fd.readline() == 'hello\n'
@@ -89,7 +89,7 @@ class TestApi(tests.TestCase):
def accept_once(listenfd):
try:
conn, addr = listenfd.accept()
fl = conn.makefile('w')
fl = conn.makeGreenFile('w')
fl.write('hello\r\n')
fl.close()
conn.close()
@@ -103,7 +103,7 @@ class TestApi(tests.TestCase):
client = util.wrap_ssl(
api.connect_tcp(('127.0.0.1', server.getsockname()[1])))
client = client.makefile()
client = client.makeGreenFile()
assert client.readline() == 'hello\r\n'
assert client.read() == ''

View File

@@ -34,7 +34,7 @@ class TestGreenIo(tests.TestCase):
# by closing the socket prior to using the made file
try:
conn, addr = listener.accept()
fd = conn.makefile()
fd = conn.makeGreenFile()
conn.close()
fd.write('hello\n')
fd.close()
@@ -48,7 +48,7 @@ class TestGreenIo(tests.TestCase):
# by closing the made file and then sending a character
try:
conn, addr = listener.accept()
fd = conn.makefile()
fd = conn.makeGreenFile()
fd.write('hello')
fd.close()
conn.send('\n')
@@ -60,7 +60,7 @@ class TestGreenIo(tests.TestCase):
def did_it_work(server):
client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
fd = client.makefile()
fd = client.makeGreenFile()
client.close()
assert fd.readline() == 'hello\n'
assert fd.read() == ''
@@ -85,7 +85,7 @@ class TestGreenIo(tests.TestCase):
# closing the file object should close everything
try:
conn, addr = listener.accept()
conn = conn.makefile()
conn = conn.makeGreenFile()
conn.write('hello\n')
conn.close()
self.assertRaises(socket.error, conn.write, 'a')
@@ -94,7 +94,7 @@ class TestGreenIo(tests.TestCase):
server = api.tcp_listener(('0.0.0.0', 0))
killer = api.spawn(accept_once, server)
client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
fd = client.makefile()
fd = client.makeGreenFile()
client.close()
assert fd.read() == 'hello\n'
assert fd.read() == ''

View File

@@ -65,7 +65,7 @@ class ConnectionClosed(Exception):
def read_http(sock):
fd = sock.makefile()
fd = sock.makeGreenFile()
response_line = fd.readline()
if not response_line:
raise ConnectionClosed
@@ -102,7 +102,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.0\r\nHost: localhost\r\n\r\n')
result = fd.read()
fd.close()
@@ -114,7 +114,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
read_http(sock)
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
@@ -126,7 +126,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
cancel = api.exc_after(1, RuntimeError)
self.assertRaises(TypeError, fd.read, "This shouldn't work")
@@ -137,7 +137,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
read_http(sock)
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
@@ -161,7 +161,7 @@ class TestHttpd(tests.TestCase):
path_parts.append('path')
path = '/'.join(path_parts)
request = 'GET /%s HTTP/1.0\r\nHost: localhost\r\n\r\n' % path
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write(request)
result = fd.readline()
status = result.split(' ')[1]
@@ -184,7 +184,7 @@ class TestHttpd(tests.TestCase):
'Content-Length: 3',
'',
'a=a'))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write(request)
# send some junk after the actual request
@@ -197,7 +197,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
response_line_200,_,_ = read_http(sock)
fd.write('GET /notexist HTTP/1.1\r\nHost: localhost\r\n\r\n')

View File

@@ -85,7 +85,7 @@ class ConnectionClosed(Exception):
def read_http(sock):
fd = sock.makefile()
fd = sock.makeGreenFile()
response_line = fd.readline()
if not response_line:
raise ConnectionClosed
@@ -123,7 +123,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.0\r\nHost: localhost\r\n\r\n')
result = fd.read()
fd.close()
@@ -135,7 +135,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
read_http(sock)
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
@@ -147,7 +147,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
cancel = api.exc_after(1, RuntimeError)
self.assertRaises(TypeError, fd.read, "This shouldn't work")
@@ -158,7 +158,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
read_http(sock)
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
@@ -182,7 +182,7 @@ class TestHttpd(tests.TestCase):
path_parts.append('path')
path = '/'.join(path_parts)
request = 'GET /%s HTTP/1.0\r\nHost: localhost\r\n\r\n' % path
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write(request)
result = fd.readline()
status = result.split(' ')[1]
@@ -205,7 +205,7 @@ class TestHttpd(tests.TestCase):
'Content-Length: 3',
'',
'a=a'))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write(request)
# send some junk after the actual request
@@ -218,7 +218,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
response_line_200,_,_ = read_http(sock)
fd.write('GET /notexist HTTP/1.1\r\nHost: localhost\r\n\r\n')
@@ -233,7 +233,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
self.assert_('Transfer-Encoding: chunked' in fd.read())
@@ -242,7 +242,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n')
self.assert_('Transfer-Encoding: chunked' not in fd.read())
@@ -251,7 +251,7 @@ class TestHttpd(tests.TestCase):
sock = api.connect_tcp(
('127.0.0.1', 12346))
fd = sock.makefile()
fd = sock.makeGreenFile()
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
headers = fd.readuntil('\r\n\r\n')
self.assert_('Transfer-Encoding: chunked' in headers)