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): def backdoor((conn, addr), locals=None):
host, port = addr host, port = addr
print "backdoor to %s:%s" % (host, port) print "backdoor to %s:%s" % (host, port)
fl = conn.makefile("rw") fl = conn.makeGreenFile("rw")
fl.newlines = '\n' fl.newlines = '\n'
ctx = SocketConsole(fl) ctx = SocketConsole(fl)
ctx.register() ctx.register()

View File

@@ -123,7 +123,6 @@ def socket_send(descriptor, data):
except util.SSL.WantReadError: except util.SSL.WantReadError:
return 0 return 0
# winsock sometimes throws ENOTCONN # winsock sometimes throws ENOTCONN
SOCKET_CLOSED = (errno.ECONNRESET, errno.ENOTCONN, errno.ESHUTDOWN) SOCKET_CLOSED = (errno.ECONNRESET, errno.ENOTCONN, errno.ESHUTDOWN)
def socket_recv(descriptor, buflen): def socket_recv(descriptor, buflen):
@@ -318,15 +317,11 @@ class GreenSocket(object):
fn = self.listen = self.fd.listen fn = self.listen = self.fd.listen
return fn(*args, **kw) 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): def makefile(self, mode='r', bufsize=-1):
return socket._fileobject(self.dup(), mode, bufsize) 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) recv = higher_order_recv(socket_recv)

View File

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

View File

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

View File

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

View File

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