diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-01-04 02:38:18 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-01-04 02:38:18 +0100 |
commit | 9b2f2b4759c98008f9435eb5b14890ae73bb1900 (patch) | |
tree | b46cf8dff05f1250ab9dfe19e76f39ba7bd25c45 | |
parent | 71285a3869392022163d4c1a2a353a8408ab76f1 (diff) | |
download | trollius-9b2f2b4759c98008f9435eb5b14890ae73bb1900.tar.gz |
Port some examples on Python 2
-rw-r--r-- | TODO | 6 | ||||
-rw-r--r-- | examples/child_process.py | 16 | ||||
-rw-r--r-- | examples/fetch0.py | 9 | ||||
-rw-r--r-- | examples/fetch1.py | 22 | ||||
-rw-r--r-- | examples/fetch2.py | 50 | ||||
-rw-r--r-- | examples/fetch3.py | 78 | ||||
-rw-r--r-- | examples/hello_coroutine.py | 2 | ||||
-rw-r--r-- | examples/simple_tcp_server.py | 23 | ||||
-rw-r--r-- | examples/sink.py | 8 | ||||
-rw-r--r-- | examples/source.py | 7 | ||||
-rw-r--r-- | examples/source1.py | 20 | ||||
-rw-r--r-- | examples/stacks.py | 4 | ||||
-rw-r--r-- | examples/timing_tcp_server.py | 23 | ||||
-rw-r--r-- | runtests.py | 1 |
14 files changed, 158 insertions, 111 deletions
@@ -5,6 +5,12 @@ - test_queues.py - test_tasks.py +* Fix examples: + + - cacheclt/cachesrv.py + - source1.py: 'StreamWriter' object has no attribute '_writer' + - stacks.py: 'exceptions.ZeroDivisionError' object has no attribute '__traceback__' + * as_completed() is a coroutine or not? * Move asyncio/coroutine.py in asyncio/tasks.py? * Fix coroutine._DEBUG=True diff --git a/examples/child_process.py b/examples/child_process.py index 4410414..76bd04d 100644 --- a/examples/child_process.py +++ b/examples/child_process.py @@ -31,8 +31,8 @@ else: def connect_write_pipe(file): loop = asyncio.get_event_loop() protocol = protocols.Protocol() - transport, _ = yield from loop.connect_write_pipe(asyncio.Protocol, file) - return transport + transport, _ = yield loop.connect_write_pipe(asyncio.Protocol, file) + raise asyncio.Return(transport) # # Wrap a readable pipe in a stream @@ -44,8 +44,8 @@ def connect_read_pipe(file): stream_reader = streams.StreamReader(loop=loop) def factory(): return streams.StreamReaderProtocol(stream_reader) - transport, _ = yield from loop.connect_read_pipe(factory, file) - return stream_reader, transport + transport, _ = yield loop.connect_read_pipe(factory, file) + raise asyncio.Return((stream_reader, transport)) # @@ -82,9 +82,9 @@ def main(loop): p = Popen([sys.executable, '-c', code], stdin=PIPE, stdout=PIPE, stderr=PIPE) - stdin = yield from connect_write_pipe(p.stdin) - stdout, stdout_transport = yield from connect_read_pipe(p.stdout) - stderr, stderr_transport = yield from connect_read_pipe(p.stderr) + stdin = yield connect_write_pipe(p.stdin) + stdout, stdout_transport = yield connect_read_pipe(p.stdout) + stderr, stderr_transport = yield connect_read_pipe(p.stderr) # interact with subprocess name = {stdout:'OUT', stderr:'ERR'} @@ -102,7 +102,7 @@ def main(loop): # get and print lines from stdout, stderr timeout = None while registered: - done, pending = yield from asyncio.wait( + done, pending = yield asyncio.wait( registered, timeout=timeout, return_when=asyncio.FIRST_COMPLETED) if not done: diff --git a/examples/fetch0.py b/examples/fetch0.py index 180fcf2..371acfb 100644 --- a/examples/fetch0.py +++ b/examples/fetch0.py @@ -1,5 +1,6 @@ """Simplest possible HTTP client.""" +from __future__ import print_function import sys from asyncio import * @@ -7,19 +8,19 @@ from asyncio import * @coroutine def fetch(): - r, w = yield from open_connection('python.org', 80) + r, w = yield open_connection('python.org', 80) request = 'GET / HTTP/1.0\r\n\r\n' print('>', request, file=sys.stderr) w.write(request.encode('latin-1')) while True: - line = yield from r.readline() + line = yield r.readline() line = line.decode('latin-1').rstrip() if not line: break print('<', line, file=sys.stderr) print(file=sys.stderr) - body = yield from r.read() - return body + body = yield r.read() + raise Return(body) def main(): diff --git a/examples/fetch1.py b/examples/fetch1.py index 8dbb6e4..fdf559d 100644 --- a/examples/fetch1.py +++ b/examples/fetch1.py @@ -3,8 +3,12 @@ This version adds URL parsing (including SSL) and a Response object. """ +from __future__ import print_function import sys -import urllib.parse +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse from asyncio import * @@ -22,13 +26,13 @@ class Response: def read(self, reader): @coroutine def getline(): - return (yield from reader.readline()).decode('latin-1').rstrip() - status_line = yield from getline() + raise Return((yield reader.readline()).decode('latin-1').rstrip()) + status_line = yield getline() if self.verbose: print('<', status_line, file=sys.stderr) self.http_version, status, self.reason = status_line.split(None, 2) self.status = int(status) while True: - header_line = yield from getline() + header_line = yield getline() if not header_line: break if self.verbose: print('<', header_line, file=sys.stderr) @@ -40,7 +44,7 @@ class Response: @coroutine def fetch(url, verbose=True): - parts = urllib.parse.urlparse(url) + parts = urlparse(url) if parts.scheme == 'http': ssl = False elif parts.scheme == 'https': @@ -57,12 +61,12 @@ def fetch(url, verbose=True): request = 'GET %s HTTP/1.0\r\n\r\n' % path if verbose: print('>', request, file=sys.stderr, end='') - r, w = yield from open_connection(parts.hostname, port, ssl=ssl) + r, w = yield open_connection(parts.hostname, port, ssl=ssl) w.write(request.encode('latin-1')) response = Response(verbose) - yield from response.read(r) - body = yield from r.read() - return body + yield response.read(r) + body = yield r.read() + raise Return(body) def main(): diff --git a/examples/fetch2.py b/examples/fetch2.py index 7617b59..4096117 100644 --- a/examples/fetch2.py +++ b/examples/fetch2.py @@ -3,9 +3,15 @@ This version adds a Request object. """ +from __future__ import print_function import sys -import urllib.parse -from http.client import BadStatusLine +try: + from urllib.parse import urlparse + from http.client import BadStatusLine +except ImportError: + # Python 2 + from urlparse import urlparse + from httplib import BadStatusLine from asyncio import * @@ -15,7 +21,7 @@ class Request: def __init__(self, url, verbose=True): self.url = url self.verbose = verbose - self.parts = urllib.parse.urlparse(self.url) + self.parts = urlparse(self.url) self.scheme = self.parts.scheme assert self.scheme in ('http', 'https'), repr(url) self.ssl = self.parts.scheme == 'https' @@ -40,9 +46,9 @@ class Request: print('* Connecting to %s:%s using %s' % (self.hostname, self.port, 'ssl' if self.ssl else 'tcp'), file=sys.stderr) - self.reader, self.writer = yield from open_connection(self.hostname, - self.port, - ssl=self.ssl) + self.reader, self.writer = yield open_connection(self.hostname, + self.port, + ssl=self.ssl) if self.verbose: print('* Connected to %s' % (self.writer.get_extra_info('peername'),), @@ -67,8 +73,8 @@ class Request: @coroutine def get_response(self): response = Response(self.reader, self.verbose) - yield from response.read_headers() - return response + yield response.read_headers() + raise Return(response) class Response: @@ -83,11 +89,11 @@ class Response: @coroutine def getline(self): - return (yield from self.reader.readline()).decode('latin-1').rstrip() + raise Return((yield self.reader.readline()).decode('latin-1').rstrip()) @coroutine def read_headers(self): - status_line = yield from self.getline() + status_line = yield self.getline() if self.verbose: print('<', status_line, file=sys.stderr) status_parts = status_line.split(None, 2) if len(status_parts) != 3: @@ -95,7 +101,7 @@ class Response: self.http_version, status, self.reason = status_parts self.status = int(status) while True: - header_line = yield from self.getline() + header_line = yield self.getline() if not header_line: break if self.verbose: print('<', header_line, file=sys.stderr) @@ -112,20 +118,20 @@ class Response: nbytes = int(value) break if nbytes is None: - body = yield from self.reader.read() + body = yield self.reader.read() else: - body = yield from self.reader.readexactly(nbytes) - return body + body = yield self.reader.readexactly(nbytes) + raise Return(body) @coroutine def fetch(url, verbose=True): request = Request(url, verbose) - yield from request.connect() - yield from request.send_request() - response = yield from request.get_response() - body = yield from response.read() - return body + yield request.connect() + yield request.send_request() + response = yield request.get_response() + body = yield response.read() + raise Return(body) def main(): @@ -134,7 +140,11 @@ def main(): body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv)) finally: loop.close() - sys.stdout.buffer.write(body) + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(body) + else: + # Python 2 + sys.stdout.write(body) if __name__ == '__main__': diff --git a/examples/fetch3.py b/examples/fetch3.py index 780222b..2e4b466 100644 --- a/examples/fetch3.py +++ b/examples/fetch3.py @@ -4,9 +4,15 @@ This version adds a primitive connection pool, redirect following and chunked transfer-encoding. It also supports a --iocp flag. """ +from __future__ import print_function import sys -import urllib.parse -from http.client import BadStatusLine +try: + from urllib.parse import urlparse + from http.client import BadStatusLine +except ImportError: + # Python 2 + from urlparse import urlparse + from httplib import BadStatusLine from asyncio import * @@ -25,25 +31,29 @@ class ConnectionPool: @coroutine def open_connection(self, host, port, ssl): port = port or (443 if ssl else 80) - ipaddrs = yield from get_event_loop().getaddrinfo(host, port) + ipaddrs = yield get_event_loop().getaddrinfo(host, port) if self.verbose: print('* %s resolves to %s' % (host, ', '.join(ip[4][0] for ip in ipaddrs)), file=sys.stderr) - for _, _, _, _, (h, p, *_) in ipaddrs: + for _, _, _, _, parts in ipaddrs: + h = parts[0] + p = parts[1] key = h, p, ssl conn = self.connections.get(key) if conn: if self.verbose: print('* Reusing pooled connection', key, file=sys.stderr) - return conn - reader, writer = yield from open_connection(host, port, ssl=ssl) - host, port, *_ = writer.get_extra_info('peername') + raise Return(conn) + reader, writer = yield open_connection(host, port, ssl=ssl) + parts = writer.get_extra_info('peername') + host = parts[0] + port = parts[1] key = host, port, ssl self.connections[key] = reader, writer if self.verbose: print('* New connection', key, file=sys.stderr) - return reader, writer + raise Return((reader, writer)) class Request: @@ -51,7 +61,7 @@ class Request: def __init__(self, url, verbose=True): self.url = url self.verbose = verbose - self.parts = urllib.parse.urlparse(self.url) + self.parts = urlparse(self.url) self.scheme = self.parts.scheme assert self.scheme in ('http', 'https'), repr(url) self.ssl = self.parts.scheme == 'https' @@ -79,7 +89,7 @@ class Request: self.vprint('* Connecting to %s:%s using %s' % (self.hostname, self.port, 'ssl' if self.ssl else 'tcp')) self.reader, self.writer = \ - yield from pool.open_connection(self.hostname, + yield pool.open_connection(self.hostname, self.port, ssl=self.ssl) self.vprint('* Connected to %s' % @@ -89,24 +99,24 @@ class Request: def putline(self, line): self.vprint('>', line) self.writer.write(line.encode('latin-1') + b'\r\n') - ##yield from self.writer.drain() + ##yield self.writer.drain() @coroutine def send_request(self): request = '%s %s %s' % (self.method, self.full_path, self.http_version) - yield from self.putline(request) + yield self.putline(request) if 'host' not in {key.lower() for key, _ in self.headers}: self.headers.insert(0, ('Host', self.netloc)) for key, value in self.headers: line = '%s: %s' % (key, value) - yield from self.putline(line) - yield from self.putline('') + yield self.putline(line) + yield self.putline('') @coroutine def get_response(self): response = Response(self.reader, self.verbose) - yield from response.read_headers() - return response + yield response.read_headers() + raise Return(response) class Response: @@ -125,20 +135,20 @@ class Response: @coroutine def getline(self): - line = (yield from self.reader.readline()).decode('latin-1').rstrip() + line = (yield self.reader.readline()).decode('latin-1').rstrip() self.vprint('<', line) - return line + raise Return(line) @coroutine def read_headers(self): - status_line = yield from self.getline() + status_line = yield self.getline() status_parts = status_line.split(None, 2) if len(status_parts) != 3: raise BadStatusLine(status_line) self.http_version, status, self.reason = status_parts self.status = int(status) while True: - header_line = yield from self.getline() + header_line = yield self.getline() if not header_line: break # TODO: Continuation lines. @@ -168,24 +178,24 @@ class Response: if self.get_header('transfer-encoding', '').lower() == 'chunked': blocks = [] while True: - size_header = yield from self.reader.readline() + size_header = yield self.reader.readline() if not size_header: break parts = size_header.split(b';') size = int(parts[0], 16) if not size: break - block = yield from self.reader.readexactly(size) + block = yield self.reader.readexactly(size) assert len(block) == size, (len(block), size) blocks.append(block) - crlf = yield from self.reader.readline() + crlf = yield self.reader.readline() assert crlf == b'\r\n' body = b''.join(blocks) else: - body = yield from self.reader.read() + body = yield self.reader.read() else: - body = yield from self.reader.readexactly(nbytes) - return body + body = yield self.reader.readexactly(nbytes) + raise Return(body) @coroutine @@ -194,16 +204,16 @@ def fetch(url, verbose=True, max_redirect=10): try: for _ in range(max_redirect): request = Request(url, verbose) - yield from request.connect(pool) - yield from request.send_request() - response = yield from request.get_response() - body = yield from response.read() + yield request.connect(pool) + yield request.send_request() + response = yield request.get_response() + body = yield response.read() next_url = response.get_redirect_url() if not next_url: break url = urllib.parse.urljoin(url, next_url) print('redirect to', url, file=sys.stderr) - return body + raise Return(body) finally: pool.close() @@ -219,7 +229,11 @@ def main(): body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv)) finally: loop.close() - sys.stdout.buffer.write(body) + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.write(body) + else: + # Python 2 + sys.stdout.write(body) if __name__ == '__main__': diff --git a/examples/hello_coroutine.py b/examples/hello_coroutine.py index 8ad682d..e2c03dc 100644 --- a/examples/hello_coroutine.py +++ b/examples/hello_coroutine.py @@ -7,7 +7,7 @@ import asyncio def greet_every_two_seconds(): while True: print('Hello World') - yield from asyncio.sleep(2) + yield asyncio.sleep(2) if __name__ == '__main__': diff --git a/examples/simple_tcp_server.py b/examples/simple_tcp_server.py index c36710d..b05052b 100644 --- a/examples/simple_tcp_server.py +++ b/examples/simple_tcp_server.py @@ -8,6 +8,7 @@ in the same process. It listens on port 1234 on 127.0.0.1, so it will fail if this port is currently in use. """ +from __future__ import print_function import sys import asyncio import asyncio.streams @@ -58,10 +59,12 @@ class MyServer: out one or more lines back to the client with the result. """ while True: - data = (yield from client_reader.readline()).decode("utf-8") + data = (yield client_reader.readline()).decode("utf-8") if not data: # an empty string means the client disconnected break - cmd, *args = data.rstrip().split(' ') + parts = data.rstrip().split(' ') + cmd = parts[0] + args = parts[1:] if cmd == 'add': arg1 = float(args[0]) arg2 = float(args[1]) @@ -79,7 +82,7 @@ class MyServer: print("Bad command {!r}".format(data), file=sys.stderr) # This enables us to have flow control in our connection. - yield from client_writer.drain() + yield client_writer.drain() def start(self, loop): """ @@ -115,7 +118,7 @@ def main(): @asyncio.coroutine def client(): - reader, writer = yield from asyncio.streams.open_connection( + reader, writer = yield asyncio.streams.open_connection( '127.0.0.1', 12345, loop=loop) def send(msg): @@ -123,24 +126,24 @@ def main(): writer.write((msg + '\n').encode("utf-8")) def recv(): - msgback = (yield from reader.readline()).decode("utf-8").rstrip() + msgback = (yield reader.readline()).decode("utf-8").rstrip() print("< " + msgback) - return msgback + raise asyncio.Return(msgback) # send a line send("add 1 2") - msg = yield from recv() + msg = yield recv() send("repeat 5 hello") - msg = yield from recv() + msg = yield recv() assert msg == 'begin' while True: - msg = yield from recv() + msg = yield recv() if msg == 'end': break writer.close() - yield from asyncio.sleep(0.5) + yield asyncio.sleep(0.5) # creates a client and connects to our server msg = loop.run_until_complete(client()) diff --git a/examples/sink.py b/examples/sink.py index d362cbb..b3ac3e8 100644 --- a/examples/sink.py +++ b/examples/sink.py @@ -1,5 +1,6 @@ """Test service that accepts connections and reads all data off them.""" +from __future__ import print_function import argparse import os import sys @@ -64,15 +65,16 @@ def start(loop, host, port): # TODO: take cert/key from args as well. here = os.path.join(os.path.dirname(__file__), '..', 'tests') sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) - sslctx.options |= ssl.OP_NO_SSLv2 + if hasattr(sslctx, 'options'): + sslctx.options |= ssl.OP_NO_SSLv2 sslctx.load_cert_chain( certfile=os.path.join(here, 'ssl_cert.pem'), keyfile=os.path.join(here, 'ssl_key.pem')) - server = yield from loop.create_server(Service, host, port, ssl=sslctx) + server = yield loop.create_server(Service, host, port, ssl=sslctx) dprint('serving TLS' if sslctx else 'serving', [s.getsockname() for s in server.sockets]) - yield from server.wait_closed() + yield server.wait_closed() def main(): diff --git a/examples/source.py b/examples/source.py index 7fd11fb..38f678a 100644 --- a/examples/source.py +++ b/examples/source.py @@ -1,5 +1,6 @@ """Test client that connects and sends infinite data.""" +from __future__ import print_function import argparse import sys @@ -74,11 +75,11 @@ def start(loop, host, port): sslctx = None if args.tls: sslctx = test_utils.dummy_ssl_context() - tr, pr = yield from loop.create_connection(Client, host, port, - ssl=sslctx) + tr, pr = yield loop.create_connection(Client, host, port, + ssl=sslctx) dprint('tr =', tr) dprint('pr =', pr) - yield from pr.waiter + yield pr.waiter def main(): diff --git a/examples/source1.py b/examples/source1.py index 6802e96..5316506 100644 --- a/examples/source1.py +++ b/examples/source1.py @@ -1,5 +1,6 @@ """Like source.py, but uses streams.""" +from __future__ import print_function import argparse import sys @@ -33,7 +34,7 @@ class Debug: overwriting = False label = 'stream1:' - def print(self, *args): + def print_(self, *args): if self.overwriting: print(file=sys.stderr) self.overwriting = 0 @@ -46,7 +47,8 @@ class Debug: if self.overwriting == 3: print(self.label, '[...]', file=sys.stderr) end = '\r' - print(self.label, *args, file=sys.stderr, end=end, flush=True) + print(self.label, *args, file=sys.stderr, end=end) + sys.stdout.flush() @coroutine @@ -55,11 +57,11 @@ def start(loop, args): total = 0 sslctx = None if args.tls: - d.print('using dummy SSLContext') + d.print_('using dummy SSLContext') sslctx = test_utils.dummy_ssl_context() - r, w = yield from open_connection(args.host, args.port, ssl=sslctx) - d.print('r =', r) - d.print('w =', w) + r, w = yield open_connection(args.host, args.port, ssl=sslctx) + d.print_('r =', r) + d.print_('w =', w) if args.stop: w.write(b'stop') w.close() @@ -73,10 +75,10 @@ def start(loop, args): w.write(data) f = w.drain() if f: - d.print('pausing') - yield from f + d.print_('pausing') + yield f except (ConnectionResetError, BrokenPipeError) as exc: - d.print('caught', repr(exc)) + d.print_('caught', repr(exc)) def main(): diff --git a/examples/stacks.py b/examples/stacks.py index 371d31f..eff73c3 100644 --- a/examples/stacks.py +++ b/examples/stacks.py @@ -10,9 +10,9 @@ def helper(r): for t in Task.all_tasks(): t.print_stack() print('--- end helper ---') - line = yield from r.readline() + line = yield r.readline() 1/0 - return line + raise Return(line) def doit(): l = get_event_loop() diff --git a/examples/timing_tcp_server.py b/examples/timing_tcp_server.py index 6f0483e..6755ab1 100644 --- a/examples/timing_tcp_server.py +++ b/examples/timing_tcp_server.py @@ -8,6 +8,7 @@ in the same process. It listens on port 1234 on 127.0.0.1, so it will fail if this port is currently in use. """ +from __future__ import print_function import sys import time import random @@ -61,10 +62,12 @@ class MyServer: out one or more lines back to the client with the result. """ while True: - data = (yield from client_reader.readline()).decode("utf-8") + data = (yield client_reader.readline()).decode("utf-8") if not data: # an empty string means the client disconnected break - cmd, *args = data.rstrip().split(' ') + parts = data.rstrip().split(' ') + cmd = parts[0] + args = parts[1:] if cmd == 'add': arg1 = float(args[0]) arg2 = float(args[1]) @@ -83,7 +86,7 @@ class MyServer: print("Bad command {!r}".format(data), file=sys.stderr) # This enables us to have flow control in our connection. - yield from client_writer.drain() + yield client_writer.drain() def start(self, loop): """ @@ -119,7 +122,7 @@ def main(): @asyncio.coroutine def client(): - reader, writer = yield from asyncio.streams.open_connection( + reader, writer = yield asyncio.streams.open_connection( '127.0.0.1', 12345, loop=loop) def send(msg): @@ -127,13 +130,13 @@ def main(): writer.write((msg + '\n').encode("utf-8")) def recv(): - msgback = (yield from reader.readline()).decode("utf-8").rstrip() + msgback = (yield reader.readline()).decode("utf-8").rstrip() print("< " + msgback) - return msgback + raise asyncio.Return(msgback) # send a line send("add 1 2") - msg = yield from recv() + msg = yield recv() Ns = list(range(100, 100000, 10000)) times = [] @@ -141,10 +144,10 @@ def main(): for N in Ns: t0 = time.time() send("repeat {} hello world ".format(N)) - msg = yield from recv() + msg = yield recv() assert msg == 'begin' while True: - msg = (yield from reader.readline()).decode("utf-8").rstrip() + msg = (yield reader.readline()).decode("utf-8").rstrip() if msg == 'end': break t1 = time.time() @@ -154,7 +157,7 @@ def main(): times.append(dt) writer.close() - yield from asyncio.sleep(0.5) + yield asyncio.sleep(0.5) # creates a client and connects to our server msg = loop.run_until_complete(client()) diff --git a/runtests.py b/runtests.py index 469ac86..59710b8 100644 --- a/runtests.py +++ b/runtests.py @@ -19,6 +19,7 @@ runtests.py --coverage is equivalent of: # Originally written by Beech Horn (for NDB). +from __future__ import print_function import argparse import gc import logging |