Skip to content

Commit

Permalink
Merge pull request ipython#1289 from takluyver/autoreload-py3
Browse files Browse the repository at this point in the history
Make autoreload extension work on Python 3.
  • Loading branch information
takluyver committed Jan 20, 2012
2 parents 02c8aa5 + 7599ae7 commit 7657369
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
43 changes: 30 additions & 13 deletions IPython/extensions/autoreload.py
Expand Up @@ -108,6 +108,9 @@
except NameError:
from imp import reload

from IPython.utils import pyfile
from IPython.utils.py3compat import PY3

def _get_compiled_ext():
"""Official way to get the extension of compiled files (.pyc or .pyo)"""
for ext, mode, typ in imp.get_suffixes():
Expand Down Expand Up @@ -198,14 +201,14 @@ def check(self, check_all=False):

if ext.lower() == '.py':
ext = PY_COMPILED_EXT
pyc_filename = path + PY_COMPILED_EXT
pyc_filename = pyfile.cache_from_source(filename)
py_filename = filename
else:
pyc_filename = filename
py_filename = filename[:-1]

if ext != PY_COMPILED_EXT:
continue
try:
py_filename = pyfile.source_from_cache(filename)
except ValueError:
continue

try:
pymtime = os.stat(py_filename).st_mtime
Expand All @@ -229,10 +232,16 @@ def check(self, check_all=False):
# superreload
#------------------------------------------------------------------------------

if PY3:
func_attrs = ['__code__', '__defaults__', '__doc__',
'__closure__', '__globals__', '__dict__']
else:
func_attrs = ['func_code', 'func_defaults', 'func_doc',
'func_closure', 'func_globals', 'func_dict']

def update_function(old, new):
"""Upgrade the code object of a function"""
for name in ['func_code', 'func_defaults', 'func_doc',
'func_closure', 'func_globals', 'func_dict']:
for name in func_attrs:
try:
setattr(old, name, getattr(new, name))
except (AttributeError, TypeError):
Expand Down Expand Up @@ -271,18 +280,26 @@ def isinstance2(a, b, typ):
return isinstance(a, typ) and isinstance(b, typ)

UPDATE_RULES = [
(lambda a, b: isinstance2(a, b, types.ClassType),
update_class),
(lambda a, b: isinstance2(a, b, types.TypeType),
(lambda a, b: isinstance2(a, b, type),
update_class),
(lambda a, b: isinstance2(a, b, types.FunctionType),
update_function),
(lambda a, b: isinstance2(a, b, property),
update_property),
(lambda a, b: isinstance2(a, b, types.MethodType),
lambda a, b: update_function(a.im_func, b.im_func)),
]

if PY3:
UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
lambda a, b: update_function(a.__func__, b.__func__)),
])
else:
UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType),
update_class),
(lambda a, b: isinstance2(a, b, types.MethodType),
lambda a, b: update_function(a.im_func, b.im_func)),
])


def update_generic(a, b):
for type_check, update in UPDATE_RULES:
if type_check(a, b):
Expand Down Expand Up @@ -317,7 +334,7 @@ def superreload(module, reload=reload, old_objects={}):
except TypeError:
# weakref doesn't work for all types;
# create strong references for 'important' cases
if isinstance(obj, types.ClassType):
if not PY3 and isinstance(obj, types.ClassType):
old_objects.setdefault(key, []).append(StrongRef(obj))

# reload module
Expand Down
5 changes: 0 additions & 5 deletions IPython/extensions/tests/test_autoreload.py
Expand Up @@ -11,7 +11,6 @@

from IPython.extensions.autoreload import AutoreloadInterface
from IPython.core.hooks import TryNext
from IPython.testing.decorators import knownfailureif

#-----------------------------------------------------------------------------
# Test fixture
Expand Down Expand Up @@ -294,12 +293,8 @@ def check_module_contents():
self.shell.run_code("pass") # trigger reload
nt.assert_equal(mod.x, -99)

# The autoreload extension needs to be updated for Python 3.2, as .pyc files
# are stored in a different location. See gh-846.
@knownfailureif(sys.version_info >= (3,2))
def test_smoketest_aimport(self):
self._check_smoketest(use_aimport=True)

@knownfailureif(sys.version_info >= (3,2))
def test_smoketest_autoreload(self):
self._check_smoketest(use_aimport=False)

0 comments on commit 7657369

Please sign in to comment.