diff options
author | David Cramer <dcramer@gmail.com> | 2015-08-07 18:08:27 -0700 |
---|---|---|
committer | David Cramer <dcramer@gmail.com> | 2015-10-22 20:45:53 +0100 |
commit | a93f8201fe08af37d204153bad3dbb20c11ca973 (patch) | |
tree | 1c9e86a4f7b61530a45dc3c06da4b2a8c996ae1e /raven/utils | |
parent | 4d34f712f2216a75e783f9f76a9dd3c08fb4839e (diff) | |
download | raven-a93f8201fe08af37d204153bad3dbb20c11ca973.tar.gz |
Switch source reading to linecache
Diffstat (limited to 'raven/utils')
-rw-r--r-- | raven/utils/stacks.py | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/raven/utils/stacks.py b/raven/utils/stacks.py index 8dc24a1..62791ff 100644 --- a/raven/utils/stacks.py +++ b/raven/utils/stacks.py @@ -8,6 +8,7 @@ raven.utils.stacks from __future__ import absolute_import import inspect +import linecache import re import sys import warnings @@ -46,28 +47,15 @@ def get_lines_from_file(filename, lineno, context_lines, loader=None, module_nam source = None if source is not None: source = source.splitlines() + if source is None: try: - f = open(filename, 'rb') - try: - source = f.readlines() - finally: - f.close() + source = linecache.getlines(filename) except (OSError, IOError): - pass - - if source is None: return None, None, None - encoding = 'utf8' - for line in source[:2]: - # File coding may be specified. Match pattern from PEP-263 - # (http://www.python.org/dev/peps/pep-0263/) - match = _coding_re.search(line.decode('utf8')) # let's assume utf8 - if match: - encoding = match.group(1) - break - source = [six.text_type(sline, encoding, 'replace') for sline in source] + if not source: + return None, None, None lower_bound = max(0, lineno - context_lines) upper_bound = min(lineno + 1 + context_lines, len(source)) |