From a27e9b168c4a928c48782fd4355fc045c2f0e661 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 1 Jan 2020 16:07:14 +0100 Subject: [PATCH] [python3] adjust code to Python3 in test scripts. Refs #38 I don't know how to fix big_file.cgi If you know how to get equivalent of this script in Python3, tell me. --- configure.ac | 2 +- test/cgi/chunked_bzip2.py | 14 ++-- test/cgi/chunked_deflate.py | 14 ++-- test/cgi/chunked_gzip.py | 18 ++-- test/cgi/chunked_raw_deflate.py | 14 ++-- test/server/crlf.py | 144 ++++++++++++++++---------------- test/server/w32.py | 38 ++++----- 7 files changed, 126 insertions(+), 118 deletions(-) diff --git a/configure.ac b/configure.ac index 30b1fc11..9b2b2ae3 100644 --- a/configure.ac +++ b/configure.ac @@ -16,7 +16,7 @@ AC_CONFIG_AUX_DIR(config) AC_CONFIG_MACRO_DIR([config/m4]) PACKAGE=elinks -VERSION=0.13.0 +VERSION=0.14.GIT AC_SUBST(PACKAGE) AC_SUBST(VERSION) AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Package version]) diff --git a/test/cgi/chunked_bzip2.py b/test/cgi/chunked_bzip2.py index 3e07a174..1d666571 100755 --- a/test/cgi/chunked_bzip2.py +++ b/test/cgi/chunked_bzip2.py @@ -1,16 +1,18 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import bz2, os, time -data1 = 'Two lines should be visible.
The second line.' +data1 = b'Two lines should be visible.
The second line.' cd1 = bz2.compress(data1) length = len(cd1) next_chunk = hex(length - 10)[2:] -os.write(1, "Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: bzip2\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") -os.write(1, "\r\na\r\n") +os.write(1, b"Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: bzip2\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") +os.write(1, b"\r\na\r\n") os.write(1, cd1[:10]) time.sleep(2) -os.write(1, "\r\n%s\r\n" % next_chunk) +os.write(1, b"\r\n") +os.write(1, bytes(next_chunk, 'iso8859-1')) +os.write(1, b"\r\n") os.write(1, cd1[10:]) -os.write(1, "\r\n0\r\n") +os.write(1, b"\r\n0\r\n") diff --git a/test/cgi/chunked_deflate.py b/test/cgi/chunked_deflate.py index 45f06828..01f676dd 100755 --- a/test/cgi/chunked_deflate.py +++ b/test/cgi/chunked_deflate.py @@ -1,16 +1,18 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os, time, zlib -data1 = 'Two lines should be visible.
The second line.' +data1 = b'Two lines should be visible.
The second line.' cd1 = zlib.compress(data1) length = len(cd1) next_chunk = hex(length - 10)[2:] -os.write(1, "Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: deflate\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") -os.write(1, "\r\na\r\n") +os.write(1, b"Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: deflate\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") +os.write(1, b"\r\na\r\n") os.write(1, cd1[:10]) time.sleep(2) -os.write(1, "\r\n%s\r\n" % next_chunk) +os.write(1, b"\r\n") +os.write(1, bytes(next_chunk, 'iso8859-1')) +os.write(1, b"\r\n") os.write(1, cd1[10:]) -os.write(1, "\r\n0\r\n") +os.write(1, b"\r\n0\r\n") diff --git a/test/cgi/chunked_gzip.py b/test/cgi/chunked_gzip.py index e01f6070..9a2fd265 100755 --- a/test/cgi/chunked_gzip.py +++ b/test/cgi/chunked_gzip.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python -import gzip, os, time, StringIO +#!/usr/bin/env python3 +import gzip, os, time, six -output = StringIO.StringIO() +output = six.BytesIO() -data1 = 'Two lines should be visible.
The second line.' +data1 = b'Two lines should be visible.
The second line.' f1 = gzip.GzipFile("/tmp/1.gz", mode = "wb", fileobj=output) f1.write(data1) @@ -15,10 +15,12 @@ output.close() length = len(cd1) next_chunk = hex(length - 10)[2:] -os.write(1, "Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: gzip\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") -os.write(1, "\r\na\r\n") +os.write(1, b"Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: gzip\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") +os.write(1, b"\r\na\r\n") os.write(1, cd1[:10]) time.sleep(2) -os.write(1, "\r\n%s\r\n" % next_chunk) +os.write(1, b"\r\n") +os.write(1, bytes(next_chunk, 'iso8859-1')) +os.write(1, b"\r\n") os.write(1, cd1[10:]) -os.write(1, "\r\n0\r\n") +os.write(1, b"\r\n0\r\n") diff --git a/test/cgi/chunked_raw_deflate.py b/test/cgi/chunked_raw_deflate.py index 6a762636..e318e5c2 100755 --- a/test/cgi/chunked_raw_deflate.py +++ b/test/cgi/chunked_raw_deflate.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os, time from zlib import * @@ -6,17 +6,19 @@ from zlib import * # requires a ZLIB header. However, Microsoft-IIS/6.0 sends a raw # DEFLATE stream instead. This CGI tests how ELinks handles that. -data1 = 'Two lines should be visible.
The second line.' +data1 = b'Two lines should be visible.
The second line.' ob = compressobj(Z_DEFAULT_COMPRESSION, DEFLATED, -MAX_WBITS) cd1 = ob.compress(data1) cd1 += ob.flush() length = len(cd1) next_chunk = hex(length - 10)[2:] -os.write(1, "Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: deflate\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") -os.write(1, "\r\na\r\n") +os.write(1, b"Date: Sun, 20 Jan 2008 15:24:00 GMT\r\nServer: ddd\r\nTransfer-Encoding: chunked\r\nContent-Encoding: deflate\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n") +os.write(1, b"\r\na\r\n") os.write(1, cd1[:10]) time.sleep(2) -os.write(1, "\r\n%s\r\n" % next_chunk) +os.write(1, b"\r\n") +os.write(1, bytes(next_chunk, 'iso8859-1')) +os.write(1, b"\r\n") os.write(1, cd1[10:]) -os.write(1, "\r\n0\r\n") +os.write(1, b"\r\n0\r\n") diff --git a/test/server/crlf.py b/test/server/crlf.py index 84b49fd1..d0c978a1 100755 --- a/test/server/crlf.py +++ b/test/server/crlf.py @@ -1,7 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os -from BaseHTTPServer import * +from http.server import * import tempfile import signal @@ -19,103 +19,103 @@ F_Hidden = 0 F_TextArea = 1 def encode(ch, encoding): - if ch == C_CRLF: - return encode(C_CR, encoding) + encode(C_LF, encoding) - if ch == C_CR: - if encoding == E_Raw: - return "\r" - if encoding == E_JavaScript: - return "\\r" - if encoding == E_Entity: - return " " - if ch == C_LF: - if encoding == E_Raw: - return "\n" - if encoding == E_JavaScript: - return "\\n" - if encoding == E_Entity: - return " " + if ch == C_CRLF: + return encode(C_CR, encoding) + encode(C_LF, encoding) + if ch == C_CR: + if encoding == E_Raw: + return "\r" + if encoding == E_JavaScript: + return "\\r" + if encoding == E_Entity: + return " " + if ch == C_LF: + if encoding == E_Raw: + return "\n" + if encoding == E_JavaScript: + return "\\n" + if encoding == E_Entity: + return " " def get_form(ch, encoding, field): - text = "foo" + encode(ch, encoding) + "bar" - if encoding == E_JavaScript: - text_initial = "" - else: - text_initial = text + text = "foo" + encode(ch, encoding) + "bar" + if encoding == E_JavaScript: + text_initial = "" + else: + text_initial = text - s = """ + s = """ Form Test
""" - if field == F_Hidden: - s += '' - elif field == F_TextArea: - s += '' - s += "\n
" - if encoding == E_JavaScript: - s += """ + if field == F_Hidden: + s += '' + elif field == F_TextArea: + s += '' + s += "\n" + if encoding == E_JavaScript: + s += """ """ % (text) - s += "" - return s + s += "" + return s class forwarder(BaseHTTPRequestHandler): - def do_GET(self): - w.write(self.path + "\n") - w.flush() - self.send_response(200) - self.send_header("Content-Type", "text/plain") - self.end_headers() - self.wfile.write("Dummy response") + def do_GET(self): + w.write(self.path + "\n") + w.flush() + self.send_response(200) + self.send_header("Content-Type", "text/plain") + self.end_headers() + self.wfile.write(b"Dummy response") def runtest(r, *args): - form = get_form(*args) + form = get_form(*args) - tmpfile, tmpname = tempfile.mkstemp(".html") - tmpfile = os.fdopen(tmpfile, 'w') - tmpfile.write(form) - tmpfile.close() + tmpfile, tmpname = tempfile.mkstemp(".html") + tmpfile = os.fdopen(tmpfile, 'w') + tmpfile.write(form) + tmpfile.close() - linkspid = os.spawnlp(os.P_NOWAIT, 'elinks', 'elinks', - '-config-dir', os.getcwd(), - '-config-file', 'crlf.conf', - '-no-connect', '1', - '-auto-submit', '1', - tmpname) - path = r.readline() - os.kill(linkspid, signal.SIGINT) - os.waitpid(linkspid, 0) + linkspid = os.spawnlp(os.P_NOWAIT, 'elinks', 'elinks', + '-config-dir', os.getcwd(), + '-config-file', 'crlf.conf', + '-no-connect', '1', + '-auto-submit', '1', + tmpname) + path = r.readline() + os.kill(linkspid, signal.SIGINT) + os.waitpid(linkspid, 0) - os.unlink(tmpname) + os.unlink(tmpname) - return path + return path pid = os.fork() if pid: - os.close(w) - r = os.fdopen(r) + os.close(w) + r = os.fdopen(r) - paths = [] + paths = [] - for c in [C_CR, C_LF, C_CRLF]: - for e in [E_Raw, E_Entity, E_JavaScript]: - for f in [F_Hidden, F_TextArea]: - paths.append(("%d %d %d " % (c, e, f)) + runtest(r, c, e, f)) + for c in [C_CR, C_LF, C_CRLF]: + for e in [E_Raw, E_Entity, E_JavaScript]: + for f in [F_Hidden, F_TextArea]: + paths.append(("%d %d %d " % (c, e, f)) + runtest(r, c, e, f)) - for path in paths: - print path, + for path in paths: + print(path, end=' ') - os.kill(pid, signal.SIGTERM) - os.waitpid(pid, 0) + os.kill(pid, signal.SIGTERM) + os.waitpid(pid, 0) else: - os.close(r) - w = os.fdopen(w, 'w') - server_address = ('127.0.0.1', 8090) - httpd = HTTPServer(server_address, forwarder) - httpd.serve_forever() + os.close(r) + w = os.fdopen(w, 'w') + server_address = ('127.0.0.1', 8090) + httpd = HTTPServer(server_address, forwarder) + httpd.serve_forever() diff --git a/test/server/w32.py b/test/server/w32.py index 5758f283..b445bc88 100755 --- a/test/server/w32.py +++ b/test/server/w32.py @@ -1,32 +1,32 @@ -#!/usr/bin/env python -import gzip, string, cStringIO, os, time, BaseHTTPServer +#!/usr/bin/env python3 +import gzip, string, six, os, time, http.server -data = [str(i) for i in xrange(34000)] +data = [str(i) for i in range(34000)] text = "\n".join(data) -s = cStringIO.StringIO() +s = six.BytesIO() gz = gzip.GzipFile("1.gz", "wb", 9, s) -gz.write(text) +gz.write(bytes(text, 'iso8859-1')) gz.close() comp = s.getvalue() s.close() pocz = comp[:65536] reszta = comp[65536:] -class obsluga(BaseHTTPServer.BaseHTTPRequestHandler): - def do_GET(self): - self.send_response(200) - self.send_header("Content-Type", "text/plain") - self.send_header("Content-Encoding", "gzip") - self.send_header("Connection", "close") - self.end_headers() - self.wfile.write(pocz) - time.sleep(5) - self.wfile.write(reszta) +class obsluga(http.server.BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header("Content-Type", "text/plain") + self.send_header("Content-Encoding", "gzip") + self.send_header("Connection", "close") + self.end_headers() + self.wfile.write(pocz) + time.sleep(5) + self.wfile.write(reszta) def run(port=8900): - server_address = ('127.0.0.1', port) - httpd = BaseHTTPServer.HTTPServer(server_address, obsluga) - httpd.handle_request() + server_address = ('127.0.0.1', port) + httpd = http.server.HTTPServer(server_address, obsluga) + httpd.handle_request() if __name__ == "__main__": - run() + run()