File: patch_source_cache.py

package info (click to toggle)
python-friendly 0.7.21%2Bgit20230418.fe5d3a2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,268 kB
  • sloc: python: 2,291; makefile: 6
file content (37 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import linecache
from idlelib import rpc

from friendly_traceback import source_cache


def _get_lines(filename, linenumber=None):
    rpchandler = rpc.objecttable["exec"].rpchandler
    lines = rpchandler.remotecall("linecache", "getlines", (filename, None), {})
    new_lines = []
    for line in lines:
        if not line.endswith("\n"):
            line += "\n"
        if filename.startswith("<pyshell#") and line.startswith("\t"):
            # Remove extra indentation added in the shell (\t == 8 spaces)
            line = "    " + line[1:]
        new_lines.append(line)
    if linenumber is None:
        return new_lines
    return new_lines[linenumber - 1 : linenumber]


old_get_lines = source_cache.cache.get_source_lines


def new_get_lines(filename, module_globals=None):
    """Intended to replace the undocumented linecache.getlines, with the
       same signature.
    """
    lines = _get_lines(filename)
    if not lines:
        lines = old_get_lines(filename=filename, module_globals=module_globals)
    return lines


source_cache.cache.get_source_lines = new_get_lines
linecache.getlines = new_get_lines