Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4eea7adb8f | ||
|
|
a3288a63ed | ||
|
|
3bd7fe8cea | ||
|
|
0ad538df91 | ||
|
|
b810346aa4 | ||
|
|
ae5d330b2d | ||
|
|
4c0afa2bec | ||
|
|
125af4b4a9 | ||
|
|
c5e1873523 | ||
|
|
dfbe2edd79 | ||
|
|
3e29af5775 | ||
|
|
1aacb3cf46 |
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
tests/files/* binary
|
||||||
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
github: miguelgrinberg
|
||||||
|
patreon: miguelgrinberg
|
||||||
|
custom: https://paypal.me/miguelgrinberg
|
||||||
53
.github/workflows/tests.yml
vendored
Normal file
53
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
name: build
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
- run: python -m pip install --upgrade pip wheel
|
||||||
|
- run: pip install tox tox-gh-actions
|
||||||
|
- run: tox -eflake8
|
||||||
|
tests:
|
||||||
|
name: tests
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
|
python: ['3.6', '3.7', '3.8', '3.9']
|
||||||
|
fail-fast: false
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python }}
|
||||||
|
- run: python -m pip install --upgrade pip wheel
|
||||||
|
- run: pip install tox tox-gh-actions
|
||||||
|
- run: tox
|
||||||
|
tests-micropython:
|
||||||
|
name: tests-micropython
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
- run: python -m pip install --upgrade pip wheel
|
||||||
|
- run: pip install tox tox-gh-actions
|
||||||
|
- run: tox -eupy
|
||||||
|
coverage:
|
||||||
|
name: coverage
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
- run: python -m pip install --upgrade pip wheel
|
||||||
|
- run: pip install tox tox-gh-actions codecov
|
||||||
|
- run: tox
|
||||||
|
- run: codecov
|
||||||
18
.travis.yml
18
.travis.yml
@@ -1,18 +0,0 @@
|
|||||||
dist: xenial
|
|
||||||
language: python
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- python: 3.7
|
|
||||||
env: TOXENV=flake8
|
|
||||||
- python: 3.5
|
|
||||||
env: TOXENV=py35
|
|
||||||
- python: 3.6
|
|
||||||
env: TOXENV=py36
|
|
||||||
- python: 3.7
|
|
||||||
env: TOXENV=py37
|
|
||||||
- python: 3.7
|
|
||||||
env: TOXENV=upy
|
|
||||||
install:
|
|
||||||
- pip install tox
|
|
||||||
script:
|
|
||||||
- tox
|
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
# microdot
|
# microdot
|
||||||
[](https://travis-ci.org/miguelgrinberg/microdot)
|
[](https://github.com/miguelgrinberg/microdot/actions) [](https://codecov.io/gh/miguelgrinberg/microdot)
|
||||||
|
|
||||||
A minimalistic Python web framework for microcontrollers inspired by Flask
|
A minimalistic Python web framework for microcontrollers inspired by Flask
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
Coming soon!
|
||||||
|
|||||||
BIN
bin/micropython
BIN
bin/micropython
Binary file not shown.
@@ -15,7 +15,7 @@ set -e
|
|||||||
for PKG in microdot*; do
|
for PKG in microdot*; do
|
||||||
echo Building $PKG...
|
echo Building $PKG...
|
||||||
cd $PKG
|
cd $PKG
|
||||||
sed -i "s/version.*$/version=\"$VERSION\",/" setup.py
|
sed -i "" "s/version.*$/version=\"$VERSION\",/" setup.py
|
||||||
git add setup.py
|
git add setup.py
|
||||||
rm -rf dist
|
rm -rf dist
|
||||||
python setup.py sdist bdist_wheel --universal
|
python setup.py sdist bdist_wheel --universal
|
||||||
|
|||||||
32
examples/hello.py
Normal file
32
examples/hello.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from microdot import Microdot, Response
|
||||||
|
|
||||||
|
app = Microdot()
|
||||||
|
|
||||||
|
htmldoc = '''<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Microdot Example Page</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h1>Microdot Example Page</h1>
|
||||||
|
<p>Hello from Microdot!</p>
|
||||||
|
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def hello(request):
|
||||||
|
return Response(body=htmldoc, headers={'Content-Type': 'text/html'})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/shutdown')
|
||||||
|
def shutdown(request):
|
||||||
|
request.app.shutdown()
|
||||||
|
return 'The server is shutting down...'
|
||||||
|
|
||||||
|
|
||||||
|
app.run(debug=True)
|
||||||
40
examples/hello_async.py
Normal file
40
examples/hello_async.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
try:
|
||||||
|
import uasyncio as asyncio
|
||||||
|
except ImportError:
|
||||||
|
import asyncio
|
||||||
|
from microdot_asyncio import Microdot, Response
|
||||||
|
|
||||||
|
app = Microdot()
|
||||||
|
|
||||||
|
htmldoc = '''<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Microdot Example Page</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h1>Microdot Example Page</h1>
|
||||||
|
<p>Hello from Microdot!</p>
|
||||||
|
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
async def hello(request):
|
||||||
|
return Response(body=htmldoc, headers={'Content-Type': 'text/html'})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/shutdown')
|
||||||
|
async def shutdown(request):
|
||||||
|
request.app.shutdown()
|
||||||
|
return 'The server is shutting down...'
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await app.start_server(debug=True)
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
@@ -14,9 +14,11 @@ def _iscoroutine(coro):
|
|||||||
|
|
||||||
class Request(BaseRequest):
|
class Request(BaseRequest):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def create(stream, client_addr):
|
async def create(app, stream, client_addr):
|
||||||
# request line
|
# request line
|
||||||
line = (await stream.readline()).strip().decode()
|
line = (await stream.readline()).strip().decode()
|
||||||
|
if not line: # pragma: no cover
|
||||||
|
return None
|
||||||
method, url, http_version = line.split()
|
method, url, http_version = line.split()
|
||||||
http_version = http_version.split('/', 1)[1]
|
http_version = http_version.split('/', 1)[1]
|
||||||
|
|
||||||
@@ -37,7 +39,8 @@ class Request(BaseRequest):
|
|||||||
body = await stream.read(content_length) \
|
body = await stream.read(content_length) \
|
||||||
if content_length else b''
|
if content_length else b''
|
||||||
|
|
||||||
return Request(client_addr, method, url, http_version, headers, body)
|
return Request(app, client_addr, method, url, http_version, headers,
|
||||||
|
body)
|
||||||
|
|
||||||
|
|
||||||
class Response(BaseResponse):
|
class Response(BaseResponse):
|
||||||
@@ -59,11 +62,21 @@ class Response(BaseResponse):
|
|||||||
|
|
||||||
# body
|
# body
|
||||||
if self.body:
|
if self.body:
|
||||||
await stream.awrite(self.body)
|
if hasattr(self.body, 'read'):
|
||||||
|
while True:
|
||||||
|
buf = self.body.read(self.send_file_buffer_size)
|
||||||
|
if len(buf):
|
||||||
|
await stream.awrite(buf)
|
||||||
|
if len(buf) < self.send_file_buffer_size:
|
||||||
|
break
|
||||||
|
if hasattr(self.body, 'close'):
|
||||||
|
self.body.close()
|
||||||
|
else:
|
||||||
|
await stream.awrite(self.body)
|
||||||
|
|
||||||
|
|
||||||
class Microdot(BaseMicrodot):
|
class Microdot(BaseMicrodot):
|
||||||
def run(self, host='0.0.0.0', port=5000, debug=False):
|
async def start_server(self, host='0.0.0.0', port=5000, debug=False):
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
|
|
||||||
async def serve(reader, writer):
|
async def serve(reader, writer):
|
||||||
@@ -86,55 +99,65 @@ class Microdot(BaseMicrodot):
|
|||||||
if self.debug: # pragma: no cover
|
if self.debug: # pragma: no cover
|
||||||
print('Starting async server on {host}:{port}...'.format(
|
print('Starting async server on {host}:{port}...'.format(
|
||||||
host=host, port=port))
|
host=host, port=port))
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
loop.run_until_complete(asyncio.start_server(serve, host, port))
|
self.server = await asyncio.start_server(serve, host, port)
|
||||||
loop.run_forever()
|
await self.server.wait_closed()
|
||||||
loop.close() # pragma: no cover
|
|
||||||
|
def run(self, host='0.0.0.0', port=5000, debug=False):
|
||||||
|
asyncio.run(self.start_server(host=host, port=port, debug=debug))
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
self.server.close()
|
||||||
|
|
||||||
async def dispatch_request(self, reader, writer):
|
async def dispatch_request(self, reader, writer):
|
||||||
req = await Request.create(reader, writer.get_extra_info('peername'))
|
req = await Request.create(self, reader,
|
||||||
f = self.find_route(req)
|
writer.get_extra_info('peername'))
|
||||||
try:
|
if req:
|
||||||
res = None
|
f = self.find_route(req)
|
||||||
if f:
|
try:
|
||||||
for handler in self.before_request_handlers:
|
res = None
|
||||||
res = await self._invoke_handler(handler, req)
|
if f:
|
||||||
if res:
|
for handler in self.before_request_handlers:
|
||||||
break
|
res = await self._invoke_handler(handler, req)
|
||||||
if res is None:
|
if res:
|
||||||
res = await self._invoke_handler(f, req, **req.url_args)
|
break
|
||||||
if isinstance(res, tuple):
|
if res is None:
|
||||||
res = Response(*res)
|
res = await self._invoke_handler(
|
||||||
elif not isinstance(res, Response):
|
f, req, **req.url_args)
|
||||||
res = Response(res)
|
if isinstance(res, tuple):
|
||||||
for handler in self.after_request_handlers:
|
res = Response(*res)
|
||||||
res = await self._invoke_handler(handler, req, res) or res
|
elif not isinstance(res, Response):
|
||||||
elif 404 in self.error_handlers:
|
res = Response(res)
|
||||||
res = await self._invoke_handler(self.error_handlers[404], req)
|
for handler in self.after_request_handlers:
|
||||||
else:
|
res = await self._invoke_handler(
|
||||||
res = 'Not found', 404
|
handler, req, res) or res
|
||||||
except Exception as exc:
|
elif 404 in self.error_handlers:
|
||||||
print_exception(exc)
|
|
||||||
res = None
|
|
||||||
if exc.__class__ in self.error_handlers:
|
|
||||||
try:
|
|
||||||
res = await self._invoke_handler(
|
res = await self._invoke_handler(
|
||||||
self.error_handlers[exc.__class__], req, exc)
|
self.error_handlers[404], req)
|
||||||
except Exception as exc2: # pragma: no cover
|
|
||||||
print_exception(exc2)
|
|
||||||
if res is None:
|
|
||||||
if 500 in self.error_handlers:
|
|
||||||
res = await self._invoke_handler(self.error_handlers[500],
|
|
||||||
req)
|
|
||||||
else:
|
else:
|
||||||
res = 'Internal server error', 500
|
res = 'Not found', 404
|
||||||
if isinstance(res, tuple):
|
except Exception as exc:
|
||||||
res = Response(*res)
|
print_exception(exc)
|
||||||
elif not isinstance(res, Response):
|
res = None
|
||||||
res = Response(res)
|
if exc.__class__ in self.error_handlers:
|
||||||
await res.write(writer)
|
try:
|
||||||
|
res = await self._invoke_handler(
|
||||||
|
self.error_handlers[exc.__class__], req, exc)
|
||||||
|
except Exception as exc2: # pragma: no cover
|
||||||
|
print_exception(exc2)
|
||||||
|
if res is None:
|
||||||
|
if 500 in self.error_handlers:
|
||||||
|
res = await self._invoke_handler(
|
||||||
|
self.error_handlers[500], req)
|
||||||
|
else:
|
||||||
|
res = 'Internal server error', 500
|
||||||
|
if isinstance(res, tuple):
|
||||||
|
res = Response(*res)
|
||||||
|
elif not isinstance(res, Response):
|
||||||
|
res = Response(res)
|
||||||
|
await res.write(writer)
|
||||||
await writer.aclose()
|
await writer.aclose()
|
||||||
if self.debug: # pragma: no cover
|
if self.debug and req: # pragma: no cover
|
||||||
print('{method} {path} {status_code}'.format(
|
print('{method} {path} {status_code}'.format(
|
||||||
method=req.method, path=req.path,
|
method=req.method, path=req.path,
|
||||||
status_code=res.status_code))
|
status_code=res.status_code))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='microdot-asyncio',
|
name='microdot-asyncio',
|
||||||
version="0.3.0",
|
version="0.4.0",
|
||||||
url='http://github.com/miguelgrinberg/microdot/',
|
url='http://github.com/miguelgrinberg/microdot/',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
author='Miguel Grinberg',
|
author='Miguel Grinberg',
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ except ImportError: # pragma: no cover
|
|||||||
|
|
||||||
def print_exception(exc):
|
def print_exception(exc):
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
try:
|
||||||
|
import uerrno as errno
|
||||||
|
except ImportError:
|
||||||
|
import errno
|
||||||
|
|
||||||
concurrency_mode = 'threaded'
|
concurrency_mode = 'threaded'
|
||||||
|
|
||||||
@@ -43,7 +47,10 @@ except ImportError:
|
|||||||
try:
|
try:
|
||||||
import usocket as socket
|
import usocket as socket
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import socket
|
try:
|
||||||
|
import socket
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
|
socket = None
|
||||||
|
|
||||||
|
|
||||||
def urldecode(string):
|
def urldecode(string):
|
||||||
@@ -66,7 +73,9 @@ class Request():
|
|||||||
class G:
|
class G:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __init__(self, client_addr, method, url, http_version, headers, body):
|
def __init__(self, app, client_addr, method, url, http_version, headers,
|
||||||
|
body):
|
||||||
|
self.app = app
|
||||||
self.client_addr = client_addr
|
self.client_addr = client_addr
|
||||||
self.method = method
|
self.method = method
|
||||||
self.path = url
|
self.path = url
|
||||||
@@ -96,9 +105,11 @@ class Request():
|
|||||||
self.g = Request.G()
|
self.g = Request.G()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(client_stream, client_addr):
|
def create(app, client_stream, client_addr):
|
||||||
# request line
|
# request line
|
||||||
line = client_stream.readline().strip().decode()
|
line = client_stream.readline().strip().decode()
|
||||||
|
if not line: # pragma: no cover
|
||||||
|
return None
|
||||||
method, url, http_version = line.split()
|
method, url, http_version = line.split()
|
||||||
http_version = http_version.split('/', 1)[1]
|
http_version = http_version.split('/', 1)[1]
|
||||||
|
|
||||||
@@ -118,7 +129,8 @@ class Request():
|
|||||||
# body
|
# body
|
||||||
body = client_stream.read(content_length) if content_length else b''
|
body = client_stream.read(content_length) if content_length else b''
|
||||||
|
|
||||||
return Request(client_addr, method, url, http_version, headers, body)
|
return Request(app, client_addr, method, url, http_version, headers,
|
||||||
|
body)
|
||||||
|
|
||||||
def _parse_urlencoded(self, urlencoded):
|
def _parse_urlencoded(self, urlencoded):
|
||||||
return {
|
return {
|
||||||
@@ -154,6 +166,7 @@ class Response():
|
|||||||
'png': 'image/png',
|
'png': 'image/png',
|
||||||
'txt': 'text/plain',
|
'txt': 'text/plain',
|
||||||
}
|
}
|
||||||
|
send_file_buffer_size = 1024
|
||||||
|
|
||||||
def __init__(self, body='', status_code=200, headers=None):
|
def __init__(self, body='', status_code=200, headers=None):
|
||||||
self.status_code = status_code
|
self.status_code = status_code
|
||||||
@@ -163,10 +176,9 @@ class Response():
|
|||||||
self.headers['Content-Type'] = 'application/json'
|
self.headers['Content-Type'] = 'application/json'
|
||||||
elif isinstance(body, str):
|
elif isinstance(body, str):
|
||||||
self.body = body.encode()
|
self.body = body.encode()
|
||||||
elif isinstance(body, bytes):
|
|
||||||
self.body = body
|
|
||||||
else:
|
else:
|
||||||
self.body = str(body).encode()
|
# this applies to bytes or file-like objects
|
||||||
|
self.body = body
|
||||||
|
|
||||||
def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
|
def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
|
||||||
max_age=None, secure=False, http_only=False):
|
max_age=None, secure=False, http_only=False):
|
||||||
@@ -190,7 +202,8 @@ class Response():
|
|||||||
self.headers['Set-Cookie'] = [http_cookie]
|
self.headers['Set-Cookie'] = [http_cookie]
|
||||||
|
|
||||||
def complete(self):
|
def complete(self):
|
||||||
if 'Content-Length' not in self.headers:
|
if isinstance(self.body, bytes) and \
|
||||||
|
'Content-Length' not in self.headers:
|
||||||
self.headers['Content-Length'] = str(len(self.body))
|
self.headers['Content-Length'] = str(len(self.body))
|
||||||
if 'Content-Type' not in self.headers:
|
if 'Content-Type' not in self.headers:
|
||||||
self.headers['Content-Type'] = 'text/plain'
|
self.headers['Content-Type'] = 'text/plain'
|
||||||
@@ -213,7 +226,17 @@ class Response():
|
|||||||
|
|
||||||
# body
|
# body
|
||||||
if self.body:
|
if self.body:
|
||||||
stream.write(self.body)
|
if hasattr(self.body, 'read'):
|
||||||
|
while True:
|
||||||
|
buf = self.body.read(self.send_file_buffer_size)
|
||||||
|
if len(buf):
|
||||||
|
stream.write(buf)
|
||||||
|
if len(buf) < self.send_file_buffer_size:
|
||||||
|
break
|
||||||
|
if hasattr(self.body, 'close'): # pragma: no close
|
||||||
|
self.body.close()
|
||||||
|
else:
|
||||||
|
stream.write(self.body)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def redirect(cls, location, status_code=302):
|
def redirect(cls, location, status_code=302):
|
||||||
@@ -227,11 +250,9 @@ class Response():
|
|||||||
content_type = Response.types_map[ext]
|
content_type = Response.types_map[ext]
|
||||||
else:
|
else:
|
||||||
content_type = 'application/octet-stream'
|
content_type = 'application/octet-stream'
|
||||||
with open(filename) as f:
|
f = open(filename, 'rb')
|
||||||
body = f.read()
|
return cls(body=f, status_code=status_code,
|
||||||
return cls(body=body, status_code=status_code,
|
headers={'Content-Type': content_type})
|
||||||
headers={'Content-Type': content_type,
|
|
||||||
'Content-Length': str(len(body))})
|
|
||||||
|
|
||||||
|
|
||||||
class URLPattern():
|
class URLPattern():
|
||||||
@@ -292,7 +313,9 @@ class Microdot():
|
|||||||
self.before_request_handlers = []
|
self.before_request_handlers = []
|
||||||
self.after_request_handlers = []
|
self.after_request_handlers = []
|
||||||
self.error_handlers = {}
|
self.error_handlers = {}
|
||||||
|
self.shutdown_requested = False
|
||||||
self.debug = False
|
self.debug = False
|
||||||
|
self.server = None
|
||||||
|
|
||||||
def route(self, url_pattern, methods=None):
|
def route(self, url_pattern, methods=None):
|
||||||
def decorated(f):
|
def decorated(f):
|
||||||
@@ -301,6 +324,21 @@ class Microdot():
|
|||||||
return f
|
return f
|
||||||
return decorated
|
return decorated
|
||||||
|
|
||||||
|
def get(self, url_pattern):
|
||||||
|
return self.route(url_pattern, methods=['GET'])
|
||||||
|
|
||||||
|
def post(self, url_pattern):
|
||||||
|
return self.route(url_pattern, methods=['POST'])
|
||||||
|
|
||||||
|
def put(self, url_pattern):
|
||||||
|
return self.route(url_pattern, methods=['PUT'])
|
||||||
|
|
||||||
|
def patch(self, url_pattern):
|
||||||
|
return self.route(url_pattern, methods=['PATCH'])
|
||||||
|
|
||||||
|
def delete(self, url_pattern):
|
||||||
|
return self.route(url_pattern, methods=['DELETE'])
|
||||||
|
|
||||||
def before_request(self, f):
|
def before_request(self, f):
|
||||||
self.before_request_handlers.append(f)
|
self.before_request_handlers.append(f)
|
||||||
return f
|
return f
|
||||||
@@ -317,22 +355,32 @@ class Microdot():
|
|||||||
|
|
||||||
def run(self, host='0.0.0.0', port=5000, debug=False):
|
def run(self, host='0.0.0.0', port=5000, debug=False):
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
|
self.shutdown_requested = False
|
||||||
|
|
||||||
s = socket.socket()
|
self.server = socket.socket()
|
||||||
ai = socket.getaddrinfo(host, port)
|
ai = socket.getaddrinfo(host, port)
|
||||||
addr = ai[0][-1]
|
addr = ai[0][-1]
|
||||||
|
|
||||||
if self.debug: # pragma: no cover
|
if self.debug: # pragma: no cover
|
||||||
print('Starting {mode} server on {host}:{port}...'.format(
|
print('Starting {mode} server on {host}:{port}...'.format(
|
||||||
mode=concurrency_mode, host=host, port=port))
|
mode=concurrency_mode, host=host, port=port))
|
||||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
s.bind(addr)
|
self.server.bind(addr)
|
||||||
s.listen(5)
|
self.server.listen(5)
|
||||||
|
|
||||||
while True:
|
while not self.shutdown_requested:
|
||||||
sock, addr = s.accept()
|
try:
|
||||||
|
sock, addr = self.server.accept()
|
||||||
|
except OSError as exc:
|
||||||
|
if exc.args[0] == errno.ECONNABORTED:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise
|
||||||
create_thread(self.dispatch_request, sock, addr)
|
create_thread(self.dispatch_request, sock, addr)
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
self.shutdown_requested = True
|
||||||
|
|
||||||
def find_route(self, req):
|
def find_route(self, req):
|
||||||
f = None
|
f = None
|
||||||
for route_methods, route_pattern, route_handler in self.url_map:
|
for route_methods, route_pattern, route_handler in self.url_map:
|
||||||
@@ -349,49 +397,52 @@ class Microdot():
|
|||||||
else:
|
else:
|
||||||
stream = sock
|
stream = sock
|
||||||
|
|
||||||
req = Request.create(stream, addr)
|
req = Request.create(self, stream, addr)
|
||||||
f = self.find_route(req)
|
if req:
|
||||||
try:
|
f = self.find_route(req)
|
||||||
res = None
|
try:
|
||||||
if f:
|
res = None
|
||||||
for handler in self.before_request_handlers:
|
if f:
|
||||||
res = handler(req)
|
for handler in self.before_request_handlers:
|
||||||
if res:
|
res = handler(req)
|
||||||
break
|
if res:
|
||||||
if res is None:
|
break
|
||||||
res = f(req, **req.url_args)
|
if res is None:
|
||||||
if isinstance(res, tuple):
|
res = f(req, **req.url_args)
|
||||||
res = Response(*res)
|
if isinstance(res, tuple):
|
||||||
elif not isinstance(res, Response):
|
res = Response(*res)
|
||||||
res = Response(res)
|
elif not isinstance(res, Response):
|
||||||
for handler in self.after_request_handlers:
|
res = Response(res)
|
||||||
res = handler(req, res) or res
|
for handler in self.after_request_handlers:
|
||||||
elif 404 in self.error_handlers:
|
res = handler(req, res) or res
|
||||||
res = self.error_handlers[404](req)
|
elif 404 in self.error_handlers:
|
||||||
else:
|
res = self.error_handlers[404](req)
|
||||||
res = 'Not found', 404
|
|
||||||
except Exception as exc:
|
|
||||||
print_exception(exc)
|
|
||||||
res = None
|
|
||||||
if exc.__class__ in self.error_handlers:
|
|
||||||
try:
|
|
||||||
res = self.error_handlers[exc.__class__](req, exc)
|
|
||||||
except Exception as exc2: # pragma: no cover
|
|
||||||
print_exception(exc2)
|
|
||||||
if res is None:
|
|
||||||
if 500 in self.error_handlers:
|
|
||||||
res = self.error_handlers[500](req)
|
|
||||||
else:
|
else:
|
||||||
res = 'Internal server error', 500
|
res = 'Not found', 404
|
||||||
if isinstance(res, tuple):
|
except Exception as exc:
|
||||||
res = Response(*res)
|
print_exception(exc)
|
||||||
elif not isinstance(res, Response):
|
res = None
|
||||||
res = Response(res)
|
if exc.__class__ in self.error_handlers:
|
||||||
res.write(stream)
|
try:
|
||||||
|
res = self.error_handlers[exc.__class__](req, exc)
|
||||||
|
except Exception as exc2: # pragma: no cover
|
||||||
|
print_exception(exc2)
|
||||||
|
if res is None:
|
||||||
|
if 500 in self.error_handlers:
|
||||||
|
res = self.error_handlers[500](req)
|
||||||
|
else:
|
||||||
|
res = 'Internal server error', 500
|
||||||
|
if isinstance(res, tuple):
|
||||||
|
res = Response(*res)
|
||||||
|
elif not isinstance(res, Response):
|
||||||
|
res = Response(res)
|
||||||
|
res.write(stream)
|
||||||
stream.close()
|
stream.close()
|
||||||
if stream != sock: # pragma: no cover
|
if stream != sock: # pragma: no cover
|
||||||
sock.close()
|
sock.close()
|
||||||
if self.debug: # pragma: no cover
|
if self.shutdown_requested: # pragma: no cover
|
||||||
|
self.server.close()
|
||||||
|
if self.debug and req: # pragma: no cover
|
||||||
print('{method} {path} {status_code}'.format(
|
print('{method} {path} {status_code}'.format(
|
||||||
method=req.method, path=req.path,
|
method=req.method, path=req.path,
|
||||||
status_code=res.status_code))
|
status_code=res.status_code))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='microdot',
|
name='microdot',
|
||||||
version="0.3.0",
|
version="0.4.0",
|
||||||
url='http://github.com/miguelgrinberg/microdot/',
|
url='http://github.com/miguelgrinberg/microdot/',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
author='Miguel Grinberg',
|
author='Miguel Grinberg',
|
||||||
|
|||||||
@@ -1,258 +1,30 @@
|
|||||||
import uerrno
|
# MicroPython uasyncio module
|
||||||
import uselect as select
|
# MIT license; Copyright (c) 2019 Damien P. George
|
||||||
import usocket as _socket
|
|
||||||
from uasyncio.core import *
|
|
||||||
|
|
||||||
|
from .core import *
|
||||||
|
|
||||||
DEBUG = 0
|
__version__ = (3, 0, 0)
|
||||||
log = None
|
|
||||||
|
|
||||||
def set_debug(val):
|
_attrs = {
|
||||||
global DEBUG, log
|
"wait_for": "funcs",
|
||||||
DEBUG = val
|
"wait_for_ms": "funcs",
|
||||||
if val:
|
"gather": "funcs",
|
||||||
import logging
|
"Event": "event",
|
||||||
log = logging.getLogger("uasyncio")
|
"ThreadSafeFlag": "event",
|
||||||
|
"Lock": "lock",
|
||||||
|
"open_connection": "stream",
|
||||||
|
"start_server": "stream",
|
||||||
|
"StreamReader": "stream",
|
||||||
|
"StreamWriter": "stream",
|
||||||
|
}
|
||||||
|
|
||||||
|
# Lazy loader, effectively does:
|
||||||
class PollEventLoop(EventLoop):
|
# global attr
|
||||||
|
# from .mod import attr
|
||||||
def __init__(self, runq_len=16, waitq_len=16):
|
def __getattr__(attr):
|
||||||
EventLoop.__init__(self, runq_len, waitq_len)
|
mod = _attrs.get(attr, None)
|
||||||
self.poller = select.poll()
|
if mod is None:
|
||||||
self.objmap = {}
|
raise AttributeError(attr)
|
||||||
|
value = getattr(__import__(mod, None, None, True, 1), attr)
|
||||||
def add_reader(self, sock, cb, *args):
|
globals()[attr] = value
|
||||||
if DEBUG and __debug__:
|
return value
|
||||||
log.debug("add_reader%s", (sock, cb, args))
|
|
||||||
if args:
|
|
||||||
self.poller.register(sock, select.POLLIN)
|
|
||||||
self.objmap[id(sock)] = (cb, args)
|
|
||||||
else:
|
|
||||||
self.poller.register(sock, select.POLLIN)
|
|
||||||
self.objmap[id(sock)] = cb
|
|
||||||
|
|
||||||
def remove_reader(self, sock):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("remove_reader(%s)", sock)
|
|
||||||
self.poller.unregister(sock)
|
|
||||||
del self.objmap[id(sock)]
|
|
||||||
|
|
||||||
def add_writer(self, sock, cb, *args):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("add_writer%s", (sock, cb, args))
|
|
||||||
if args:
|
|
||||||
self.poller.register(sock, select.POLLOUT)
|
|
||||||
self.objmap[id(sock)] = (cb, args)
|
|
||||||
else:
|
|
||||||
self.poller.register(sock, select.POLLOUT)
|
|
||||||
self.objmap[id(sock)] = cb
|
|
||||||
|
|
||||||
def remove_writer(self, sock):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("remove_writer(%s)", sock)
|
|
||||||
try:
|
|
||||||
self.poller.unregister(sock)
|
|
||||||
self.objmap.pop(id(sock), None)
|
|
||||||
except OSError as e:
|
|
||||||
# StreamWriter.awrite() first tries to write to a socket,
|
|
||||||
# and if that succeeds, yield IOWrite may never be called
|
|
||||||
# for that socket, and it will never be added to poller. So,
|
|
||||||
# ignore such error.
|
|
||||||
if e.args[0] != uerrno.ENOENT:
|
|
||||||
raise
|
|
||||||
|
|
||||||
def wait(self, delay):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("poll.wait(%d)", delay)
|
|
||||||
# We need one-shot behavior (second arg of 1 to .poll())
|
|
||||||
res = self.poller.ipoll(delay, 1)
|
|
||||||
#log.debug("poll result: %s", res)
|
|
||||||
# Remove "if res" workaround after
|
|
||||||
# https://github.com/micropython/micropython/issues/2716 fixed.
|
|
||||||
if res:
|
|
||||||
for sock, ev in res:
|
|
||||||
cb = self.objmap[id(sock)]
|
|
||||||
if ev & (select.POLLHUP | select.POLLERR):
|
|
||||||
# These events are returned even if not requested, and
|
|
||||||
# are sticky, i.e. will be returned again and again.
|
|
||||||
# If the caller doesn't do proper error handling and
|
|
||||||
# unregister this sock, we'll busy-loop on it, so we
|
|
||||||
# as well can unregister it now "just in case".
|
|
||||||
self.remove_reader(sock)
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("Calling IO callback: %r", cb)
|
|
||||||
if isinstance(cb, tuple):
|
|
||||||
cb[0](*cb[1])
|
|
||||||
else:
|
|
||||||
cb.pend_throw(None)
|
|
||||||
self.call_soon(cb)
|
|
||||||
|
|
||||||
|
|
||||||
class StreamReader:
|
|
||||||
|
|
||||||
def __init__(self, polls, ios=None):
|
|
||||||
if ios is None:
|
|
||||||
ios = polls
|
|
||||||
self.polls = polls
|
|
||||||
self.ios = ios
|
|
||||||
|
|
||||||
def read(self, n=-1):
|
|
||||||
while True:
|
|
||||||
yield IORead(self.polls)
|
|
||||||
res = self.ios.read(n)
|
|
||||||
if res is not None:
|
|
||||||
break
|
|
||||||
# This should not happen for real sockets, but can easily
|
|
||||||
# happen for stream wrappers (ssl, websockets, etc.)
|
|
||||||
#log.warn("Empty read")
|
|
||||||
if not res:
|
|
||||||
yield IOReadDone(self.polls)
|
|
||||||
return res
|
|
||||||
|
|
||||||
def readexactly(self, n):
|
|
||||||
buf = b""
|
|
||||||
while n:
|
|
||||||
yield IORead(self.polls)
|
|
||||||
res = self.ios.read(n)
|
|
||||||
assert res is not None
|
|
||||||
if not res:
|
|
||||||
yield IOReadDone(self.polls)
|
|
||||||
break
|
|
||||||
buf += res
|
|
||||||
n -= len(res)
|
|
||||||
return buf
|
|
||||||
|
|
||||||
def readline(self):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("StreamReader.readline()")
|
|
||||||
buf = b""
|
|
||||||
while True:
|
|
||||||
yield IORead(self.polls)
|
|
||||||
res = self.ios.readline()
|
|
||||||
assert res is not None
|
|
||||||
if not res:
|
|
||||||
yield IOReadDone(self.polls)
|
|
||||||
break
|
|
||||||
buf += res
|
|
||||||
if buf[-1] == 0x0a:
|
|
||||||
break
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("StreamReader.readline(): %s", buf)
|
|
||||||
return buf
|
|
||||||
|
|
||||||
def aclose(self):
|
|
||||||
yield IOReadDone(self.polls)
|
|
||||||
self.ios.close()
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "<StreamReader %r %r>" % (self.polls, self.ios)
|
|
||||||
|
|
||||||
|
|
||||||
class StreamWriter:
|
|
||||||
|
|
||||||
def __init__(self, s, extra):
|
|
||||||
self.s = s
|
|
||||||
self.extra = extra
|
|
||||||
|
|
||||||
def awrite(self, buf, off=0, sz=-1):
|
|
||||||
# This method is called awrite (async write) to not proliferate
|
|
||||||
# incompatibility with original asyncio. Unlike original asyncio
|
|
||||||
# whose .write() method is both not a coroutine and guaranteed
|
|
||||||
# to return immediately (which means it has to buffer all the
|
|
||||||
# data), this method is a coroutine.
|
|
||||||
if sz == -1:
|
|
||||||
sz = len(buf) - off
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("StreamWriter.awrite(): spooling %d bytes", sz)
|
|
||||||
while True:
|
|
||||||
res = self.s.write(buf, off, sz)
|
|
||||||
# If we spooled everything, return immediately
|
|
||||||
if res == sz:
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("StreamWriter.awrite(): completed spooling %d bytes", res)
|
|
||||||
return
|
|
||||||
if res is None:
|
|
||||||
res = 0
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("StreamWriter.awrite(): spooled partial %d bytes", res)
|
|
||||||
assert res < sz
|
|
||||||
off += res
|
|
||||||
sz -= res
|
|
||||||
yield IOWrite(self.s)
|
|
||||||
#assert s2.fileno() == self.s.fileno()
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("StreamWriter.awrite(): can write more")
|
|
||||||
|
|
||||||
# Write piecewise content from iterable (usually, a generator)
|
|
||||||
def awriteiter(self, iterable):
|
|
||||||
for buf in iterable:
|
|
||||||
yield from self.awrite(buf)
|
|
||||||
|
|
||||||
def aclose(self):
|
|
||||||
yield IOWriteDone(self.s)
|
|
||||||
self.s.close()
|
|
||||||
|
|
||||||
def get_extra_info(self, name, default=None):
|
|
||||||
return self.extra.get(name, default)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "<StreamWriter %r>" % self.s
|
|
||||||
|
|
||||||
|
|
||||||
def open_connection(host, port, ssl=False):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("open_connection(%s, %s)", host, port)
|
|
||||||
ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM)
|
|
||||||
ai = ai[0]
|
|
||||||
s = _socket.socket(ai[0], ai[1], ai[2])
|
|
||||||
s.setblocking(False)
|
|
||||||
try:
|
|
||||||
s.connect(ai[-1])
|
|
||||||
except OSError as e:
|
|
||||||
if e.args[0] != uerrno.EINPROGRESS:
|
|
||||||
raise
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("open_connection: After connect")
|
|
||||||
yield IOWrite(s)
|
|
||||||
# if __debug__:
|
|
||||||
# assert s2.fileno() == s.fileno()
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("open_connection: After iowait: %s", s)
|
|
||||||
if ssl:
|
|
||||||
print("Warning: uasyncio SSL support is alpha")
|
|
||||||
import ussl
|
|
||||||
s.setblocking(True)
|
|
||||||
s2 = ussl.wrap_socket(s)
|
|
||||||
s.setblocking(False)
|
|
||||||
return StreamReader(s, s2), StreamWriter(s2, {})
|
|
||||||
return StreamReader(s), StreamWriter(s, {})
|
|
||||||
|
|
||||||
|
|
||||||
def start_server(client_coro, host, port, backlog=10):
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("start_server(%s, %s)", host, port)
|
|
||||||
ai = _socket.getaddrinfo(host, port, 0, _socket.SOCK_STREAM)
|
|
||||||
ai = ai[0]
|
|
||||||
s = _socket.socket(ai[0], ai[1], ai[2])
|
|
||||||
s.setblocking(False)
|
|
||||||
|
|
||||||
s.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
|
|
||||||
s.bind(ai[-1])
|
|
||||||
s.listen(backlog)
|
|
||||||
while True:
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("start_server: Before accept")
|
|
||||||
yield IORead(s)
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("start_server: After iowait")
|
|
||||||
s2, client_addr = s.accept()
|
|
||||||
s2.setblocking(False)
|
|
||||||
if DEBUG and __debug__:
|
|
||||||
log.debug("start_server: After accept: %s", s2)
|
|
||||||
extra = {"peername": client_addr}
|
|
||||||
yield client_coro(StreamReader(s2), StreamWriter(s2, extra))
|
|
||||||
|
|
||||||
|
|
||||||
import uasyncio.core
|
|
||||||
uasyncio.core._event_loop_class = PollEventLoop
|
|
||||||
|
|||||||
@@ -1,318 +1,281 @@
|
|||||||
import utime as time
|
# MicroPython uasyncio module
|
||||||
import utimeq
|
# MIT license; Copyright (c) 2019 Damien P. George
|
||||||
import ucollections
|
|
||||||
|
from time import ticks_ms as ticks, ticks_diff, ticks_add
|
||||||
|
import sys, select
|
||||||
|
|
||||||
|
# Import TaskQueue and Task, preferring built-in C code over Python code
|
||||||
|
try:
|
||||||
|
from _uasyncio import TaskQueue, Task
|
||||||
|
except:
|
||||||
|
from .task import TaskQueue, Task
|
||||||
|
|
||||||
|
|
||||||
type_gen = type((lambda: (yield))())
|
################################################################################
|
||||||
|
# Exceptions
|
||||||
DEBUG = 0
|
|
||||||
log = None
|
|
||||||
|
|
||||||
def set_debug(val):
|
|
||||||
global DEBUG, log
|
|
||||||
DEBUG = val
|
|
||||||
if val:
|
|
||||||
import logging
|
|
||||||
log = logging.getLogger("uasyncio.core")
|
|
||||||
|
|
||||||
|
|
||||||
class CancelledError(Exception):
|
class CancelledError(BaseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TimeoutError(CancelledError):
|
class TimeoutError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class EventLoop:
|
# Used when calling Loop.call_exception_handler
|
||||||
|
_exc_context = {"message": "Task exception wasn't retrieved", "exception": None, "future": None}
|
||||||
def __init__(self, runq_len=16, waitq_len=16):
|
|
||||||
self.runq = ucollections.deque((), runq_len, True)
|
|
||||||
self.waitq = utimeq.utimeq(waitq_len)
|
|
||||||
# Current task being run. Task is a top-level coroutine scheduled
|
|
||||||
# in the event loop (sub-coroutines executed transparently by
|
|
||||||
# yield from/await, event loop "doesn't see" them).
|
|
||||||
self.cur_task = None
|
|
||||||
|
|
||||||
def time(self):
|
|
||||||
return time.ticks_ms()
|
|
||||||
|
|
||||||
def create_task(self, coro):
|
|
||||||
# CPython 3.4.2
|
|
||||||
self.call_later_ms(0, coro)
|
|
||||||
# CPython asyncio incompatibility: we don't return Task object
|
|
||||||
|
|
||||||
def call_soon(self, callback, *args):
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Scheduling in runq: %s", (callback, args))
|
|
||||||
self.runq.append(callback)
|
|
||||||
if not isinstance(callback, type_gen):
|
|
||||||
self.runq.append(args)
|
|
||||||
|
|
||||||
def call_later(self, delay, callback, *args):
|
|
||||||
self.call_at_(time.ticks_add(self.time(), int(delay * 1000)), callback, args)
|
|
||||||
|
|
||||||
def call_later_ms(self, delay, callback, *args):
|
|
||||||
if not delay:
|
|
||||||
return self.call_soon(callback, *args)
|
|
||||||
self.call_at_(time.ticks_add(self.time(), delay), callback, args)
|
|
||||||
|
|
||||||
def call_at_(self, time, callback, args=()):
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Scheduling in waitq: %s", (time, callback, args))
|
|
||||||
self.waitq.push(time, callback, args)
|
|
||||||
|
|
||||||
def wait(self, delay):
|
|
||||||
# Default wait implementation, to be overriden in subclasses
|
|
||||||
# with IO scheduling
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Sleeping for: %s", delay)
|
|
||||||
time.sleep_ms(delay)
|
|
||||||
|
|
||||||
def run_forever(self):
|
|
||||||
cur_task = [0, 0, 0]
|
|
||||||
while True:
|
|
||||||
# Expire entries in waitq and move them to runq
|
|
||||||
tnow = self.time()
|
|
||||||
while self.waitq:
|
|
||||||
t = self.waitq.peektime()
|
|
||||||
delay = time.ticks_diff(t, tnow)
|
|
||||||
if delay > 0:
|
|
||||||
break
|
|
||||||
self.waitq.pop(cur_task)
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Moving from waitq to runq: %s", cur_task[1])
|
|
||||||
self.call_soon(cur_task[1], *cur_task[2])
|
|
||||||
|
|
||||||
# Process runq
|
|
||||||
l = len(self.runq)
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Entries in runq: %d", l)
|
|
||||||
while l:
|
|
||||||
cb = self.runq.popleft()
|
|
||||||
l -= 1
|
|
||||||
args = ()
|
|
||||||
if not isinstance(cb, type_gen):
|
|
||||||
args = self.runq.popleft()
|
|
||||||
l -= 1
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.info("Next callback to run: %s", (cb, args))
|
|
||||||
cb(*args)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.info("Next coroutine to run: %s", (cb, args))
|
|
||||||
self.cur_task = cb
|
|
||||||
delay = 0
|
|
||||||
try:
|
|
||||||
if args is ():
|
|
||||||
ret = next(cb)
|
|
||||||
else:
|
|
||||||
ret = cb.send(*args)
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.info("Coroutine %s yield result: %s", cb, ret)
|
|
||||||
if isinstance(ret, SysCall1):
|
|
||||||
arg = ret.arg
|
|
||||||
if isinstance(ret, SleepMs):
|
|
||||||
delay = arg
|
|
||||||
elif isinstance(ret, IORead):
|
|
||||||
cb.pend_throw(False)
|
|
||||||
self.add_reader(arg, cb)
|
|
||||||
continue
|
|
||||||
elif isinstance(ret, IOWrite):
|
|
||||||
cb.pend_throw(False)
|
|
||||||
self.add_writer(arg, cb)
|
|
||||||
continue
|
|
||||||
elif isinstance(ret, IOReadDone):
|
|
||||||
self.remove_reader(arg)
|
|
||||||
elif isinstance(ret, IOWriteDone):
|
|
||||||
self.remove_writer(arg)
|
|
||||||
elif isinstance(ret, StopLoop):
|
|
||||||
return arg
|
|
||||||
else:
|
|
||||||
assert False, "Unknown syscall yielded: %r (of type %r)" % (ret, type(ret))
|
|
||||||
elif isinstance(ret, type_gen):
|
|
||||||
self.call_soon(ret)
|
|
||||||
elif isinstance(ret, int):
|
|
||||||
# Delay
|
|
||||||
delay = ret
|
|
||||||
elif ret is None:
|
|
||||||
# Just reschedule
|
|
||||||
pass
|
|
||||||
elif ret is False:
|
|
||||||
# Don't reschedule
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
assert False, "Unsupported coroutine yield value: %r (of type %r)" % (ret, type(ret))
|
|
||||||
except StopIteration as e:
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Coroutine finished: %s", cb)
|
|
||||||
continue
|
|
||||||
except CancelledError as e:
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("Coroutine cancelled: %s", cb)
|
|
||||||
continue
|
|
||||||
# Currently all syscalls don't return anything, so we don't
|
|
||||||
# need to feed anything to the next invocation of coroutine.
|
|
||||||
# If that changes, need to pass that value below.
|
|
||||||
if delay:
|
|
||||||
self.call_later_ms(delay, cb)
|
|
||||||
else:
|
|
||||||
self.call_soon(cb)
|
|
||||||
|
|
||||||
# Wait until next waitq task or I/O availability
|
|
||||||
delay = 0
|
|
||||||
if not self.runq:
|
|
||||||
delay = -1
|
|
||||||
if self.waitq:
|
|
||||||
tnow = self.time()
|
|
||||||
t = self.waitq.peektime()
|
|
||||||
delay = time.ticks_diff(t, tnow)
|
|
||||||
if delay < 0:
|
|
||||||
delay = 0
|
|
||||||
self.wait(delay)
|
|
||||||
|
|
||||||
def run_until_complete(self, coro):
|
|
||||||
ret = None
|
|
||||||
def _run_and_stop():
|
|
||||||
nonlocal ret
|
|
||||||
ret = yield from coro
|
|
||||||
yield StopLoop(0)
|
|
||||||
self.call_soon(_run_and_stop())
|
|
||||||
self.run_forever()
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
self.call_soon((lambda: (yield StopLoop(0)))())
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class SysCall:
|
################################################################################
|
||||||
|
# Sleep functions
|
||||||
def __init__(self, *args):
|
|
||||||
self.args = args
|
|
||||||
|
|
||||||
def handle(self):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
# Optimized syscall with 1 arg
|
|
||||||
class SysCall1(SysCall):
|
|
||||||
|
|
||||||
def __init__(self, arg):
|
|
||||||
self.arg = arg
|
|
||||||
|
|
||||||
class StopLoop(SysCall1):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class IORead(SysCall1):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class IOWrite(SysCall1):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class IOReadDone(SysCall1):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class IOWriteDone(SysCall1):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
_event_loop = None
|
|
||||||
_event_loop_class = EventLoop
|
|
||||||
def get_event_loop(runq_len=16, waitq_len=16):
|
|
||||||
global _event_loop
|
|
||||||
if _event_loop is None:
|
|
||||||
_event_loop = _event_loop_class(runq_len, waitq_len)
|
|
||||||
return _event_loop
|
|
||||||
|
|
||||||
def sleep(secs):
|
|
||||||
yield int(secs * 1000)
|
|
||||||
|
|
||||||
# Implementation of sleep_ms awaitable with zero heap memory usage
|
|
||||||
class SleepMs(SysCall1):
|
|
||||||
|
|
||||||
|
# "Yield" once, then raise StopIteration
|
||||||
|
class SingletonGenerator:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.v = None
|
self.state = None
|
||||||
self.arg = None
|
self.exc = StopIteration()
|
||||||
|
|
||||||
def __call__(self, arg):
|
|
||||||
self.v = arg
|
|
||||||
#print("__call__")
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
#print("__iter__")
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
if self.v is not None:
|
if self.state is not None:
|
||||||
#print("__next__ syscall enter")
|
_task_queue.push_sorted(cur_task, self.state)
|
||||||
self.arg = self.v
|
self.state = None
|
||||||
self.v = None
|
return None
|
||||||
return self
|
else:
|
||||||
#print("__next__ syscall exit")
|
self.exc.__traceback__ = None
|
||||||
_stop_iter.__traceback__ = None
|
raise self.exc
|
||||||
raise _stop_iter
|
|
||||||
|
|
||||||
_stop_iter = StopIteration()
|
|
||||||
sleep_ms = SleepMs()
|
|
||||||
|
|
||||||
|
|
||||||
def cancel(coro):
|
# Pause task execution for the given time (integer in milliseconds, uPy extension)
|
||||||
prev = coro.pend_throw(CancelledError())
|
# Use a SingletonGenerator to do it without allocating on the heap
|
||||||
if prev is False:
|
def sleep_ms(t, sgen=SingletonGenerator()):
|
||||||
_event_loop.call_soon(coro)
|
assert sgen.state is None
|
||||||
|
sgen.state = ticks_add(ticks(), max(0, t))
|
||||||
|
return sgen
|
||||||
|
|
||||||
|
|
||||||
class TimeoutObj:
|
# Pause task execution for the given time (in seconds)
|
||||||
def __init__(self, coro):
|
def sleep(t):
|
||||||
self.coro = coro
|
return sleep_ms(int(t * 1000))
|
||||||
|
|
||||||
|
|
||||||
def wait_for_ms(coro, timeout):
|
################################################################################
|
||||||
|
# Queue and poller for stream IO
|
||||||
def waiter(coro, timeout_obj):
|
|
||||||
res = yield from coro
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("waiter: cancelling %s", timeout_obj)
|
|
||||||
timeout_obj.coro = None
|
|
||||||
return res
|
|
||||||
|
|
||||||
def timeout_func(timeout_obj):
|
|
||||||
if timeout_obj.coro:
|
|
||||||
if __debug__ and DEBUG:
|
|
||||||
log.debug("timeout_func: cancelling %s", timeout_obj.coro)
|
|
||||||
prev = timeout_obj.coro.pend_throw(TimeoutError())
|
|
||||||
#print("prev pend", prev)
|
|
||||||
if prev is False:
|
|
||||||
_event_loop.call_soon(timeout_obj.coro)
|
|
||||||
|
|
||||||
timeout_obj = TimeoutObj(_event_loop.cur_task)
|
|
||||||
_event_loop.call_later_ms(timeout, timeout_func, timeout_obj)
|
|
||||||
return (yield from waiter(coro, timeout_obj))
|
|
||||||
|
|
||||||
|
|
||||||
def wait_for(coro, timeout):
|
class IOQueue:
|
||||||
return wait_for_ms(coro, int(timeout * 1000))
|
def __init__(self):
|
||||||
|
self.poller = select.poll()
|
||||||
|
self.map = {} # maps id(stream) to [task_waiting_read, task_waiting_write, stream]
|
||||||
|
|
||||||
|
def _enqueue(self, s, idx):
|
||||||
|
if id(s) not in self.map:
|
||||||
|
entry = [None, None, s]
|
||||||
|
entry[idx] = cur_task
|
||||||
|
self.map[id(s)] = entry
|
||||||
|
self.poller.register(s, select.POLLIN if idx == 0 else select.POLLOUT)
|
||||||
|
else:
|
||||||
|
sm = self.map[id(s)]
|
||||||
|
assert sm[idx] is None
|
||||||
|
assert sm[1 - idx] is not None
|
||||||
|
sm[idx] = cur_task
|
||||||
|
self.poller.modify(s, select.POLLIN | select.POLLOUT)
|
||||||
|
# Link task to this IOQueue so it can be removed if needed
|
||||||
|
cur_task.data = self
|
||||||
|
|
||||||
|
def _dequeue(self, s):
|
||||||
|
del self.map[id(s)]
|
||||||
|
self.poller.unregister(s)
|
||||||
|
|
||||||
|
def queue_read(self, s):
|
||||||
|
self._enqueue(s, 0)
|
||||||
|
|
||||||
|
def queue_write(self, s):
|
||||||
|
self._enqueue(s, 1)
|
||||||
|
|
||||||
|
def remove(self, task):
|
||||||
|
while True:
|
||||||
|
del_s = None
|
||||||
|
for k in self.map: # Iterate without allocating on the heap
|
||||||
|
q0, q1, s = self.map[k]
|
||||||
|
if q0 is task or q1 is task:
|
||||||
|
del_s = s
|
||||||
|
break
|
||||||
|
if del_s is not None:
|
||||||
|
self._dequeue(s)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
def wait_io_event(self, dt):
|
||||||
|
for s, ev in self.poller.ipoll(dt):
|
||||||
|
sm = self.map[id(s)]
|
||||||
|
# print('poll', s, sm, ev)
|
||||||
|
if ev & ~select.POLLOUT and sm[0] is not None:
|
||||||
|
# POLLIN or error
|
||||||
|
_task_queue.push_head(sm[0])
|
||||||
|
sm[0] = None
|
||||||
|
if ev & ~select.POLLIN and sm[1] is not None:
|
||||||
|
# POLLOUT or error
|
||||||
|
_task_queue.push_head(sm[1])
|
||||||
|
sm[1] = None
|
||||||
|
if sm[0] is None and sm[1] is None:
|
||||||
|
self._dequeue(s)
|
||||||
|
elif sm[0] is None:
|
||||||
|
self.poller.modify(s, select.POLLOUT)
|
||||||
|
else:
|
||||||
|
self.poller.modify(s, select.POLLIN)
|
||||||
|
|
||||||
|
|
||||||
def coroutine(f):
|
################################################################################
|
||||||
return f
|
# Main run loop
|
||||||
|
|
||||||
#
|
# Ensure the awaitable is a task
|
||||||
# The functions below are deprecated in uasyncio, and provided only
|
def _promote_to_task(aw):
|
||||||
# for compatibility with CPython asyncio
|
return aw if isinstance(aw, Task) else create_task(aw)
|
||||||
#
|
|
||||||
|
|
||||||
def ensure_future(coro, loop=_event_loop):
|
|
||||||
_event_loop.call_soon(coro)
|
|
||||||
# CPython asyncio incompatibility: we don't return Task object
|
|
||||||
return coro
|
|
||||||
|
|
||||||
|
|
||||||
# CPython asyncio incompatibility: Task is a function, not a class (for efficiency)
|
# Create and schedule a new task from a coroutine
|
||||||
def Task(coro, loop=_event_loop):
|
def create_task(coro):
|
||||||
# Same as async()
|
if not hasattr(coro, "send"):
|
||||||
_event_loop.call_soon(coro)
|
raise TypeError("coroutine expected")
|
||||||
|
t = Task(coro, globals())
|
||||||
|
_task_queue.push_head(t)
|
||||||
|
return t
|
||||||
|
|
||||||
|
|
||||||
|
# Keep scheduling tasks until there are none left to schedule
|
||||||
|
def run_until_complete(main_task=None):
|
||||||
|
global cur_task
|
||||||
|
excs_all = (CancelledError, Exception) # To prevent heap allocation in loop
|
||||||
|
excs_stop = (CancelledError, StopIteration) # To prevent heap allocation in loop
|
||||||
|
while True:
|
||||||
|
# Wait until the head of _task_queue is ready to run
|
||||||
|
dt = 1
|
||||||
|
while dt > 0:
|
||||||
|
dt = -1
|
||||||
|
t = _task_queue.peek()
|
||||||
|
if t:
|
||||||
|
# A task waiting on _task_queue; "ph_key" is time to schedule task at
|
||||||
|
dt = max(0, ticks_diff(t.ph_key, ticks()))
|
||||||
|
elif not _io_queue.map:
|
||||||
|
# No tasks can be woken so finished running
|
||||||
|
return
|
||||||
|
# print('(poll {})'.format(dt), len(_io_queue.map))
|
||||||
|
_io_queue.wait_io_event(dt)
|
||||||
|
|
||||||
|
# Get next task to run and continue it
|
||||||
|
t = _task_queue.pop_head()
|
||||||
|
cur_task = t
|
||||||
|
try:
|
||||||
|
# Continue running the coroutine, it's responsible for rescheduling itself
|
||||||
|
exc = t.data
|
||||||
|
if not exc:
|
||||||
|
t.coro.send(None)
|
||||||
|
else:
|
||||||
|
t.data = None
|
||||||
|
t.coro.throw(exc)
|
||||||
|
except excs_all as er:
|
||||||
|
# Check the task is not on any event queue
|
||||||
|
assert t.data is None
|
||||||
|
# This task is done, check if it's the main task and then loop should stop
|
||||||
|
if t is main_task:
|
||||||
|
if isinstance(er, StopIteration):
|
||||||
|
return er.value
|
||||||
|
raise er
|
||||||
|
# Schedule any other tasks waiting on the completion of this task
|
||||||
|
waiting = False
|
||||||
|
if hasattr(t, "waiting"):
|
||||||
|
while t.waiting.peek():
|
||||||
|
_task_queue.push_head(t.waiting.pop_head())
|
||||||
|
waiting = True
|
||||||
|
t.waiting = None # Free waiting queue head
|
||||||
|
if not waiting and not isinstance(er, excs_stop):
|
||||||
|
# An exception ended this detached task, so queue it for later
|
||||||
|
# execution to handle the uncaught exception if no other task retrieves
|
||||||
|
# the exception in the meantime (this is handled by Task.throw).
|
||||||
|
_task_queue.push_head(t)
|
||||||
|
# Indicate task is done by setting coro to the task object itself
|
||||||
|
t.coro = t
|
||||||
|
# Save return value of coro to pass up to caller
|
||||||
|
t.data = er
|
||||||
|
|
||||||
|
|
||||||
|
# Create a new task from a coroutine and run it until it finishes
|
||||||
|
def run(coro):
|
||||||
|
return run_until_complete(create_task(coro))
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# Event loop wrapper
|
||||||
|
|
||||||
|
|
||||||
|
async def _stopper():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
_stop_task = None
|
||||||
|
|
||||||
|
|
||||||
|
class Loop:
|
||||||
|
_exc_handler = None
|
||||||
|
|
||||||
|
def create_task(coro):
|
||||||
|
return create_task(coro)
|
||||||
|
|
||||||
|
def run_forever():
|
||||||
|
global _stop_task
|
||||||
|
_stop_task = Task(_stopper(), globals())
|
||||||
|
run_until_complete(_stop_task)
|
||||||
|
# TODO should keep running until .stop() is called, even if there're no tasks left
|
||||||
|
|
||||||
|
def run_until_complete(aw):
|
||||||
|
return run_until_complete(_promote_to_task(aw))
|
||||||
|
|
||||||
|
def stop():
|
||||||
|
global _stop_task
|
||||||
|
if _stop_task is not None:
|
||||||
|
_task_queue.push_head(_stop_task)
|
||||||
|
# If stop() is called again, do nothing
|
||||||
|
_stop_task = None
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def set_exception_handler(handler):
|
||||||
|
Loop._exc_handler = handler
|
||||||
|
|
||||||
|
def get_exception_handler():
|
||||||
|
return Loop._exc_handler
|
||||||
|
|
||||||
|
def default_exception_handler(loop, context):
|
||||||
|
print(context["message"])
|
||||||
|
print("future:", context["future"], "coro=", context["future"].coro)
|
||||||
|
sys.print_exception(context["exception"])
|
||||||
|
|
||||||
|
def call_exception_handler(context):
|
||||||
|
(Loop._exc_handler or Loop.default_exception_handler)(Loop, context)
|
||||||
|
|
||||||
|
|
||||||
|
# The runq_len and waitq_len arguments are for legacy uasyncio compatibility
|
||||||
|
def get_event_loop(runq_len=0, waitq_len=0):
|
||||||
|
return Loop
|
||||||
|
|
||||||
|
|
||||||
|
def current_task():
|
||||||
|
return cur_task
|
||||||
|
|
||||||
|
|
||||||
|
def new_event_loop():
|
||||||
|
global _task_queue, _io_queue
|
||||||
|
# TaskQueue of Task instances
|
||||||
|
_task_queue = TaskQueue()
|
||||||
|
# Task queue and poller for stream IO
|
||||||
|
_io_queue = IOQueue()
|
||||||
|
return Loop
|
||||||
|
|
||||||
|
|
||||||
|
# Initialise default event loop
|
||||||
|
new_event_loop()
|
||||||
|
|||||||
62
tests/libs/uasyncio/event.py
Normal file
62
tests/libs/uasyncio/event.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# MicroPython uasyncio module
|
||||||
|
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||||
|
|
||||||
|
from . import core
|
||||||
|
|
||||||
|
# Event class for primitive events that can be waited on, set, and cleared
|
||||||
|
class Event:
|
||||||
|
def __init__(self):
|
||||||
|
self.state = False # False=unset; True=set
|
||||||
|
self.waiting = core.TaskQueue() # Queue of Tasks waiting on completion of this event
|
||||||
|
|
||||||
|
def is_set(self):
|
||||||
|
return self.state
|
||||||
|
|
||||||
|
def set(self):
|
||||||
|
# Event becomes set, schedule any tasks waiting on it
|
||||||
|
# Note: This must not be called from anything except the thread running
|
||||||
|
# the asyncio loop (i.e. neither hard or soft IRQ, or a different thread).
|
||||||
|
while self.waiting.peek():
|
||||||
|
core._task_queue.push_head(self.waiting.pop_head())
|
||||||
|
self.state = True
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
self.state = False
|
||||||
|
|
||||||
|
async def wait(self):
|
||||||
|
if not self.state:
|
||||||
|
# Event not set, put the calling task on the event's waiting queue
|
||||||
|
self.waiting.push_head(core.cur_task)
|
||||||
|
# Set calling task's data to the event's queue so it can be removed if needed
|
||||||
|
core.cur_task.data = self.waiting
|
||||||
|
yield
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# MicroPython-extension: This can be set from outside the asyncio event loop,
|
||||||
|
# such as other threads, IRQs or scheduler context. Implementation is a stream
|
||||||
|
# that asyncio will poll until a flag is set.
|
||||||
|
# Note: Unlike Event, this is self-clearing.
|
||||||
|
try:
|
||||||
|
import uio
|
||||||
|
|
||||||
|
class ThreadSafeFlag(uio.IOBase):
|
||||||
|
def __init__(self):
|
||||||
|
self._flag = 0
|
||||||
|
|
||||||
|
def ioctl(self, req, flags):
|
||||||
|
if req == 3: # MP_STREAM_POLL
|
||||||
|
return self._flag * flags
|
||||||
|
return None
|
||||||
|
|
||||||
|
def set(self):
|
||||||
|
self._flag = 1
|
||||||
|
|
||||||
|
async def wait(self):
|
||||||
|
if not self._flag:
|
||||||
|
yield core._io_queue.queue_read(self)
|
||||||
|
self._flag = 0
|
||||||
|
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
74
tests/libs/uasyncio/funcs.py
Normal file
74
tests/libs/uasyncio/funcs.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
# MicroPython uasyncio module
|
||||||
|
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||||
|
|
||||||
|
from . import core
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_for(aw, timeout, sleep=core.sleep):
|
||||||
|
aw = core._promote_to_task(aw)
|
||||||
|
if timeout is None:
|
||||||
|
return await aw
|
||||||
|
|
||||||
|
def runner(waiter, aw):
|
||||||
|
nonlocal status, result
|
||||||
|
try:
|
||||||
|
result = await aw
|
||||||
|
s = True
|
||||||
|
except BaseException as er:
|
||||||
|
s = er
|
||||||
|
if status is None:
|
||||||
|
# The waiter is still waiting, set status for it and cancel it.
|
||||||
|
status = s
|
||||||
|
waiter.cancel()
|
||||||
|
|
||||||
|
# Run aw in a separate runner task that manages its exceptions.
|
||||||
|
status = None
|
||||||
|
result = None
|
||||||
|
runner_task = core.create_task(runner(core.cur_task, aw))
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Wait for the timeout to elapse.
|
||||||
|
await sleep(timeout)
|
||||||
|
except core.CancelledError as er:
|
||||||
|
if status is True:
|
||||||
|
# aw completed successfully and cancelled the sleep, so return aw's result.
|
||||||
|
return result
|
||||||
|
elif status is None:
|
||||||
|
# This wait_for was cancelled externally, so cancel aw and re-raise.
|
||||||
|
status = True
|
||||||
|
runner_task.cancel()
|
||||||
|
raise er
|
||||||
|
else:
|
||||||
|
# aw raised an exception, propagate it out to the caller.
|
||||||
|
raise status
|
||||||
|
|
||||||
|
# The sleep finished before aw, so cancel aw and raise TimeoutError.
|
||||||
|
status = True
|
||||||
|
runner_task.cancel()
|
||||||
|
await runner_task
|
||||||
|
raise core.TimeoutError
|
||||||
|
|
||||||
|
|
||||||
|
def wait_for_ms(aw, timeout):
|
||||||
|
return wait_for(aw, timeout, core.sleep_ms)
|
||||||
|
|
||||||
|
|
||||||
|
async def gather(*aws, return_exceptions=False):
|
||||||
|
ts = [core._promote_to_task(aw) for aw in aws]
|
||||||
|
for i in range(len(ts)):
|
||||||
|
try:
|
||||||
|
# TODO handle cancel of gather itself
|
||||||
|
# if ts[i].coro:
|
||||||
|
# iter(ts[i]).waiting.push_head(cur_task)
|
||||||
|
# try:
|
||||||
|
# yield
|
||||||
|
# except CancelledError as er:
|
||||||
|
# # cancel all waiting tasks
|
||||||
|
# raise er
|
||||||
|
ts[i] = await ts[i]
|
||||||
|
except Exception as er:
|
||||||
|
if return_exceptions:
|
||||||
|
ts[i] = er
|
||||||
|
else:
|
||||||
|
raise er
|
||||||
|
return ts
|
||||||
53
tests/libs/uasyncio/lock.py
Normal file
53
tests/libs/uasyncio/lock.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# MicroPython uasyncio module
|
||||||
|
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||||
|
|
||||||
|
from . import core
|
||||||
|
|
||||||
|
# Lock class for primitive mutex capability
|
||||||
|
class Lock:
|
||||||
|
def __init__(self):
|
||||||
|
# The state can take the following values:
|
||||||
|
# - 0: unlocked
|
||||||
|
# - 1: locked
|
||||||
|
# - <Task>: unlocked but this task has been scheduled to acquire the lock next
|
||||||
|
self.state = 0
|
||||||
|
# Queue of Tasks waiting to acquire this Lock
|
||||||
|
self.waiting = core.TaskQueue()
|
||||||
|
|
||||||
|
def locked(self):
|
||||||
|
return self.state == 1
|
||||||
|
|
||||||
|
def release(self):
|
||||||
|
if self.state != 1:
|
||||||
|
raise RuntimeError("Lock not acquired")
|
||||||
|
if self.waiting.peek():
|
||||||
|
# Task(s) waiting on lock, schedule next Task
|
||||||
|
self.state = self.waiting.pop_head()
|
||||||
|
core._task_queue.push_head(self.state)
|
||||||
|
else:
|
||||||
|
# No Task waiting so unlock
|
||||||
|
self.state = 0
|
||||||
|
|
||||||
|
async def acquire(self):
|
||||||
|
if self.state != 0:
|
||||||
|
# Lock unavailable, put the calling Task on the waiting queue
|
||||||
|
self.waiting.push_head(core.cur_task)
|
||||||
|
# Set calling task's data to the lock's queue so it can be removed if needed
|
||||||
|
core.cur_task.data = self.waiting
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except core.CancelledError as er:
|
||||||
|
if self.state == core.cur_task:
|
||||||
|
# Cancelled while pending on resume, schedule next waiting Task
|
||||||
|
self.state = 1
|
||||||
|
self.release()
|
||||||
|
raise er
|
||||||
|
# Lock available, set it as locked
|
||||||
|
self.state = 1
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
return await self.acquire()
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
return self.release()
|
||||||
13
tests/libs/uasyncio/manifest.py
Normal file
13
tests/libs/uasyncio/manifest.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# This list of frozen files doesn't include task.py because that's provided by the C module.
|
||||||
|
freeze(
|
||||||
|
"..",
|
||||||
|
(
|
||||||
|
"uasyncio/__init__.py",
|
||||||
|
"uasyncio/core.py",
|
||||||
|
"uasyncio/event.py",
|
||||||
|
"uasyncio/funcs.py",
|
||||||
|
"uasyncio/lock.py",
|
||||||
|
"uasyncio/stream.py",
|
||||||
|
),
|
||||||
|
opt=3,
|
||||||
|
)
|
||||||
158
tests/libs/uasyncio/stream.py
Normal file
158
tests/libs/uasyncio/stream.py
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
# MicroPython uasyncio module
|
||||||
|
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||||
|
|
||||||
|
from . import core
|
||||||
|
|
||||||
|
|
||||||
|
class Stream:
|
||||||
|
def __init__(self, s, e={}):
|
||||||
|
self.s = s
|
||||||
|
self.e = e
|
||||||
|
self.out_buf = b""
|
||||||
|
|
||||||
|
def get_extra_info(self, v):
|
||||||
|
return self.e[v]
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
await self.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def wait_closed(self):
|
||||||
|
# TODO yield?
|
||||||
|
self.s.close()
|
||||||
|
|
||||||
|
async def read(self, n):
|
||||||
|
yield core._io_queue.queue_read(self.s)
|
||||||
|
return self.s.read(n)
|
||||||
|
|
||||||
|
async def readexactly(self, n):
|
||||||
|
r = b""
|
||||||
|
while n:
|
||||||
|
yield core._io_queue.queue_read(self.s)
|
||||||
|
r2 = self.s.read(n)
|
||||||
|
if r2 is not None:
|
||||||
|
if not len(r2):
|
||||||
|
raise EOFError
|
||||||
|
r += r2
|
||||||
|
n -= len(r2)
|
||||||
|
return r
|
||||||
|
|
||||||
|
async def readline(self):
|
||||||
|
l = b""
|
||||||
|
while True:
|
||||||
|
yield core._io_queue.queue_read(self.s)
|
||||||
|
l2 = self.s.readline() # may do multiple reads but won't block
|
||||||
|
l += l2
|
||||||
|
if not l2 or l[-1] == 10: # \n (check l in case l2 is str)
|
||||||
|
return l
|
||||||
|
|
||||||
|
def write(self, buf):
|
||||||
|
self.out_buf += buf
|
||||||
|
|
||||||
|
async def drain(self):
|
||||||
|
mv = memoryview(self.out_buf)
|
||||||
|
off = 0
|
||||||
|
while off < len(mv):
|
||||||
|
yield core._io_queue.queue_write(self.s)
|
||||||
|
ret = self.s.write(mv[off:])
|
||||||
|
if ret is not None:
|
||||||
|
off += ret
|
||||||
|
self.out_buf = b""
|
||||||
|
|
||||||
|
|
||||||
|
# Stream can be used for both reading and writing to save code size
|
||||||
|
StreamReader = Stream
|
||||||
|
StreamWriter = Stream
|
||||||
|
|
||||||
|
|
||||||
|
# Create a TCP stream connection to a remote host
|
||||||
|
async def open_connection(host, port):
|
||||||
|
from uerrno import EINPROGRESS
|
||||||
|
import usocket as socket
|
||||||
|
|
||||||
|
ai = socket.getaddrinfo(host, port)[0] # TODO this is blocking!
|
||||||
|
s = socket.socket()
|
||||||
|
s.setblocking(False)
|
||||||
|
ss = Stream(s)
|
||||||
|
try:
|
||||||
|
s.connect(ai[-1])
|
||||||
|
except OSError as er:
|
||||||
|
if er.errno != EINPROGRESS:
|
||||||
|
raise er
|
||||||
|
yield core._io_queue.queue_write(s)
|
||||||
|
return ss, ss
|
||||||
|
|
||||||
|
|
||||||
|
# Class representing a TCP stream server, can be closed and used in "async with"
|
||||||
|
class Server:
|
||||||
|
async def __aenter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
self.close()
|
||||||
|
await self.wait_closed()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.task.cancel()
|
||||||
|
|
||||||
|
async def wait_closed(self):
|
||||||
|
await self.task
|
||||||
|
|
||||||
|
async def _serve(self, cb, host, port, backlog):
|
||||||
|
import usocket as socket
|
||||||
|
|
||||||
|
ai = socket.getaddrinfo(host, port)[0] # TODO this is blocking!
|
||||||
|
s = socket.socket()
|
||||||
|
s.setblocking(False)
|
||||||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
s.bind(ai[-1])
|
||||||
|
s.listen(backlog)
|
||||||
|
self.task = core.cur_task
|
||||||
|
# Accept incoming connections
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
yield core._io_queue.queue_read(s)
|
||||||
|
except core.CancelledError:
|
||||||
|
# Shutdown server
|
||||||
|
s.close()
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
s2, addr = s.accept()
|
||||||
|
except:
|
||||||
|
# Ignore a failed accept
|
||||||
|
continue
|
||||||
|
s2.setblocking(False)
|
||||||
|
s2s = Stream(s2, {"peername": addr})
|
||||||
|
core.create_task(cb(s2s, s2s))
|
||||||
|
|
||||||
|
|
||||||
|
# Helper function to start a TCP stream server, running as a new task
|
||||||
|
# TODO could use an accept-callback on socket read activity instead of creating a task
|
||||||
|
async def start_server(cb, host, port, backlog=5):
|
||||||
|
s = Server()
|
||||||
|
core.create_task(s._serve(cb, host, port, backlog))
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# Legacy uasyncio compatibility
|
||||||
|
|
||||||
|
|
||||||
|
async def stream_awrite(self, buf, off=0, sz=-1):
|
||||||
|
if off != 0 or sz != -1:
|
||||||
|
buf = memoryview(buf)
|
||||||
|
if sz == -1:
|
||||||
|
sz = len(buf)
|
||||||
|
buf = buf[off : off + sz]
|
||||||
|
self.write(buf)
|
||||||
|
await self.drain()
|
||||||
|
|
||||||
|
|
||||||
|
Stream.aclose = Stream.wait_closed
|
||||||
|
Stream.awrite = stream_awrite
|
||||||
|
Stream.awritestr = stream_awrite # TODO explicitly convert to bytes?
|
||||||
184
tests/libs/uasyncio/task.py
Normal file
184
tests/libs/uasyncio/task.py
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
# MicroPython uasyncio module
|
||||||
|
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
||||||
|
|
||||||
|
# This file contains the core TaskQueue based on a pairing heap, and the core Task class.
|
||||||
|
# They can optionally be replaced by C implementations.
|
||||||
|
|
||||||
|
from . import core
|
||||||
|
|
||||||
|
|
||||||
|
# pairing-heap meld of 2 heaps; O(1)
|
||||||
|
def ph_meld(h1, h2):
|
||||||
|
if h1 is None:
|
||||||
|
return h2
|
||||||
|
if h2 is None:
|
||||||
|
return h1
|
||||||
|
lt = core.ticks_diff(h1.ph_key, h2.ph_key) < 0
|
||||||
|
if lt:
|
||||||
|
if h1.ph_child is None:
|
||||||
|
h1.ph_child = h2
|
||||||
|
else:
|
||||||
|
h1.ph_child_last.ph_next = h2
|
||||||
|
h1.ph_child_last = h2
|
||||||
|
h2.ph_next = None
|
||||||
|
h2.ph_rightmost_parent = h1
|
||||||
|
return h1
|
||||||
|
else:
|
||||||
|
h1.ph_next = h2.ph_child
|
||||||
|
h2.ph_child = h1
|
||||||
|
if h1.ph_next is None:
|
||||||
|
h2.ph_child_last = h1
|
||||||
|
h1.ph_rightmost_parent = h2
|
||||||
|
return h2
|
||||||
|
|
||||||
|
|
||||||
|
# pairing-heap pairing operation; amortised O(log N)
|
||||||
|
def ph_pairing(child):
|
||||||
|
heap = None
|
||||||
|
while child is not None:
|
||||||
|
n1 = child
|
||||||
|
child = child.ph_next
|
||||||
|
n1.ph_next = None
|
||||||
|
if child is not None:
|
||||||
|
n2 = child
|
||||||
|
child = child.ph_next
|
||||||
|
n2.ph_next = None
|
||||||
|
n1 = ph_meld(n1, n2)
|
||||||
|
heap = ph_meld(heap, n1)
|
||||||
|
return heap
|
||||||
|
|
||||||
|
|
||||||
|
# pairing-heap delete of a node; stable, amortised O(log N)
|
||||||
|
def ph_delete(heap, node):
|
||||||
|
if node is heap:
|
||||||
|
child = heap.ph_child
|
||||||
|
node.ph_child = None
|
||||||
|
return ph_pairing(child)
|
||||||
|
# Find parent of node
|
||||||
|
parent = node
|
||||||
|
while parent.ph_next is not None:
|
||||||
|
parent = parent.ph_next
|
||||||
|
parent = parent.ph_rightmost_parent
|
||||||
|
# Replace node with pairing of its children
|
||||||
|
if node is parent.ph_child and node.ph_child is None:
|
||||||
|
parent.ph_child = node.ph_next
|
||||||
|
node.ph_next = None
|
||||||
|
return heap
|
||||||
|
elif node is parent.ph_child:
|
||||||
|
child = node.ph_child
|
||||||
|
next = node.ph_next
|
||||||
|
node.ph_child = None
|
||||||
|
node.ph_next = None
|
||||||
|
node = ph_pairing(child)
|
||||||
|
parent.ph_child = node
|
||||||
|
else:
|
||||||
|
n = parent.ph_child
|
||||||
|
while node is not n.ph_next:
|
||||||
|
n = n.ph_next
|
||||||
|
child = node.ph_child
|
||||||
|
next = node.ph_next
|
||||||
|
node.ph_child = None
|
||||||
|
node.ph_next = None
|
||||||
|
node = ph_pairing(child)
|
||||||
|
if node is None:
|
||||||
|
node = n
|
||||||
|
else:
|
||||||
|
n.ph_next = node
|
||||||
|
node.ph_next = next
|
||||||
|
if next is None:
|
||||||
|
node.ph_rightmost_parent = parent
|
||||||
|
parent.ph_child_last = node
|
||||||
|
return heap
|
||||||
|
|
||||||
|
|
||||||
|
# TaskQueue class based on the above pairing-heap functions.
|
||||||
|
class TaskQueue:
|
||||||
|
def __init__(self):
|
||||||
|
self.heap = None
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
return self.heap
|
||||||
|
|
||||||
|
def push_sorted(self, v, key):
|
||||||
|
v.data = None
|
||||||
|
v.ph_key = key
|
||||||
|
v.ph_child = None
|
||||||
|
v.ph_next = None
|
||||||
|
self.heap = ph_meld(v, self.heap)
|
||||||
|
|
||||||
|
def push_head(self, v):
|
||||||
|
self.push_sorted(v, core.ticks())
|
||||||
|
|
||||||
|
def pop_head(self):
|
||||||
|
v = self.heap
|
||||||
|
self.heap = ph_pairing(self.heap.ph_child)
|
||||||
|
return v
|
||||||
|
|
||||||
|
def remove(self, v):
|
||||||
|
self.heap = ph_delete(self.heap, v)
|
||||||
|
|
||||||
|
|
||||||
|
# Task class representing a coroutine, can be waited on and cancelled.
|
||||||
|
class Task:
|
||||||
|
def __init__(self, coro, globals=None):
|
||||||
|
self.coro = coro # Coroutine of this Task
|
||||||
|
self.data = None # General data for queue it is waiting on
|
||||||
|
self.ph_key = 0 # Pairing heap
|
||||||
|
self.ph_child = None # Paring heap
|
||||||
|
self.ph_child_last = None # Paring heap
|
||||||
|
self.ph_next = None # Paring heap
|
||||||
|
self.ph_rightmost_parent = None # Paring heap
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
if self.coro is self:
|
||||||
|
# Signal that the completed-task has been await'ed on.
|
||||||
|
self.waiting = None
|
||||||
|
elif not hasattr(self, "waiting"):
|
||||||
|
# Lazily allocated head of linked list of Tasks waiting on completion of this task.
|
||||||
|
self.waiting = TaskQueue()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if self.coro is self:
|
||||||
|
# Task finished, raise return value to caller so it can continue.
|
||||||
|
raise self.data
|
||||||
|
else:
|
||||||
|
# Put calling task on waiting queue.
|
||||||
|
self.waiting.push_head(core.cur_task)
|
||||||
|
# Set calling task's data to this task that it waits on, to double-link it.
|
||||||
|
core.cur_task.data = self
|
||||||
|
|
||||||
|
def done(self):
|
||||||
|
return self.coro is self
|
||||||
|
|
||||||
|
def cancel(self):
|
||||||
|
# Check if task is already finished.
|
||||||
|
if self.coro is self:
|
||||||
|
return False
|
||||||
|
# Can't cancel self (not supported yet).
|
||||||
|
if self is core.cur_task:
|
||||||
|
raise RuntimeError("can't cancel self")
|
||||||
|
# If Task waits on another task then forward the cancel to the one it's waiting on.
|
||||||
|
while isinstance(self.data, Task):
|
||||||
|
self = self.data
|
||||||
|
# Reschedule Task as a cancelled task.
|
||||||
|
if hasattr(self.data, "remove"):
|
||||||
|
# Not on the main running queue, remove the task from the queue it's on.
|
||||||
|
self.data.remove(self)
|
||||||
|
core._task_queue.push_head(self)
|
||||||
|
elif core.ticks_diff(self.ph_key, core.ticks()) > 0:
|
||||||
|
# On the main running queue but scheduled in the future, so bring it forward to now.
|
||||||
|
core._task_queue.remove(self)
|
||||||
|
core._task_queue.push_head(self)
|
||||||
|
self.data = core.CancelledError
|
||||||
|
return True
|
||||||
|
|
||||||
|
def throw(self, value):
|
||||||
|
# This task raised an exception which was uncaught; handle that now.
|
||||||
|
# Set the data because it was cleared by the main scheduling loop.
|
||||||
|
self.data = value
|
||||||
|
if not hasattr(self, "waiting"):
|
||||||
|
# Nothing await'ed on the task so call the exception handler.
|
||||||
|
core._exc_context["exception"] = value
|
||||||
|
core._exc_context["future"] = self
|
||||||
|
core.Loop.call_exception_handler(core._exc_context)
|
||||||
@@ -21,6 +21,14 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
sys.modules['microdot'].socket = self.original_socket
|
sys.modules['microdot'].socket = self.original_socket
|
||||||
sys.modules['microdot'].create_thread = self.original_create_thread
|
sys.modules['microdot'].create_thread = self.original_create_thread
|
||||||
|
|
||||||
|
def _add_shutdown(self, app):
|
||||||
|
@app.route('/shutdown')
|
||||||
|
def shutdown(req):
|
||||||
|
app.shutdown()
|
||||||
|
return ''
|
||||||
|
|
||||||
|
mock_socket.add_request('GET', '/shutdown')
|
||||||
|
|
||||||
def test_get_request(self):
|
def test_get_request(self):
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
|
|
||||||
@@ -30,7 +38,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -49,7 +58,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('POST', '/')
|
fd = mock_socket.add_request('POST', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -83,7 +93,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/bar')
|
fd = mock_socket.add_request('GET', '/bar')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
|
||||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||||
@@ -93,7 +104,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/baz')
|
fd = mock_socket.add_request('GET', '/baz')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||||
@@ -110,7 +122,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/foo')
|
fd = mock_socket.add_request('GET', '/foo')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 9\r\n', fd.response)
|
self.assertIn(b'Content-Length: 9\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -129,7 +142,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/foo')
|
fd = mock_socket.add_request('GET', '/foo')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -144,7 +158,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 21\r\n', fd.response)
|
self.assertIn(b'Content-Length: 21\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -163,7 +178,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -182,7 +198,8 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ from tests.mock_socket import get_request_fd
|
|||||||
class TestRequest(unittest.TestCase):
|
class TestRequest(unittest.TestCase):
|
||||||
def test_create_request(self):
|
def test_create_request(self):
|
||||||
fd = get_request_fd('GET', '/foo')
|
fd = get_request_fd('GET', '/foo')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
|
self.assertEqual(req.app, 'app')
|
||||||
self.assertEqual(req.client_addr, 'addr')
|
self.assertEqual(req.client_addr, 'addr')
|
||||||
self.assertEqual(req.method, 'GET')
|
self.assertEqual(req.method, 'GET')
|
||||||
self.assertEqual(req.path, '/foo')
|
self.assertEqual(req.path, '/foo')
|
||||||
@@ -26,7 +27,7 @@ class TestRequest(unittest.TestCase):
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Cookie': 'foo=bar;abc=def',
|
'Cookie': 'foo=bar;abc=def',
|
||||||
'Content-Length': '3'}, body='aaa')
|
'Content-Length': '3'}, body='aaa')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
self.assertEqual(req.headers, {
|
self.assertEqual(req.headers, {
|
||||||
'Host': 'example.com:1234',
|
'Host': 'example.com:1234',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -39,33 +40,33 @@ class TestRequest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_args(self):
|
def test_args(self):
|
||||||
fd = get_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
|
fd = get_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
|
self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
|
||||||
self.assertEqual(req.args, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
self.assertEqual(req.args, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
||||||
|
|
||||||
def test_json(self):
|
def test_json(self):
|
||||||
fd = get_request_fd('GET', '/foo', headers={
|
fd = get_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/json'}, body='{"foo":"bar"}')
|
'Content-Type': 'application/json'}, body='{"foo":"bar"}')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
json = req.json
|
json = req.json
|
||||||
self.assertEqual(json, {'foo': 'bar'})
|
self.assertEqual(json, {'foo': 'bar'})
|
||||||
self.assertTrue(req.json is json)
|
self.assertTrue(req.json is json)
|
||||||
|
|
||||||
fd = get_request_fd('GET', '/foo', headers={
|
fd = get_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/json'}, body='[1, "2"]')
|
'Content-Type': 'application/json'}, body='[1, "2"]')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
self.assertEqual(req.json, [1, '2'])
|
self.assertEqual(req.json, [1, '2'])
|
||||||
|
|
||||||
fd = get_request_fd('GET', '/foo', headers={
|
fd = get_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/xml'}, body='[1, "2"]')
|
'Content-Type': 'application/xml'}, body='[1, "2"]')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
self.assertIsNone(req.json)
|
self.assertIsNone(req.json)
|
||||||
|
|
||||||
def test_form(self):
|
def test_form(self):
|
||||||
fd = get_request_fd('GET', '/foo', headers={
|
fd = get_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||||
body='foo=bar&abc=def&x=%2f%%')
|
body='foo=bar&abc=def&x=%2f%%')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
form = req.form
|
form = req.form
|
||||||
self.assertEqual(form, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
self.assertEqual(form, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
||||||
self.assertTrue(req.form is form)
|
self.assertTrue(req.form is form)
|
||||||
@@ -73,5 +74,5 @@ class TestRequest(unittest.TestCase):
|
|||||||
fd = get_request_fd('GET', '/foo', headers={
|
fd = get_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/json'},
|
'Content-Type': 'application/json'},
|
||||||
body='foo=bar&abc=def&x=%2f%%')
|
body='foo=bar&abc=def&x=%2f%%')
|
||||||
req = Request.create(fd, 'addr')
|
req = Request.create('app', fd, 'addr')
|
||||||
self.assertIsNone(req.form)
|
self.assertIsNone(req.form)
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ class TestResponse(unittest.TestCase):
|
|||||||
res = Response(123)
|
res = Response(123)
|
||||||
self.assertEqual(res.status_code, 200)
|
self.assertEqual(res.status_code, 200)
|
||||||
self.assertEqual(res.headers, {})
|
self.assertEqual(res.headers, {})
|
||||||
self.assertEqual(res.body, b'123')
|
self.assertEqual(res.body, 123)
|
||||||
|
|
||||||
def test_create_with_status_code(self):
|
def test_create_with_status_code(self):
|
||||||
res = Response('not found', 404)
|
res = Response('not found', 404)
|
||||||
@@ -161,11 +161,34 @@ class TestResponse(unittest.TestCase):
|
|||||||
res = Response.send_file('tests/files/' + file)
|
res = Response.send_file('tests/files/' + file)
|
||||||
self.assertEqual(res.status_code, 200)
|
self.assertEqual(res.status_code, 200)
|
||||||
self.assertEqual(res.headers['Content-Type'], content_type)
|
self.assertEqual(res.headers['Content-Type'], content_type)
|
||||||
self.assertEqual(res.headers['Content-Length'], '4')
|
fd = io.BytesIO()
|
||||||
self.assertEqual(res.body, b'foo\n')
|
res.write(fd)
|
||||||
|
response = fd.getvalue()
|
||||||
|
self.assertEqual(response, (
|
||||||
|
b'HTTP/1.0 200 OK\r\nContent-Type: ' + content_type.encode()
|
||||||
|
+ b'\r\n\r\nfoo\n'))
|
||||||
res = Response.send_file('tests/files/test.txt',
|
res = Response.send_file('tests/files/test.txt',
|
||||||
content_type='text/html')
|
content_type='text/html')
|
||||||
self.assertEqual(res.status_code, 200)
|
self.assertEqual(res.status_code, 200)
|
||||||
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||||
self.assertEqual(res.headers['Content-Length'], '4')
|
fd = io.BytesIO()
|
||||||
self.assertEqual(res.body, b'foo\n')
|
res.write(fd)
|
||||||
|
response = fd.getvalue()
|
||||||
|
self.assertEqual(
|
||||||
|
response,
|
||||||
|
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||||
|
|
||||||
|
def test_send_file_small_buffer(self):
|
||||||
|
original_buffer_size = Response.send_file_buffer_size
|
||||||
|
Response.send_file_buffer_size = 2
|
||||||
|
res = Response.send_file('tests/files/test.txt',
|
||||||
|
content_type='text/html')
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||||
|
fd = io.BytesIO()
|
||||||
|
res.write(fd)
|
||||||
|
response = fd.getvalue()
|
||||||
|
self.assertEqual(
|
||||||
|
response,
|
||||||
|
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||||
|
Response.send_file_buffer_size = original_buffer_size
|
||||||
|
|||||||
@@ -14,6 +14,14 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
# restore original socket module
|
# restore original socket module
|
||||||
sys.modules['microdot_asyncio'].asyncio = self.original_asyncio
|
sys.modules['microdot_asyncio'].asyncio = self.original_asyncio
|
||||||
|
|
||||||
|
def _add_shutdown(self, app):
|
||||||
|
@app.route('/shutdown')
|
||||||
|
def shutdown(req):
|
||||||
|
app.shutdown()
|
||||||
|
return ''
|
||||||
|
|
||||||
|
mock_socket.add_request('GET', '/shutdown')
|
||||||
|
|
||||||
def test_get_request(self):
|
def test_get_request(self):
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
|
|
||||||
@@ -28,7 +36,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
fd2 = mock_socket.add_request('GET', '/async')
|
fd2 = mock_socket.add_request('GET', '/async')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -56,7 +65,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('POST', '/')
|
fd = mock_socket.add_request('POST', '/')
|
||||||
fd2 = mock_socket.add_request('POST', '/async')
|
fd2 = mock_socket.add_request('POST', '/async')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -94,7 +104,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/bar')
|
fd = mock_socket.add_request('GET', '/bar')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
|
||||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||||
@@ -104,7 +115,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/baz')
|
fd = mock_socket.add_request('GET', '/baz')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'X-One: 1\r\n', fd.response)
|
self.assertIn(b'X-One: 1\r\n', fd.response)
|
||||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
||||||
@@ -121,7 +133,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/foo')
|
fd = mock_socket.add_request('GET', '/foo')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 9\r\n', fd.response)
|
self.assertIn(b'Content-Length: 9\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -140,7 +153,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/foo')
|
fd = mock_socket.add_request('GET', '/foo')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -155,7 +169,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 21\r\n', fd.response)
|
self.assertIn(b'Content-Length: 21\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -174,7 +189,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
@@ -193,7 +209,8 @@ class TestMicrodotAsync(unittest.TestCase):
|
|||||||
|
|
||||||
mock_socket.clear_requests()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/')
|
fd = mock_socket.add_request('GET', '/')
|
||||||
self.assertRaises(IndexError, app.run)
|
self._add_shutdown(app)
|
||||||
|
app.run()
|
||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n'))
|
||||||
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
self.assertIn(b'Content-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ def _run(coro):
|
|||||||
class TestRequestAsync(unittest.TestCase):
|
class TestRequestAsync(unittest.TestCase):
|
||||||
def test_create_request(self):
|
def test_create_request(self):
|
||||||
fd = get_async_request_fd('GET', '/foo')
|
fd = get_async_request_fd('GET', '/foo')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
|
self.assertEqual(req.app, 'app')
|
||||||
self.assertEqual(req.client_addr, 'addr')
|
self.assertEqual(req.client_addr, 'addr')
|
||||||
self.assertEqual(req.method, 'GET')
|
self.assertEqual(req.method, 'GET')
|
||||||
self.assertEqual(req.path, '/foo')
|
self.assertEqual(req.path, '/foo')
|
||||||
@@ -35,7 +36,7 @@ class TestRequestAsync(unittest.TestCase):
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Cookie': 'foo=bar;abc=def',
|
'Cookie': 'foo=bar;abc=def',
|
||||||
'Content-Length': '3'}, body='aaa')
|
'Content-Length': '3'}, body='aaa')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
self.assertEqual(req.headers, {
|
self.assertEqual(req.headers, {
|
||||||
'Host': 'example.com:1234',
|
'Host': 'example.com:1234',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -48,33 +49,33 @@ class TestRequestAsync(unittest.TestCase):
|
|||||||
|
|
||||||
def test_args(self):
|
def test_args(self):
|
||||||
fd = get_async_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
|
fd = get_async_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
|
self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
|
||||||
self.assertEqual(req.args, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
self.assertEqual(req.args, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
||||||
|
|
||||||
def test_json(self):
|
def test_json(self):
|
||||||
fd = get_async_request_fd('GET', '/foo', headers={
|
fd = get_async_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/json'}, body='{"foo":"bar"}')
|
'Content-Type': 'application/json'}, body='{"foo":"bar"}')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
json = req.json
|
json = req.json
|
||||||
self.assertEqual(json, {'foo': 'bar'})
|
self.assertEqual(json, {'foo': 'bar'})
|
||||||
self.assertTrue(req.json is json)
|
self.assertTrue(req.json is json)
|
||||||
|
|
||||||
fd = get_async_request_fd('GET', '/foo', headers={
|
fd = get_async_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/json'}, body='[1, "2"]')
|
'Content-Type': 'application/json'}, body='[1, "2"]')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
self.assertEqual(req.json, [1, '2'])
|
self.assertEqual(req.json, [1, '2'])
|
||||||
|
|
||||||
fd = get_async_request_fd('GET', '/foo', headers={
|
fd = get_async_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/xml'}, body='[1, "2"]')
|
'Content-Type': 'application/xml'}, body='[1, "2"]')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
self.assertIsNone(req.json)
|
self.assertIsNone(req.json)
|
||||||
|
|
||||||
def test_form(self):
|
def test_form(self):
|
||||||
fd = get_async_request_fd('GET', '/foo', headers={
|
fd = get_async_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/x-www-form-urlencoded'},
|
'Content-Type': 'application/x-www-form-urlencoded'},
|
||||||
body='foo=bar&abc=def&x=%2f%%')
|
body='foo=bar&abc=def&x=%2f%%')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
form = req.form
|
form = req.form
|
||||||
self.assertEqual(form, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
self.assertEqual(form, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})
|
||||||
self.assertTrue(req.form is form)
|
self.assertTrue(req.form is form)
|
||||||
@@ -82,5 +83,5 @@ class TestRequestAsync(unittest.TestCase):
|
|||||||
fd = get_async_request_fd('GET', '/foo', headers={
|
fd = get_async_request_fd('GET', '/foo', headers={
|
||||||
'Content-Type': 'application/json'},
|
'Content-Type': 'application/json'},
|
||||||
body='foo=bar&abc=def&x=%2f%%')
|
body='foo=bar&abc=def&x=%2f%%')
|
||||||
req = _run(Request.create(fd, 'addr'))
|
req = _run(Request.create('app', fd, 'addr'))
|
||||||
self.assertIsNone(req.form)
|
self.assertIsNone(req.form)
|
||||||
|
|||||||
@@ -84,3 +84,28 @@ class TestResponseAsync(unittest.TestCase):
|
|||||||
self.assertIn(b'Content-Length: 8\r\n', fd.response)
|
self.assertIn(b'Content-Length: 8\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: application/json\r\n', fd.response)
|
self.assertIn(b'Content-Type: application/json\r\n', fd.response)
|
||||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
|
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
|
||||||
|
|
||||||
|
def test_send_file(self):
|
||||||
|
res = Response.send_file('tests/files/test.txt',
|
||||||
|
content_type='text/html')
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||||
|
fd = FakeStreamAsync()
|
||||||
|
_run(res.write(fd))
|
||||||
|
self.assertEqual(
|
||||||
|
fd.response,
|
||||||
|
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||||
|
|
||||||
|
def test_send_file_small_buffer(self):
|
||||||
|
original_buffer_size = Response.send_file_buffer_size
|
||||||
|
Response.send_file_buffer_size = 2
|
||||||
|
res = Response.send_file('tests/files/test.txt',
|
||||||
|
content_type='text/html')
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers['Content-Type'], 'text/html')
|
||||||
|
fd = FakeStreamAsync()
|
||||||
|
_run(res.write(fd))
|
||||||
|
self.assertEqual(
|
||||||
|
fd.response,
|
||||||
|
b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
|
||||||
|
Response.send_file_buffer_size = original_buffer_size
|
||||||
|
|||||||
@@ -5,38 +5,35 @@ except ImportError:
|
|||||||
|
|
||||||
from tests import mock_socket
|
from tests import mock_socket
|
||||||
|
|
||||||
_calls = []
|
|
||||||
|
|
||||||
|
|
||||||
class EventLoop:
|
|
||||||
def run_until_complete(self, coro):
|
|
||||||
_calls.append(('run_until_complete', coro))
|
|
||||||
self.coro = coro
|
|
||||||
|
|
||||||
def run_forever(self):
|
|
||||||
_calls.append(('run_forever',))
|
|
||||||
|
|
||||||
async def rf():
|
|
||||||
s = mock_socket.socket()
|
|
||||||
while True:
|
|
||||||
fd, addr = s.accept()
|
|
||||||
fd = mock_socket.FakeStreamAsync(fd)
|
|
||||||
await self.coro(fd, fd)
|
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(rf())
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
loop = EventLoop()
|
|
||||||
|
|
||||||
|
|
||||||
def get_event_loop():
|
def get_event_loop():
|
||||||
_calls.append(('get_event_loop',))
|
return asyncio.get_event_loop()
|
||||||
return loop
|
|
||||||
|
|
||||||
|
|
||||||
def start_server(cb, host, port):
|
async def start_server(cb, host, port):
|
||||||
_calls.append(('start_server', cb, host, port))
|
class MockServer:
|
||||||
return cb
|
def __init__(self):
|
||||||
|
self.closed = False
|
||||||
|
|
||||||
|
async def run(self):
|
||||||
|
s = mock_socket.socket()
|
||||||
|
while not self.closed:
|
||||||
|
fd, addr = s.accept()
|
||||||
|
fd = mock_socket.FakeStreamAsync(fd)
|
||||||
|
await cb(fd, fd)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.closed = True
|
||||||
|
|
||||||
|
async def wait_closed(self):
|
||||||
|
while not self.closed:
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
|
|
||||||
|
server = MockServer()
|
||||||
|
asyncio.get_event_loop().create_task(server.run())
|
||||||
|
return server
|
||||||
|
|
||||||
|
|
||||||
|
def run(coro):
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
return loop.run_until_complete(coro)
|
||||||
|
|||||||
@@ -6,12 +6,10 @@ except ImportError:
|
|||||||
SOL_SOCKET = 'SOL_SOCKET'
|
SOL_SOCKET = 'SOL_SOCKET'
|
||||||
SO_REUSEADDR = 'SO_REUSEADDR'
|
SO_REUSEADDR = 'SO_REUSEADDR'
|
||||||
|
|
||||||
_calls = []
|
|
||||||
_requests = []
|
_requests = []
|
||||||
|
|
||||||
|
|
||||||
def getaddrinfo(host, port):
|
def getaddrinfo(host, port):
|
||||||
_calls.append(('getaddrinfo', host, port))
|
|
||||||
return (('family', 'addr'), 'socktype', 'proto', 'canonname', 'sockaddr')
|
return (('family', 'addr'), 'socktype', 'proto', 'canonname', 'sockaddr')
|
||||||
|
|
||||||
|
|
||||||
@@ -20,19 +18,21 @@ class socket:
|
|||||||
self.request_index = 0
|
self.request_index = 0
|
||||||
|
|
||||||
def setsockopt(self, level, optname, value):
|
def setsockopt(self, level, optname, value):
|
||||||
_calls.append(('setsockopt', level, optname, value))
|
pass
|
||||||
|
|
||||||
def bind(self, addr):
|
def bind(self, addr):
|
||||||
_calls.append(('bind', addr))
|
pass
|
||||||
|
|
||||||
def listen(self, backlog):
|
def listen(self, backlog):
|
||||||
_calls.append(('listen', backlog))
|
pass
|
||||||
|
|
||||||
def accept(self):
|
def accept(self):
|
||||||
_calls.append(('accept',))
|
|
||||||
self.request_index += 1
|
self.request_index += 1
|
||||||
return _requests[self.request_index - 1], 'addr'
|
return _requests[self.request_index - 1], 'addr'
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FakeStream(io.BytesIO):
|
class FakeStream(io.BytesIO):
|
||||||
def __init__(self, input_data):
|
def __init__(self, input_data):
|
||||||
|
|||||||
25
tools/Dockerfile
Normal file
25
tools/Dockerfile
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
FROM ubuntu:latest
|
||||||
|
|
||||||
|
ARG DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y build-essential libffi-dev git pkg-config python python3 && \
|
||||||
|
rm -rf /var/lib/apt/lists/* && \
|
||||||
|
git clone https://github.com/micropython/micropython.git && \
|
||||||
|
cd micropython && \
|
||||||
|
git checkout v1.15 && \
|
||||||
|
git submodule update --init && \
|
||||||
|
cd mpy-cross && \
|
||||||
|
make && \
|
||||||
|
cd .. && \
|
||||||
|
cd ports/unix && \
|
||||||
|
make axtls && \
|
||||||
|
make && \
|
||||||
|
make test && \
|
||||||
|
make install && \
|
||||||
|
apt-get purge --auto-remove -y build-essential libffi-dev git pkg-config python python3 && \
|
||||||
|
cd ../../.. && \
|
||||||
|
rm -rf micropython
|
||||||
|
|
||||||
|
CMD ["/usr/local/bin/micropython"]
|
||||||
|
|
||||||
6
tools/update-micropython.sh
Executable file
6
tools/update-micropython.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
# this script updates the micropython binary in the /bin directory that is
|
||||||
|
# used to run unit tests under GitHub Actions builds
|
||||||
|
docker build -t micropython .
|
||||||
|
docker create -it --name dummy-micropython micropython
|
||||||
|
docker cp dummy-micropython:/usr/local/bin/micropython ../bin/micropython
|
||||||
|
docker rm dummy-micropython
|
||||||
19
tox.ini
19
tox.ini
@@ -1,28 +1,29 @@
|
|||||||
[tox]
|
[tox]
|
||||||
envlist=flake8,py35,py36,py37,upy
|
envlist=flake8,py36,py37,py38,py39,upy
|
||||||
skipsdist=True
|
skipsdist=True
|
||||||
skip_missing_interpreters=True
|
skip_missing_interpreters=True
|
||||||
|
|
||||||
|
[gh-actions]
|
||||||
|
python =
|
||||||
|
3.6: py36
|
||||||
|
3.7: py37
|
||||||
|
3.8: py38
|
||||||
|
3.9: py39
|
||||||
|
pypy3: pypy3
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
commands=
|
commands=
|
||||||
pip install -e microdot
|
pip install -e microdot
|
||||||
pip install -e microdot-asyncio
|
pip install -e microdot-asyncio
|
||||||
coverage run --branch --include="microdot*.py" -m unittest tests
|
coverage run --branch --include="microdot*.py" -m unittest tests
|
||||||
coverage report --show-missing
|
coverage report --show-missing
|
||||||
coverage erase
|
|
||||||
deps=coverage
|
deps=coverage
|
||||||
basepython=
|
|
||||||
flake8: python3.7
|
|
||||||
py35: python3.5
|
|
||||||
py36: python3.6
|
|
||||||
py37: python3.7
|
|
||||||
upy: python3.7
|
|
||||||
|
|
||||||
[testenv:flake8]
|
[testenv:flake8]
|
||||||
deps=
|
deps=
|
||||||
flake8
|
flake8
|
||||||
commands=
|
commands=
|
||||||
flake8 --exclude tests/libs microdot microdot-asyncio tests
|
flake8 --ignore=W503 --exclude tests/libs microdot microdot-asyncio tests
|
||||||
|
|
||||||
[testenv:upy]
|
[testenv:upy]
|
||||||
whitelist_externals=sh
|
whitelist_externals=sh
|
||||||
|
|||||||
Reference in New Issue
Block a user