##// END OF EJS Templates
Merge pull request #1289 from takluyver/autoreload-py3...
Thomas -
r5929:34720727 merge
parent child Browse files
Show More
@@ -108,6 +108,9 b' try:'
108 except NameError:
108 except NameError:
109 from imp import reload
109 from imp import reload
110
110
111 from IPython.utils import pyfile
112 from IPython.utils.py3compat import PY3
113
111 def _get_compiled_ext():
114 def _get_compiled_ext():
112 """Official way to get the extension of compiled files (.pyc or .pyo)"""
115 """Official way to get the extension of compiled files (.pyc or .pyo)"""
113 for ext, mode, typ in imp.get_suffixes():
116 for ext, mode, typ in imp.get_suffixes():
@@ -198,14 +201,14 b' class ModuleReloader(object):'
198
201
199 if ext.lower() == '.py':
202 if ext.lower() == '.py':
200 ext = PY_COMPILED_EXT
203 ext = PY_COMPILED_EXT
201 pyc_filename = path + PY_COMPILED_EXT
204 pyc_filename = pyfile.cache_from_source(filename)
202 py_filename = filename
205 py_filename = filename
203 else:
206 else:
204 pyc_filename = filename
207 pyc_filename = filename
205 py_filename = filename[:-1]
208 try:
206
209 py_filename = pyfile.source_from_cache(filename)
207 if ext != PY_COMPILED_EXT:
210 except ValueError:
208 continue
211 continue
209
212
210 try:
213 try:
211 pymtime = os.stat(py_filename).st_mtime
214 pymtime = os.stat(py_filename).st_mtime
@@ -229,10 +232,16 b' class ModuleReloader(object):'
229 # superreload
232 # superreload
230 #------------------------------------------------------------------------------
233 #------------------------------------------------------------------------------
231
234
235 if PY3:
236 func_attrs = ['__code__', '__defaults__', '__doc__',
237 '__closure__', '__globals__', '__dict__']
238 else:
239 func_attrs = ['func_code', 'func_defaults', 'func_doc',
240 'func_closure', 'func_globals', 'func_dict']
241
232 def update_function(old, new):
242 def update_function(old, new):
233 """Upgrade the code object of a function"""
243 """Upgrade the code object of a function"""
234 for name in ['func_code', 'func_defaults', 'func_doc',
244 for name in func_attrs:
235 'func_closure', 'func_globals', 'func_dict']:
236 try:
245 try:
237 setattr(old, name, getattr(new, name))
246 setattr(old, name, getattr(new, name))
238 except (AttributeError, TypeError):
247 except (AttributeError, TypeError):
@@ -271,18 +280,26 b' def isinstance2(a, b, typ):'
271 return isinstance(a, typ) and isinstance(b, typ)
280 return isinstance(a, typ) and isinstance(b, typ)
272
281
273 UPDATE_RULES = [
282 UPDATE_RULES = [
274 (lambda a, b: isinstance2(a, b, types.ClassType),
283 (lambda a, b: isinstance2(a, b, type),
275 update_class),
276 (lambda a, b: isinstance2(a, b, types.TypeType),
277 update_class),
284 update_class),
278 (lambda a, b: isinstance2(a, b, types.FunctionType),
285 (lambda a, b: isinstance2(a, b, types.FunctionType),
279 update_function),
286 update_function),
280 (lambda a, b: isinstance2(a, b, property),
287 (lambda a, b: isinstance2(a, b, property),
281 update_property),
288 update_property),
282 (lambda a, b: isinstance2(a, b, types.MethodType),
283 lambda a, b: update_function(a.im_func, b.im_func)),
284 ]
289 ]
285
290
291 if PY3:
292 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
293 lambda a, b: update_function(a.__func__, b.__func__)),
294 ])
295 else:
296 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType),
297 update_class),
298 (lambda a, b: isinstance2(a, b, types.MethodType),
299 lambda a, b: update_function(a.im_func, b.im_func)),
300 ])
301
302
286 def update_generic(a, b):
303 def update_generic(a, b):
287 for type_check, update in UPDATE_RULES:
304 for type_check, update in UPDATE_RULES:
288 if type_check(a, b):
305 if type_check(a, b):
@@ -317,7 +334,7 b' def superreload(module, reload=reload, old_objects={}):'
317 except TypeError:
334 except TypeError:
318 # weakref doesn't work for all types;
335 # weakref doesn't work for all types;
319 # create strong references for 'important' cases
336 # create strong references for 'important' cases
320 if isinstance(obj, types.ClassType):
337 if not PY3 and isinstance(obj, types.ClassType):
321 old_objects.setdefault(key, []).append(StrongRef(obj))
338 old_objects.setdefault(key, []).append(StrongRef(obj))
322
339
323 # reload module
340 # reload module
@@ -11,7 +11,6 b' import IPython.testing.tools as tt'
11
11
12 from IPython.extensions.autoreload import AutoreloadInterface
12 from IPython.extensions.autoreload import AutoreloadInterface
13 from IPython.core.hooks import TryNext
13 from IPython.core.hooks import TryNext
14 from IPython.testing.decorators import knownfailureif
15
14
16 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
17 # Test fixture
16 # Test fixture
@@ -294,12 +293,8 b' x = -99'
294 self.shell.run_code("pass") # trigger reload
293 self.shell.run_code("pass") # trigger reload
295 nt.assert_equal(mod.x, -99)
294 nt.assert_equal(mod.x, -99)
296
295
297 # The autoreload extension needs to be updated for Python 3.2, as .pyc files
298 # are stored in a different location. See gh-846.
299 @knownfailureif(sys.version_info >= (3,2))
300 def test_smoketest_aimport(self):
296 def test_smoketest_aimport(self):
301 self._check_smoketest(use_aimport=True)
297 self._check_smoketest(use_aimport=True)
302
298
303 @knownfailureif(sys.version_info >= (3,2))
304 def test_smoketest_autoreload(self):
299 def test_smoketest_autoreload(self):
305 self._check_smoketest(use_aimport=False)
300 self._check_smoketest(use_aimport=False)
General Comments 0
You need to be logged in to leave comments. Login now