diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 6413215..a0d787b 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -104,9 +104,9 @@ from IPython.core.display_trap import DisplayTrap from IPython.core.excolors import exception_colors from IPython.utils import PyColorize from IPython.utils import io +from IPython.utils import openpy from IPython.utils import path as util_path from IPython.utils import py3compat -from IPython.utils import pyfile from IPython.utils import ulinecache from IPython.utils.data import uniq_stable from IPython.utils.warn import info, error @@ -837,7 +837,7 @@ class VerboseTB(TBTools): continue elif file.endswith(('.pyc','.pyo')): # Look up the corresponding source file. - file = pyfile.source_from_cache(file) + file = openpy.source_from_cache(file) def linereader(file=file, lnum=[lnum], getline=ulinecache.getline): line = getline(file, lnum[0]) diff --git a/IPython/extensions/autoreload.py b/IPython/extensions/autoreload.py index 947d4a1..f1cec38 100644 --- a/IPython/extensions/autoreload.py +++ b/IPython/extensions/autoreload.py @@ -119,7 +119,7 @@ try: except NameError: from imp import reload -from IPython.utils import pyfile +from IPython.utils import openpy from IPython.utils.py3compat import PY3 #------------------------------------------------------------------------------ @@ -207,12 +207,12 @@ class ModuleReloader(object): path, ext = os.path.splitext(filename) if ext.lower() == '.py': - pyc_filename = pyfile.cache_from_source(filename) + pyc_filename = openpy.cache_from_source(filename) py_filename = filename else: pyc_filename = filename try: - py_filename = pyfile.source_from_cache(filename) + py_filename = openpy.source_from_cache(filename) except ValueError: continue diff --git a/IPython/utils/openpy.py b/IPython/utils/openpy.py index 1b3d8b5..dde5229 100644 --- a/IPython/utils/openpy.py +++ b/IPython/utils/openpy.py @@ -8,6 +8,7 @@ from __future__ import absolute_import import io from io import TextIOWrapper, BytesIO +import os.path import re cookie_re = re.compile(ur"coding[:=]\s*([-\w.]+)", re.UNICODE) @@ -217,3 +218,22 @@ def _list_readline(x): def readline(): return next(x) return readline + +# Code for going between .py files and cached .pyc files ---------------------- + +try: # Python 3.2, see PEP 3147 + from imp import source_from_cache, cache_from_source +except ImportError: + # Python <= 3.1: .pyc files go next to .py + def source_from_cache(path): + basename, ext = os.path.splitext(path) + if ext not in ('.pyc', '.pyo'): + raise ValueError('Not a cached Python file extension', ext) + # Should we look for .pyw files? + return basename + '.py' + + def cache_from_source(path, debug_override=None): + if debug_override is None: + debug_override = __debug__ + basename, ext = os.path.splitext(path) + return basename + '.pyc' if debug_override else '.pyo' diff --git a/IPython/utils/pyfile.py b/IPython/utils/pyfile.py deleted file mode 100644 index e663c81..0000000 --- a/IPython/utils/pyfile.py +++ /dev/null @@ -1,23 +0,0 @@ -"""Utilities for working with Python source files. - -Exposes various functions from recent Python standard libraries, along with -equivalents for older Python versions. -""" -import os.path - -try: # Python 3.2 - from imp import source_from_cache, cache_from_source -except ImportError: - # Python <= 3.1: .pyc files go next to .py - def source_from_cache(path): - basename, ext = os.path.splitext(path) - if ext not in ('.pyc', '.pyo'): - raise ValueError('Not a cached Python file extension', ext) - # Should we look for .pyw files? - return basename + '.py' - - def cache_from_source(path, debug_override=None): - if debug_override is None: - debug_override = __debug__ - basename, ext = os.path.splitext(path) - return basename + '.pyc' if debug_override else '.pyo'