Commit c71ebb60 authored by Jason Madden's avatar Jason Madden

First pass on socket._fileobject for Py3; this went away, but is used heavily...

First pass on socket._fileobject for Py3; this went away, but is used heavily in our code base, so the shortest path is probably going to be to emulate it. This fixes test__backdoor.py. others to come.
parent 59440100
This diff is collapsed.
......@@ -72,6 +72,7 @@ class BackdoorServer(StreamServer):
except ImportError:
import builtins
console.locals["builtins"] = builtins
console.locals['__builtins__'] = builtins
console.interact(banner=self.banner)
except SystemExit: # raised by quit()
if not PY3:
......@@ -89,7 +90,13 @@ class BackdoorServer(StreamServer):
class _fileobject(socket._fileobject):
if not PY3:
def write(self, data):
self._sock.sendall(data)
else:
def write(self, data):
if isinstance(data, str):
data = data.encode('utf-8')
self._sock.sendall(data)
def isatty(self):
......@@ -98,8 +105,15 @@ class _fileobject(socket._fileobject):
def flush(self):
pass
def _readline(self, *a):
return socket._fileobject.readline(self, *a).replace(b"\r\n", b"\n")
if not PY3:
readline = _readline
else:
def readline(self, *a):
return socket._fileobject.readline(self, *a).replace("\r\n", "\n")
line = self._readline(*a)
return line.decode('utf-8')
if __name__ == '__main__':
if not sys.argv[1:]:
......
......@@ -6,12 +6,16 @@ from six import xrange
def read_until(conn, postfix):
read = ''
read = b''
if isinstance(postfix, str) and str != bytes:
postfix = postfix.encode('utf-8')
while not read.endswith(postfix):
result = conn.recv(1)
if not result:
raise AssertionError('Connection ended before %r. Data read:\n%r' % (postfix, read))
read += result
if str != bytes:
read = read.decode('utf-8')
return read
......@@ -28,10 +32,14 @@ class Test(greentest.TestCase):
def connect():
conn = create_connection(('127.0.0.1', server.server_port))
try:
read_until(conn, '>>> ')
conn.sendall('2+2\r\n')
line = conn.makefile().readline()
assert line.strip() == '4', repr(line)
conn.sendall(b'2+2\r\n')
with conn.makefile() as f:
line = f.readline()
self.assertEqual(line.strip(), '4', repr(line))
finally:
conn.close()
server.start()
try:
......@@ -47,10 +55,12 @@ class Test(greentest.TestCase):
try:
conn = create_connection(('127.0.0.1', server.server_port))
read_until(conn, '>>> ')
conn.sendall('quit()\r\n')
line = conn.makefile().read()
conn.sendall(b'quit()\r\n')
with conn.makefile() as f:
line = f.read()
self.assertEqual(line, '')
finally:
conn.close()
server.stop()
def test_sys_exit(self):
......@@ -58,21 +68,24 @@ class Test(greentest.TestCase):
server.start()
try:
conn = create_connection(('127.0.0.1', server.server_port))
read_until(conn, '>>> ')
conn.sendall('import sys; sys.exit(0)\r\n')
line = conn.makefile().read()
read_until(conn, b'>>> ')
conn.sendall(b'import sys; sys.exit(0)\r\n')
with conn.makefile() as f:
line = f.read()
self.assertEqual(line, '')
finally:
conn.close()
server.stop()
def test_banner(self):
banner = "Welcome stranger!"
banner = "Welcome stranger!" # native string
server = backdoor.BackdoorServer(('127.0.0.1', 0), banner=banner)
server.start()
try:
conn = create_connection(('127.0.0.1', server.server_port))
response = read_until(conn, '>>> ')
self.assertEqual(response[:len(banner)], banner)
response = read_until(conn, b'>>> ')
self.assertEqual(response[:len(banner)], banner, response)
conn.close()
finally:
server.stop()
......@@ -81,11 +94,12 @@ class Test(greentest.TestCase):
server.start()
try:
conn = create_connection(('127.0.0.1', server.server_port))
read_until(conn, '>>> ')
conn.sendall('locals()["__builtins__"]\r\n')
read_until(conn, b'>>> ')
conn.sendall(b'locals()["__builtins__"]\r\n')
response = read_until(conn, '>>> ')
self.assertTrue(len(response) < 300, msg="locals() unusable: %s..." % response[:100])
self.assertTrue(len(response) < 300, msg="locals() unusable: %s..." % response)
finally:
conn.close()
server.stop()
......
......@@ -89,7 +89,6 @@ test__pool.py
FLAKY test___example_servers.py
test__example_udp_client.py
test__os.py
test__backdoor.py
test_threading_2.py
test__refcount.py
test__subprocess.py
......
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