prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Clean library/frameworks from backtraces
  We should clean all non-user code from backtraces as much as possible.  Here's a heroku app running 2.13.0:
  
  <img width="890" alt="Screenshot 2020-04-13 14 39 56" src="https://user-images.githubusercontent.com/102774/79160522-26a36e80-7d97-11ea-8376-f8dfcfd6ece0.png">
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/scout_apm/core/backtrace.py`
  Content:
  ```
  1 # coding=utf-8
  2 from __future__ import absolute_import, division, print_function, unicode_literals
  3 
  4 import sys
  5 import traceback
  6 
  7 # Maximum non-Scout frames to target retrieving
  8 LIMIT = 50
  9 # How many upper frames from inside Scout to ignore
  10 IGNORED = 1
  11 
  12 
  13 if sys.version_info >= (3, 5):
  14 
  15     def capture():
  16         return [
  17             {"file": frame.filename, "line": frame.lineno, "function": frame.name}
  18             for frame in reversed(
  19                 traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]
  20             )
  21         ]
  22 
  23 
  24 else:
  25 
  26     def capture():
  27         return [
  28             {"file": frame[0], "line": frame[1], "function": frame[3]}
  29             for frame in reversed(
  30                 traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]
  31             )
  32         ]
  33
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/scout_apm/core/backtrace.py b/src/scout_apm/core/backtrace.py
  --- a/src/scout_apm/core/backtrace.py
  +++ b/src/scout_apm/core/backtrace.py
  @@ -1,7 +1,9 @@
   # coding=utf-8
   from __future__ import absolute_import, division, print_function, unicode_literals
   
  +import itertools
   import sys
  +import sysconfig
   import traceback
   
   # Maximum non-Scout frames to target retrieving
  @@ -10,23 +12,55 @@
   IGNORED = 1
   
   
  +def filter_frames(frames):
  +    """Filter the stack trace frames down to non-library code."""
  +    paths = sysconfig.get_paths()
  +    library_paths = {paths["purelib"], paths["platlib"]}
  +    for frame in frames:
  +        if not any(frame["file"].startswith(exclusion) for exclusion in library_paths):
  +            yield frame
  +
  +
   if sys.version_info >= (3, 5):
   
  +    def frame_walker():
  +        """Iterate over each frame of the stack.
  +
  +        Taken from python3/traceback.ExtractSummary.extract to support
  +        iterating over the entire stack, but without creating a large
  +        data structure.
  +        """
  +        for frame, lineno in traceback.walk_stack(sys._getframe().f_back):
  +            co = frame.f_code
  +            filename = co.co_filename
  +            name = co.co_name
  +            yield {"file": filename, "line": lineno, "function": name}
  +
       def capture():
  -        return [
  -            {"file": frame.filename, "line": frame.lineno, "function": frame.name}
  -            for frame in reversed(
  -                traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]
  -            )
  -        ]
  +        return list(itertools.islice(filter_frames(frame_walker()), LIMIT))
   
   
   else:
   
  +    def frame_walker():
  +        """Iterate over each frame of the stack.
  +
  +        Taken from python2.7/traceback.extract_stack to support iterating
  +        over the entire stack, but without creating a large data structure.
  +        """
  +        try:
  +            raise ZeroDivisionError
  +        except ZeroDivisionError:
  +            # Get the current frame
  +            f = sys.exc_info()[2].tb_frame.f_back
  +
  +        while f is not None:
  +            lineno = f.f_lineno
  +            co = f.f_code
  +            filename = co.co_filename
  +            name = co.co_name
  +            yield {"file": filename, "line": lineno, "function": name}
  +            f = f.f_back
  +
       def capture():
  -        return [
  -            {"file": frame[0], "line": frame[1], "function": frame[3]}
  -            for frame in reversed(
  -                traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]
  -            )
  -        ]
  +        return list(itertools.islice(filter_frames(frame_walker()), LIMIT))
  
