Show More
@@ -0,0 +1,23 b'' | |||||
|
1 | """Utilities for working with Python source files. | |||
|
2 | ||||
|
3 | Exposes various functions from recent Python standard libraries, along with | |||
|
4 | equivalents for older Python versions. | |||
|
5 | """ | |||
|
6 | import os.path | |||
|
7 | ||||
|
8 | try: # Python 3.2 | |||
|
9 | from imp import source_from_cache, cache_from_source | |||
|
10 | except ImportError: | |||
|
11 | # Python <= 3.1: .pyc files go next to .py | |||
|
12 | def source_from_cache(path): | |||
|
13 | basename, ext = os.path.splitext(path) | |||
|
14 | if ext not in {'.pyc', '.pyo'}: | |||
|
15 | raise ValueError('Not a cached Python file extension', ext) | |||
|
16 | # Should we look for .pyw files? | |||
|
17 | return basename + '.py' | |||
|
18 | ||||
|
19 | def cache_from_source(path, debug_override=None): | |||
|
20 | if debug_override is None: | |||
|
21 | debug_override = __debug__ | |||
|
22 | basename, ext = os.path.splitext(path) | |||
|
23 | return basename + '.pyc' if debug_override else '.pyo' |
@@ -100,6 +100,7 b' from IPython.core.excolors import exception_colors' | |||||
100 | from IPython.utils import PyColorize |
|
100 | from IPython.utils import PyColorize | |
101 | from IPython.utils import io |
|
101 | from IPython.utils import io | |
102 | from IPython.utils import py3compat |
|
102 | from IPython.utils import py3compat | |
|
103 | from IPython.utils import pyfile | |||
103 | from IPython.utils.data import uniq_stable |
|
104 | from IPython.utils.data import uniq_stable | |
104 | from IPython.utils.warn import info, error |
|
105 | from IPython.utils.warn import info, error | |
105 |
|
106 | |||
@@ -873,6 +874,8 b' class VerboseTB(TBTools):' | |||||
873 | tokeneater.name_cont = False |
|
874 | tokeneater.name_cont = False | |
874 |
|
875 | |||
875 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
876 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): | |
|
877 | if file.endswith(('.pyc','.pyo')): | |||
|
878 | file = pyfile.source_from_cache(file) | |||
876 | line = getline(file, lnum[0]) |
|
879 | line = getline(file, lnum[0]) | |
877 | lnum[0] += 1 |
|
880 | lnum[0] += 1 | |
878 | return line |
|
881 | return line |
General Comments 0
You need to be logged in to leave comments.
Login now