Skip to content

Commit

Permalink
Pass keyword arguments to thread executor in the correct way (Fixes #195
Browse files Browse the repository at this point in the history
)
  • Loading branch information
miguelgrinberg committed Jan 7, 2024
1 parent c8c91e8 commit 6712c47
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/microdot/microdot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

try:
from inspect import iscoroutinefunction, iscoroutine
from functools import partial

async def invoke_handler(handler, *args, **kwargs):
"""Invoke a handler and return the result.
Expand All @@ -23,7 +24,7 @@ async def invoke_handler(handler, *args, **kwargs):
ret = await handler(*args, **kwargs)
else:
ret = await asyncio.get_running_loop().run_in_executor(
None, handler, *args, **kwargs)
None, partial(handler, *args, **kwargs))
return ret
except ImportError: # pragma: no cover
def iscoroutine(coro):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_microdot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def index(req):
async def index2(req):
return 'foo-async'

@app.route('/arg/<id>')
def index3(req, id):
return id

@app.route('/arg/async/<id>')
async def index4(req, id):
return f'async-{id}'

client = TestClient(app)

res = self._run(client.get('/'))
Expand All @@ -45,6 +53,24 @@ async def index2(req):
self.assertEqual(res.body, b'foo-async')
self.assertEqual(res.json, None)

res = self._run(client.get('/arg/123'))
self.assertEqual(res.status_code, 200)
self.assertEqual(res.headers['Content-Type'],
'text/plain; charset=UTF-8')
self.assertEqual(res.headers['Content-Length'], '3')
self.assertEqual(res.text, '123')
self.assertEqual(res.body, b'123')
self.assertEqual(res.json, None)

res = self._run(client.get('/arg/async/123'))
self.assertEqual(res.status_code, 200)
self.assertEqual(res.headers['Content-Type'],
'text/plain; charset=UTF-8')
self.assertEqual(res.headers['Content-Length'], '9')
self.assertEqual(res.text, 'async-123')
self.assertEqual(res.body, b'async-123')
self.assertEqual(res.json, None)

def test_post_request(self):
app = Microdot()

Expand Down

0 comments on commit 6712c47

Please sign in to comment.