verification_info: {"golden_diff": "diff --git a/src/scout_apm/core/backtrace.py b/src/scout_apm/core/backtrace.py\n--- a/src/scout_apm/core/backtrace.py\n+++ b/src/scout_apm/core/backtrace.py\n@@ -1,7 +1,9 @@\n # coding=utf-8\n from __future__ import absolute_import, division, print_function, unicode_literals\n \n+import itertools\n import sys\n+import sysconfig\n import traceback\n \n # Maximum non-Scout frames to target retrieving\n@@ -10,23 +12,55 @@\n IGNORED = 1\n \n \n+def filter_frames(frames):\n+    \"\"\"Filter the stack trace frames down to non-library code.\"\"\"\n+    paths = sysconfig.get_paths()\n+    library_paths = {paths[\"purelib\"], paths[\"platlib\"]}\n+    for frame in frames:\n+        if not any(frame[\"file\"].startswith(exclusion) for exclusion in library_paths):\n+            yield frame\n+\n+\n if sys.version_info >= (3, 5):\n \n+    def frame_walker():\n+        \"\"\"Iterate over each frame of the stack.\n+\n+        Taken from python3/traceback.ExtractSummary.extract to support\n+        iterating over the entire stack, but without creating a large\n+        data structure.\n+        \"\"\"\n+        for frame, lineno in traceback.walk_stack(sys._getframe().f_back):\n+            co = frame.f_code\n+            filename = co.co_filename\n+            name = co.co_name\n+            yield {\"file\": filename, \"line\": lineno, \"function\": name}\n+\n     def capture():\n-        return [\n-            {\"file\": frame.filename, \"line\": frame.lineno, \"function\": frame.name}\n-            for frame in reversed(\n-                traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]\n-            )\n-        ]\n+        return list(itertools.islice(filter_frames(frame_walker()), LIMIT))\n \n \n else:\n \n+    def frame_walker():\n+        \"\"\"Iterate over each frame of the stack.\n+\n+        Taken from python2.7/traceback.extract_stack to support iterating\n+        over the entire stack, but without creating a large data structure.\n+        \"\"\"\n+        try:\n+            raise ZeroDivisionError\n+        except ZeroDivisionError:\n+            # Get the current frame\n+            f = sys.exc_info()[2].tb_frame.f_back\n+\n+        while f is not None:\n+            lineno = f.f_lineno\n+            co = f.f_code\n+            filename = co.co_filename\n+            name = co.co_name\n+            yield {\"file\": filename, \"line\": lineno, \"function\": name}\n+            f = f.f_back\n+\n     def capture():\n-        return [\n-            {\"file\": frame[0], \"line\": frame[1], \"function\": frame[3]}\n-            for frame in reversed(\n-                traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]\n-            )\n-        ]\n+        return list(itertools.islice(filter_frames(frame_walker()), LIMIT))\n", "issue": "Clean library/frameworks from backtraces\nWe should clean all non-user code from backtraces as much as possible.  Here's a heroku app running 2.13.0:\r\n\r\n<img width=\"890\" alt=\"Screenshot 2020-04-13 14 39 56\" src=\"https://user-images.githubusercontent.com/102774/79160522-26a36e80-7d97-11ea-8376-f8dfcfd6ece0.png\">\r\n\n", "before_files": [{"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport sys\nimport traceback\n\n# Maximum non-Scout frames to target retrieving\nLIMIT = 50\n# How many upper frames from inside Scout to ignore\nIGNORED = 1\n\n\nif sys.version_info >= (3, 5):\n\n    def capture():\n        return [\n            {\"file\": frame.filename, \"line\": frame.lineno, \"function\": frame.name}\n            for frame in reversed(\n                traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]\n            )\n        ]\n\n\nelse:\n\n    def capture():\n        return [\n            {\"file\": frame[0], \"line\": frame[1], \"function\": frame[3]}\n            for frame in reversed(\n                traceback.extract_stack(limit=LIMIT + IGNORED)[:-IGNORED]\n            )\n        ]\n", "path": "src/scout_apm/core/backtrace.py"}], "after_files": [{"content": "# coding=utf-8\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nimport itertools\nimport sys\nimport sysconfig\nimport traceback\n\n# Maximum non-Scout frames to target retrieving\nLIMIT = 50\n# How many upper frames from inside Scout to ignore\nIGNORED = 1\n\n\ndef filter_frames(frames):\n    \"\"\"Filter the stack trace frames down to non-library code.\"\"\"\n    paths = sysconfig.get_paths()\n    library_paths = {paths[\"purelib\"], paths[\"platlib\"]}\n    for frame in frames:\n        if not any(frame[\"file\"].startswith(exclusion) for exclusion in library_paths):\n            yield frame\n\n\nif sys.version_info >= (3, 5):\n\n    def frame_walker():\n        \"\"\"Iterate over each frame of the stack.\n\n        Taken from python3/traceback.ExtractSummary.extract to support\n        iterating over the entire stack, but without creating a large\n        data structure.\n        \"\"\"\n        for frame, lineno in traceback.walk_stack(sys._getframe().f_back):\n            co = frame.f_code\n            filename = co.co_filename\n            name = co.co_name\n            yield {\"file\": filename, \"line\": lineno, \"function\": name}\n\n    def capture():\n        return list(itertools.islice(filter_frames(frame_walker()), LIMIT))\n\n\nelse:\n\n    def frame_walker():\n        \"\"\"Iterate over each frame of the stack.\n\n        Taken from python2.7/traceback.extract_stack to support iterating\n        over the entire stack, but without creating a large data structure.\n        \"\"\"\n        try:\n            raise ZeroDivisionError\n        except ZeroDivisionError:\n            # Get the current frame\n            f = sys.exc_info()[2].tb_frame.f_back\n\n        while f is not None:\n            lineno = f.f_lineno\n            co = f.f_code\n            filename = co.co_filename\n            name = co.co_name\n            yield {\"file\": filename, \"line\": lineno, \"function\": name}\n            f = f.f_back\n\n    def capture():\n        return list(itertools.islice(filter_frames(frame_walker()), LIMIT))\n", "path": "src/scout_apm/core/backtrace.py"}]}