Skip to content

Commit

Permalink
Merge pull request #137 from pyutils/dev/fix-coroutines
Browse files Browse the repository at this point in the history
Fix coroutines
  • Loading branch information
Erotemic authored Apr 1, 2022
2 parents 2ac329f + 9ab8ac2 commit 8788d13
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changes
=======

3.5.1
~~~~~
* FIX: #19 line profiler now works on async functions again

3.5.0
~~~~~
* FIX: #109 kernprof fails to write to stdout if stdout was replaced
Expand Down
2 changes: 1 addition & 1 deletion kernprof.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# NOTE: This version needs to be manually maintained with the line_profiler
# __version__ for now.
__version__ = '3.5.0'
__version__ = '3.5.1'

# Guard the import of cProfile such that 3.x people
# without lsprof can still use this script.
Expand Down
20 changes: 18 additions & 2 deletions line_profiler/line_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
f'Has it been compiled? Underlying error is ex={ex!r}'
)

__version__ = '3.5.0'
__version__ = '3.5.1'


def load_ipython_extension(ip):
Expand All @@ -27,7 +27,7 @@ def load_ipython_extension(ip):


def is_coroutine(f):
return False
return inspect.iscoroutinefunction(f)


CO_GENERATOR = 0x0020
Expand Down Expand Up @@ -57,6 +57,22 @@ def __call__(self, func):
wrapper = self.wrap_function(func)
return wrapper

def wrap_coroutine(self, func):
"""
Wrap a Python 3.5 coroutine to profile it.
"""

@functools.wraps(func)
async def wrapper(*args, **kwds):
self.enable_by_count()
try:
result = await func(*args, **kwds)
finally:
self.disable_by_count()
return result

return wrapper

def wrap_generator(self, func):
""" Wrap a generator to profile it.
"""
Expand Down

0 comments on commit 8788d13

Please sign in to comment.