Show More
@@ -35,20 +35,20 b' from IPython.core.events import EventManager, pre_run_cell' | |||||
35 |
|
35 | |||
36 | noop = lambda *a, **kw: None |
|
36 | noop = lambda *a, **kw: None | |
37 |
|
37 | |||
38 | class FakeShell: |
|
|||
39 |
|
38 | |||
|
39 | class FakeShell: | |||
40 | def __init__(self): |
|
40 | def __init__(self): | |
41 | self.ns = {} |
|
41 | self.ns = {} | |
42 | self.user_ns = self.ns |
|
42 | self.user_ns = self.ns | |
43 | self.user_ns_hidden = {} |
|
43 | self.user_ns_hidden = {} | |
44 |
self.events = EventManager(self, { |
|
44 | self.events = EventManager(self, {"pre_run_cell", pre_run_cell}) | |
45 | self.auto_magics = AutoreloadMagics(shell=self) |
|
45 | self.auto_magics = AutoreloadMagics(shell=self) | |
46 |
self.events.register( |
|
46 | self.events.register("pre_run_cell", self.auto_magics.pre_run_cell) | |
47 |
|
47 | |||
48 | register_magics = set_hook = noop |
|
48 | register_magics = set_hook = noop | |
49 |
|
49 | |||
50 | def run_code(self, code): |
|
50 | def run_code(self, code): | |
51 |
self.events.trigger( |
|
51 | self.events.trigger("pre_run_cell") | |
52 | exec(code, self.user_ns) |
|
52 | exec(code, self.user_ns) | |
53 | self.auto_magics.post_execute_hook() |
|
53 | self.auto_magics.post_execute_hook() | |
54 |
|
54 | |||
@@ -111,20 +111,22 b' class Fixture(TestCase):' | |||||
111 | time.sleep(1.05) |
|
111 | time.sleep(1.05) | |
112 |
|
112 | |||
113 | # Write |
|
113 | # Write | |
114 |
with open(filename, |
|
114 | with open(filename, "w") as f: | |
115 | f.write(content) |
|
115 | f.write(content) | |
116 |
|
116 | |||
117 | def new_module(self, code): |
|
117 | def new_module(self, code): | |
118 | code = textwrap.dedent(code) |
|
118 | code = textwrap.dedent(code) | |
119 | mod_name, mod_fn = self.get_module() |
|
119 | mod_name, mod_fn = self.get_module() | |
120 |
with open(mod_fn, |
|
120 | with open(mod_fn, "w") as f: | |
121 | f.write(code) |
|
121 | f.write(code) | |
122 | return mod_name, mod_fn |
|
122 | return mod_name, mod_fn | |
123 |
|
123 | |||
|
124 | ||||
124 | #----------------------------------------------------------------------------- |
|
125 | # ----------------------------------------------------------------------------- | |
125 | # Test automatic reloading |
|
126 | # Test automatic reloading | |
126 | #----------------------------------------------------------------------------- |
|
127 | # ----------------------------------------------------------------------------- | |
127 |
|
128 | |||
|
129 | ||||
128 | def pickle_get_current_class(obj): |
|
130 | def pickle_get_current_class(obj): | |
129 | """ |
|
131 | """ | |
130 | Original issue comes from pickle; hence the name. |
|
132 | Original issue comes from pickle; hence the name. | |
@@ -136,25 +138,36 b' def pickle_get_current_class(obj):' | |||||
136 | obj2 = getattr(obj2, subpath) |
|
138 | obj2 = getattr(obj2, subpath) | |
137 | return obj2 |
|
139 | return obj2 | |
138 |
|
140 | |||
139 | class TestAutoreload(Fixture): |
|
|||
140 |
|
141 | |||
|
142 | class TestAutoreload(Fixture): | |||
141 | def test_reload_enums(self): |
|
143 | def test_reload_enums(self): | |
142 |
mod_name, mod_fn = self.new_module( |
|
144 | mod_name, mod_fn = self.new_module( | |
|
145 | textwrap.dedent( | |||
|
146 | """ | |||
143 |
|
|
147 | from enum import Enum | |
144 |
|
|
148 | class MyEnum(Enum): | |
145 | A = 'A' |
|
149 | A = 'A' | |
146 | B = 'B' |
|
150 | B = 'B' | |
147 |
|
|
151 | """ | |
|
152 | ) | |||
|
153 | ) | |||
148 | self.shell.magic_autoreload("2") |
|
154 | self.shell.magic_autoreload("2") | |
149 | self.shell.magic_aimport(mod_name) |
|
155 | self.shell.magic_aimport(mod_name) | |
150 |
self.write_file( |
|
156 | self.write_file( | |
|
157 | mod_fn, | |||
|
158 | textwrap.dedent( | |||
|
159 | """ | |||
151 |
|
|
160 | from enum import Enum | |
152 |
|
|
161 | class MyEnum(Enum): | |
153 | A = 'A' |
|
162 | A = 'A' | |
154 | B = 'B' |
|
163 | B = 'B' | |
155 | C = 'C' |
|
164 | C = 'C' | |
156 |
|
|
165 | """ | |
157 | with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'): |
|
166 | ), | |
|
167 | ) | |||
|
168 | with tt.AssertNotPrints( | |||
|
169 | ("[autoreload of %s failed:" % mod_name), channel="stderr" | |||
|
170 | ): | |||
158 | self.shell.run_code("pass") # trigger another reload |
|
171 | self.shell.run_code("pass") # trigger another reload | |
159 |
|
172 | |||
160 | def test_reload_class_type(self): |
|
173 | def test_reload_class_type(self): | |
@@ -195,7 +208,9 b' class TestAutoreload(Fixture):' | |||||
195 |
|
208 | |||
196 | def test_reload_class_attributes(self): |
|
209 | def test_reload_class_attributes(self): | |
197 | self.shell.magic_autoreload("2") |
|
210 | self.shell.magic_autoreload("2") | |
198 |
mod_name, mod_fn = self.new_module( |
|
211 | mod_name, mod_fn = self.new_module( | |
|
212 | textwrap.dedent( | |||
|
213 | """ | |||
199 |
|
|
214 | class MyClass: | |
200 |
|
|
215 | ||
201 |
|
|
216 | def __init__(self, a=10): | |
@@ -241,7 +256,7 b' class TestAutoreload(Fixture):' | |||||
241 |
|
256 | |||
242 | self.shell.run_code("second = MyClass(5)") |
|
257 | self.shell.run_code("second = MyClass(5)") | |
243 |
|
258 | |||
244 |
for object_name in { |
|
259 | for object_name in {"first", "second"}: | |
245 | self.shell.run_code(f"{object_name}.power(5)") |
|
260 | self.shell.run_code(f"{object_name}.power(5)") | |
246 | with nt.assert_raises(AttributeError): |
|
261 | with nt.assert_raises(AttributeError): | |
247 | self.shell.run_code(f"{object_name}.cube()") |
|
262 | self.shell.run_code(f"{object_name}.cube()") | |
@@ -261,11 +276,11 b' class TestAutoreload(Fixture):' | |||||
261 | self.shell.run_code(f"from {mod_name} import *") |
|
276 | self.shell.run_code(f"from {mod_name} import *") | |
262 | self.shell.run_code("func1()") |
|
277 | self.shell.run_code("func1()") | |
263 | with nt.assert_raises(NameError): |
|
278 | with nt.assert_raises(NameError): | |
264 |
self.shell.run_code( |
|
279 | self.shell.run_code("func2()") | |
265 | with nt.assert_raises(NameError): |
|
280 | with nt.assert_raises(NameError): | |
266 |
self.shell.run_code( |
|
281 | self.shell.run_code("t = Test()") | |
267 | with nt.assert_raises(NameError): |
|
282 | with nt.assert_raises(NameError): | |
268 |
self.shell.run_code( |
|
283 | self.shell.run_code("number") | |
269 |
|
284 | |||
270 | # ----------- TEST NEW OBJ LOADED -------------------------- |
|
285 | # ----------- TEST NEW OBJ LOADED -------------------------- | |
271 |
|
286 | |||
@@ -287,7 +302,7 b' class TestAutoreload(Fixture):' | |||||
287 | # test class now exists |
|
302 | # test class now exists | |
288 | self.shell.run_code("t = Test()") |
|
303 | self.shell.run_code("t = Test()") | |
289 | # test global built-in var now exists |
|
304 | # test global built-in var now exists | |
290 |
self.shell.run_code( |
|
305 | self.shell.run_code("number") | |
291 | # test the enumerations gets loaded succesfully |
|
306 | # test the enumerations gets loaded succesfully | |
292 | self.shell.run_code("TestEnum.A") |
|
307 | self.shell.run_code("TestEnum.A") | |
293 |
|
308 | |||
@@ -314,7 +329,7 b' class TestAutoreload(Fixture):' | |||||
314 |
|
329 | |||
315 | # ----------- TEST IMPORT FROM MODULE -------------------------- |
|
330 | # ----------- TEST IMPORT FROM MODULE -------------------------- | |
316 |
|
331 | |||
317 |
new_mod_code = |
|
332 | new_mod_code = """ | |
318 | from enum import Enum |
|
333 | from enum import Enum | |
319 | class Ext(Enum): |
|
334 | class Ext(Enum): | |
320 | A = 'ext' |
|
335 | A = 'ext' | |
@@ -324,11 +339,11 b' class TestAutoreload(Fixture):' | |||||
324 | def meth(self): |
|
339 | def meth(self): | |
325 | return 'ext' |
|
340 | return 'ext' | |
326 | ext_int = 2 |
|
341 | ext_int = 2 | |
327 |
|
|
342 | """ | |
328 | new_mod_name, new_mod_fn = self.new_module(textwrap.dedent(new_mod_code)) |
|
343 | new_mod_name, new_mod_fn = self.new_module(textwrap.dedent(new_mod_code)) | |
329 |
current_mod_code = f |
|
344 | current_mod_code = f""" | |
330 | from {new_mod_name} import * |
|
345 | from {new_mod_name} import * | |
331 |
|
|
346 | """ | |
332 | self.write_file(mod_fn, textwrap.dedent(current_mod_code)) |
|
347 | self.write_file(mod_fn, textwrap.dedent(current_mod_code)) | |
333 | self.shell.run_code("assert Ext.A.value == 'ext'") |
|
348 | self.shell.run_code("assert Ext.A.value == 'ext'") | |
334 | self.shell.run_code("assert ext_func() == 'ext'") |
|
349 | self.shell.run_code("assert ext_func() == 'ext'") | |
@@ -341,7 +356,8 b' class TestAutoreload(Fixture):' | |||||
341 | '%autoreload 1' or '%autoreload 2' |
|
356 | '%autoreload 1' or '%autoreload 2' | |
342 | """ |
|
357 | """ | |
343 |
|
358 | |||
344 |
mod_name, mod_fn = self.new_module( |
|
359 | mod_name, mod_fn = self.new_module( | |
|
360 | """ | |||
345 |
|
|
361 | x = 9 | |
346 |
|
|
362 | ||
347 |
|
|
363 | z = 123 # this item will be deleted | |
@@ -364,7 +380,8 b' class Baz(object):' | |||||
364 | class Bar: # old-style class: weakref doesn't work for it on Python < 2.7 |
|
380 | class Bar: # old-style class: weakref doesn't work for it on Python < 2.7 | |
365 |
|
|
381 | def foo(self): | |
366 |
|
|
382 | return 1 | |
367 |
|
|
383 | """ | |
|
384 | ) | |||
368 |
|
385 | |||
369 | # |
|
386 | # | |
370 | # Import module, and mark for reloading |
|
387 | # Import module, and mark for reloading | |
@@ -383,8 +400,9 b" class Bar: # old-style class: weakref doesn't work for it on Python < 2.7" | |||||
383 | self.shell.run_code("import %s" % mod_name) |
|
400 | self.shell.run_code("import %s" % mod_name) | |
384 | stream = StringIO() |
|
401 | stream = StringIO() | |
385 | self.shell.magic_aimport("", stream=stream) |
|
402 | self.shell.magic_aimport("", stream=stream) | |
386 | nt.assert_true("Modules to reload:\nall-except-skipped" in |
|
403 | nt.assert_true( | |
387 |
|
|
404 | "Modules to reload:\nall-except-skipped" in stream.getvalue() | |
|
405 | ) | |||
388 | nt.assert_in(mod_name, self.shell.ns) |
|
406 | nt.assert_in(mod_name, self.shell.ns) | |
389 |
|
407 | |||
390 | mod = sys.modules[mod_name] |
|
408 | mod = sys.modules[mod_name] | |
@@ -419,20 +437,29 b" class Bar: # old-style class: weakref doesn't work for it on Python < 2.7" | |||||
419 | # Simulate a failed reload: no reload should occur and exactly |
|
437 | # Simulate a failed reload: no reload should occur and exactly | |
420 | # one error message should be printed |
|
438 | # one error message should be printed | |
421 | # |
|
439 | # | |
422 |
self.write_file( |
|
440 | self.write_file( | |
|
441 | mod_fn, | |||
|
442 | """ | |||
423 |
|
|
443 | a syntax error | |
424 |
|
|
444 | """, | |
|
445 | ) | |||
425 |
|
446 | |||
426 | with tt.AssertPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'): |
|
447 | with tt.AssertPrints( | |
|
448 | ("[autoreload of %s failed:" % mod_name), channel="stderr" | |||
|
449 | ): | |||
427 | self.shell.run_code("pass") # trigger reload |
|
450 | self.shell.run_code("pass") # trigger reload | |
428 | with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'): |
|
451 | with tt.AssertNotPrints( | |
|
452 | ("[autoreload of %s failed:" % mod_name), channel="stderr" | |||
|
453 | ): | |||
429 | self.shell.run_code("pass") # trigger another reload |
|
454 | self.shell.run_code("pass") # trigger another reload | |
430 | check_module_contents() |
|
455 | check_module_contents() | |
431 |
|
456 | |||
432 | # |
|
457 | # | |
433 | # Rewrite module (this time reload should succeed) |
|
458 | # Rewrite module (this time reload should succeed) | |
434 | # |
|
459 | # | |
435 |
self.write_file( |
|
460 | self.write_file( | |
|
461 | mod_fn, | |||
|
462 | """ | |||
436 |
|
|
463 | x = 10 | |
437 |
|
|
464 | ||
438 |
|
|
465 | def foo(y): | |
@@ -450,11 +477,12 b' class Baz(object):' | |||||
450 |
|
|
477 | class Bar: # old-style class | |
451 |
|
|
478 | def foo(self): | |
452 |
|
|
479 | return 2 | |
453 |
|
|
480 | """, | |
|
481 | ) | |||
454 |
|
482 | |||
455 | def check_module_contents(): |
|
483 | def check_module_contents(): | |
456 | nt.assert_equal(mod.x, 10) |
|
484 | nt.assert_equal(mod.x, 10) | |
457 |
nt.assert_false(hasattr(mod, |
|
485 | nt.assert_false(hasattr(mod, "z")) | |
458 |
|
486 | |||
459 | nt.assert_equal(old_foo(0), 4) # superreload magic! |
|
487 | nt.assert_equal(old_foo(0), 4) # superreload magic! | |
460 | nt.assert_equal(mod.foo(0), 4) |
|
488 | nt.assert_equal(mod.foo(0), 4) | |
@@ -466,8 +494,8 b' class Bar: # old-style class' | |||||
466 | nt.assert_equal(old_obj.quux, 43) |
|
494 | nt.assert_equal(old_obj.quux, 43) | |
467 | nt.assert_equal(obj.quux, 43) |
|
495 | nt.assert_equal(obj.quux, 43) | |
468 |
|
496 | |||
469 |
nt.assert_false(hasattr(old_obj, |
|
497 | nt.assert_false(hasattr(old_obj, "zzz")) | |
470 |
nt.assert_false(hasattr(obj, |
|
498 | nt.assert_false(hasattr(obj, "zzz")) | |
471 |
|
499 | |||
472 | obj2 = mod.Bar() |
|
500 | obj2 = mod.Bar() | |
473 | nt.assert_equal(old_obj2.foo(), 2) |
|
501 | nt.assert_equal(old_obj2.foo(), 2) | |
@@ -491,17 +519,19 b' class Bar: # old-style class' | |||||
491 | self.shell.magic_aimport("-" + mod_name) |
|
519 | self.shell.magic_aimport("-" + mod_name) | |
492 | stream = StringIO() |
|
520 | stream = StringIO() | |
493 | self.shell.magic_aimport("", stream=stream) |
|
521 | self.shell.magic_aimport("", stream=stream) | |
494 | nt.assert_true(("Modules to skip:\n%s" % mod_name) in |
|
522 | nt.assert_true(("Modules to skip:\n%s" % mod_name) in stream.getvalue()) | |
495 | stream.getvalue()) |
|
|||
496 |
|
523 | |||
497 | # This should succeed, although no such module exists |
|
524 | # This should succeed, although no such module exists | |
498 | self.shell.magic_aimport("-tmpmod_as318989e89ds") |
|
525 | self.shell.magic_aimport("-tmpmod_as318989e89ds") | |
499 | else: |
|
526 | else: | |
500 | self.shell.magic_autoreload("0") |
|
527 | self.shell.magic_autoreload("0") | |
501 |
|
528 | |||
502 |
self.write_file( |
|
529 | self.write_file( | |
|
530 | mod_fn, | |||
|
531 | """ | |||
503 |
|
|
532 | x = -99 | |
504 |
|
|
533 | """, | |
|
534 | ) | |||
505 |
|
535 | |||
506 | self.shell.run_code("pass") # trigger reload |
|
536 | self.shell.run_code("pass") # trigger reload | |
507 | self.shell.run_code("pass") |
|
537 | self.shell.run_code("pass") | |
@@ -523,8 +553,3 b' x = -99' | |||||
523 |
|
553 | |||
524 | def test_smoketest_autoreload(self): |
|
554 | def test_smoketest_autoreload(self): | |
525 | self._check_smoketest(use_aimport=False) |
|
555 | self._check_smoketest(use_aimport=False) | |
526 |
|
||||
527 |
|
||||
528 |
|
||||
529 |
|
||||
530 |
|
General Comments 0
You need to be logged in to leave comments.
Login now