108
108
except NameError :
109
109
from imp import reload
110
110
111
+ from IPython .utils import pyfile
112
+ from IPython .utils .py3compat import PY3
113
+
111
114
def _get_compiled_ext ():
112
115
"""Official way to get the extension of compiled files (.pyc or .pyo)"""
113
116
for ext , mode , typ in imp .get_suffixes ():
@@ -198,14 +201,14 @@ def check(self, check_all=False):
198
201
199
202
if ext .lower () == '.py' :
200
203
ext = PY_COMPILED_EXT
201
- pyc_filename = path + PY_COMPILED_EXT
204
+ pyc_filename = pyfile . cache_from_source ( filename )
202
205
py_filename = filename
203
206
else :
204
207
pyc_filename = filename
205
- py_filename = filename [: - 1 ]
206
-
207
- if ext != PY_COMPILED_EXT :
208
- continue
208
+ try :
209
+ py_filename = pyfile . source_from_cache ( filename )
210
+ except ValueError :
211
+ continue
209
212
210
213
try :
211
214
pymtime = os .stat (py_filename ).st_mtime
@@ -229,10 +232,16 @@ def check(self, check_all=False):
229
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
242
def update_function (old , new ):
233
243
"""Upgrade the code object of a function"""
234
- for name in ['func_code' , 'func_defaults' , 'func_doc' ,
235
- 'func_closure' , 'func_globals' , 'func_dict' ]:
244
+ for name in func_attrs :
236
245
try :
237
246
setattr (old , name , getattr (new , name ))
238
247
except (AttributeError , TypeError ):
@@ -271,18 +280,26 @@ def isinstance2(a, b, typ):
271
280
return isinstance (a , typ ) and isinstance (b , typ )
272
281
273
282
UPDATE_RULES = [
274
- (lambda a , b : isinstance2 (a , b , types .ClassType ),
275
- update_class ),
276
- (lambda a , b : isinstance2 (a , b , types .TypeType ),
283
+ (lambda a , b : isinstance2 (a , b , type ),
277
284
update_class ),
278
285
(lambda a , b : isinstance2 (a , b , types .FunctionType ),
279
286
update_function ),
280
287
(lambda a , b : isinstance2 (a , b , property ),
281
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
303
def update_generic (a , b ):
287
304
for type_check , update in UPDATE_RULES :
288
305
if type_check (a , b ):
@@ -317,7 +334,7 @@ def superreload(module, reload=reload, old_objects={}):
317
334
except TypeError :
318
335
# weakref doesn't work for all types;
319
336
# create strong references for 'important' cases
320
- if isinstance (obj , types .ClassType ):
337
+ if not PY3 and isinstance (obj , types .ClassType ):
321
338
old_objects .setdefault (key , []).append (StrongRef (obj ))
322
339
323
340
# reload module
0 commit comments