Skip to content

Commit

Permalink
better stack trace in run_in_greenlet decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Nov 28, 2017
1 parent 76f8d4b commit b325884
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pulsar/apps/greenio/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from functools import wraps
from inspect import isawaitable

Expand Down Expand Up @@ -38,8 +39,9 @@ async def _(*args, **kwargs):
# keep on switching back to the greenlet if we get a Future
try:
result = green.switch((await result))
except Exception as exc:
result = green.throw(exc)
except Exception:
exc_info = sys.exc_info()
result = green.throw(*exc_info)

return green.switch(result)

Expand Down
21 changes: 21 additions & 0 deletions tests/apps/test_greenio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
import unittest
import asyncio
import traceback
from unittest import mock

from pulsar.api import send, create_future
Expand Down Expand Up @@ -168,6 +170,25 @@ def test_green_http(self):
self.assertEqual(response.status_code, 200)
self.assertTrue(response.text)

async def test_run_in_greenlet_error(self):

def error():
return 'foo' + 1

@run_in_greenlet
def green_test():
return error()

try:
await green_test()
except TypeError:
exc = sys.exc_info()
else:
raise RuntimeError
info = traceback.format_tb(exc[2])
self.assertEqual(len(info), 4)
self.assertTrue(info[3].endswith("return 'foo' + 1\n"))


async def async_function(test):
future = create_future()
Expand Down

0 comments on commit b325884

Please sign in to comment.