Repository: cool-RR/PySnooper

Issue:
Feature request: Show the return value

Discussion:

Patch:
diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py
index cc6a90d..9ad85c9 100644
--- a/pysnooper/tracer.py
+++ b/pysnooper/tracer.py
@@ -213,6 +213,8 @@ class Tracer:
 
         self.write('{indent}{now_string} {event:9} '
                    '{line_no:4} {source_line}'.format(**locals()))
+        if event == 'return':
+            self.write('{indent}{now_string} return_value {line_no:4} {arg}'.format(**locals()))
         return self.trace
 
 


Test patch:
diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py
index 4ba26c4..d0ff913 100644
--- a/tests/test_pysnooper.py
+++ b/tests/test_pysnooper.py
@@ -12,7 +12,7 @@ from python_toolbox import temp_file_tools
 import pysnooper
 
 from .utils import (assert_output, VariableEntry, CallEntry, LineEntry,
-                    ReturnEntry, OpcodeEntry, ExceptionEntry)
+                    ReturnEntry, OpcodeEntry, Return_valueEntry, ExceptionEntry)
 
 
 def test_string_io():
@@ -36,6 +36,7 @@ def test_string_io():
             VariableEntry('y', '8'),
             LineEntry('return y + x'),
             ReturnEntry('return y + x'),
+            Return_valueEntry('15'),
         )
     )
 
@@ -78,6 +79,7 @@ def test_variables():
             VariableEntry('foo.x', '16'),
             LineEntry(),
             ReturnEntry(),
+            Return_valueEntry('None')
         )
     )
 
@@ -125,14 +127,17 @@ def test_depth():
             VariableEntry(),
             LineEntry(),
             ReturnEntry(),
+            Return_valueEntry('20'),
 
             VariableEntry(),
             LineEntry(),
             ReturnEntry(),
+            Return_valueEntry('20'),
 
             VariableEntry(),
             LineEntry(),
             ReturnEntry(),
+            Return_valueEntry('20'),
         )
     )
 
@@ -167,6 +172,7 @@ def test_method_and_prefix():
             LineEntry('self.x **= 2'),
             LineEntry(),
             ReturnEntry(),
+            Return_valueEntry(),
         ),
         prefix='ZZZ'
     )
@@ -194,6 +200,7 @@ def test_file_output():
                 VariableEntry('y', '8'),
                 LineEntry('return y + x'),
                 ReturnEntry('return y + x'),
+                Return_valueEntry('15'),
             )
         )
 
@@ -229,8 +236,10 @@ def test_confusing_decorator_lines():
             CallEntry('x = lambda bar: 7'),
             LineEntry(),
             ReturnEntry(),
+            Return_valueEntry('7'),
             # back in my_function
             ReturnEntry(),
+            Return_valueEntry('15'),
         )
     )
 
@@ -248,5 +257,6 @@ def test_lambda():
             CallEntry(source_regex='^my_function = pysnooper.*'),
             LineEntry(source_regex='^my_function = pysnooper.*'),
             ReturnEntry(source_regex='^my_function = pysnooper.*'),
+            Return_valueEntry('49'),
         )
     )
diff --git a/tests/utils.py b/tests/utils.py
index fc8242a..f704215 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -81,13 +81,13 @@ class _BaseEventEntry(_BaseEntry):
 
     line_pattern = re.compile(
         (r"""^(?P<prefix>.*?)(?P<indent>(?: {4})*)[0-9:.]{15} """
-         r"""(?P<event_name>[a-z]*) +(?P<line_number>[0-9]*) """
+         r"""(?P<event_name>[a-z_]*) +(?P<line_number>[0-9]*) """
          r"""+(?P<source>.*)$""")
     )
 
     @caching.CachedProperty
     def event_name(self):
-        return re.match('^[A-Z][a-z]*', type(self).__name__).group(0).lower()
+        return re.match('^[A-Z][a-z_]*', type(self).__name__).group(0).lower()
 
     def _check_source(self, source):
         if self.source is not None:
@@ -121,6 +121,8 @@ class ExceptionEntry(_BaseEventEntry):
 class OpcodeEntry(_BaseEventEntry):
     pass
 
+class Return_valueEntry(_BaseEventEntry):
+    pass
 
 class OutputFailure(Exception):
     pass