##// END OF EJS Templates
Add tests that the class attribute is properly updated
Matthias Bussonnier -
Show More
@@ -39,7 +39,7 b' class FakeShell:'
39
39
40 def __init__(self):
40 def __init__(self):
41 self.ns = {}
41 self.ns = {}
42 self.user_ns = {}
42 self.user_ns = self.ns
43 self.user_ns_hidden = {}
43 self.user_ns_hidden = {}
44 self.events = EventManager(self, {'pre_run_cell', pre_run_cell})
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)
@@ -49,7 +49,7 b' class FakeShell:'
49
49
50 def run_code(self, code):
50 def run_code(self, code):
51 self.events.trigger('pre_run_cell')
51 self.events.trigger('pre_run_cell')
52 exec(code, self.ns)
52 exec(code, self.user_ns)
53 self.auto_magics.post_execute_hook()
53 self.auto_magics.post_execute_hook()
54
54
55 def push(self, items):
55 def push(self, items):
@@ -106,7 +106,7 b' class Fixture(object):'
106 (because that is stored in the file). The only reliable way
106 (because that is stored in the file). The only reliable way
107 to achieve this seems to be to sleep.
107 to achieve this seems to be to sleep.
108 """
108 """
109
109 content = textwrap.dedent(content)
110 # Sleep one second + eps
110 # Sleep one second + eps
111 time.sleep(1.05)
111 time.sleep(1.05)
112
112
@@ -115,6 +115,7 b' class Fixture(object):'
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 mod_name, mod_fn = self.get_module()
119 mod_name, mod_fn = self.get_module()
119 with open(mod_fn, 'w') as f:
120 with open(mod_fn, 'w') as f:
120 f.write(code)
121 f.write(code)
@@ -124,6 +125,17 b' class Fixture(object):'
124 # Test automatic reloading
125 # Test automatic reloading
125 #-----------------------------------------------------------------------------
126 #-----------------------------------------------------------------------------
126
127
128 def pickle_get_current_class(obj):
129 """
130 Original issue comes from pickle; hence the name.
131 """
132 name = obj.__class__.__name__
133 module_name = getattr(obj, "__module__", None)
134 obj2 = sys.modules[module_name]
135 for subpath in name.split("."):
136 obj2 = getattr(obj2, subpath)
137 return obj2
138
127 class TestAutoreload(Fixture):
139 class TestAutoreload(Fixture):
128
140
129 @skipif(sys.version_info < (3, 6))
141 @skipif(sys.version_info < (3, 6))
@@ -147,6 +159,42 b' class TestAutoreload(Fixture):'
147 with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
159 with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
148 self.shell.run_code("pass") # trigger another reload
160 self.shell.run_code("pass") # trigger another reload
149
161
162 def test_reload_class_type(self):
163 self.shell.magic_autoreload("2")
164 mod_name, mod_fn = self.new_module(
165 """
166 class Test():
167 def meth(self):
168 return "old"
169 """
170 )
171 assert "test" not in self.shell.ns
172 assert "result" not in self.shell.ns
173
174 self.shell.run_code("from %s import Test" % mod_name)
175 self.shell.run_code("test = Test()")
176
177 self.write_file(
178 mod_fn,
179 """
180 class Test():
181 def meth(self):
182 return "new"
183 """,
184 )
185
186 test_object = self.shell.ns["test"]
187
188 # important to trigger autoreload logic !
189 self.shell.run_code("pass")
190
191 test_class = pickle_get_current_class(test_object)
192 assert isinstance(test_object, test_class)
193
194 # extra check.
195 self.shell.run_code("import pickle")
196 self.shell.run_code("p = pickle.dumps(test)")
197
150 def test_reload_class_attributes(self):
198 def test_reload_class_attributes(self):
151 self.shell.magic_autoreload("2")
199 self.shell.magic_autoreload("2")
152 mod_name, mod_fn = self.new_module(textwrap.dedent("""
200 mod_name, mod_fn = self.new_module(textwrap.dedent("""
@@ -398,3 +446,4 b' x = -99'
398
446
399
447
400
448
449
General Comments 0
You need to be logged in to leave comments. Login now