##// END OF EJS Templates
Merge pull request #9825 from Carreau/no-overwrite-master...
Min RK -
r22854:4564091b merge
parent child Browse files
Show More
@@ -76,6 +76,9 b' class DisplayHook(Configurable):'
76 # particular uses _, so we need to stay away from it.
76 # particular uses _, so we need to stay away from it.
77 if '_' in builtin_mod.__dict__:
77 if '_' in builtin_mod.__dict__:
78 try:
78 try:
79 user_value = self.shell.user_ns['_']
80 if user_value is not self._:
81 return
79 del self.shell.user_ns['_']
82 del self.shell.user_ns['_']
80 except KeyError:
83 except KeyError:
81 pass
84 pass
@@ -195,13 +198,23 b' class DisplayHook(Configurable):'
195 if result is not self.shell.user_ns['_oh']:
198 if result is not self.shell.user_ns['_oh']:
196 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
199 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
197 self.cull_cache()
200 self.cull_cache()
198 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
199 # we cause buggy behavior for things like gettext).
200
201
201 if '_' not in builtin_mod.__dict__:
202 # Don't overwrite '_' and friends if '_' is in __builtin__
203 # (otherwise we cause buggy behavior for things like gettext). and
204 # do not overwrite _, __ or ___ if one of these has been assigned
205 # by the user.
206 update_unders = True
207 for unders in ['_'*i for i in range(1,4)]:
208 if not unders in self.shell.user_ns:
209 continue
210 if getattr(self, unders) is not self.shell.user_ns.get(unders):
211 update_unders = False
212
202 self.___ = self.__
213 self.___ = self.__
203 self.__ = self._
214 self.__ = self._
204 self._ = result
215 self._ = result
216
217 if ('_' not in builtin_mod.__dict__) and (update_unders):
205 self.shell.push({'_':self._,
218 self.shell.push({'_':self._,
206 '__':self.__,
219 '__':self.__,
207 '___':self.___}, interactive=False)
220 '___':self.___}, interactive=False)
@@ -209,7 +222,7 b' class DisplayHook(Configurable):'
209 # hackish access to top-level namespace to create _1,_2... dynamically
222 # hackish access to top-level namespace to create _1,_2... dynamically
210 to_main = {}
223 to_main = {}
211 if self.do_full_cache:
224 if self.do_full_cache:
212 new_result = '_'+repr(self.prompt_count)
225 new_result = '_%s' % self.prompt_count
213 to_main[new_result] = result
226 to_main[new_result] = result
214 self.shell.push(to_main, interactive=False)
227 self.shell.push(to_main, interactive=False)
215 self.shell.user_ns['_oh'][self.prompt_count] = result
228 self.shell.user_ns['_oh'][self.prompt_count] = result
@@ -26,3 +26,30 b' def test_output_quiet():'
26
26
27 with AssertNotPrints('2'):
27 with AssertNotPrints('2'):
28 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
28 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
29
30 def test_underscore_no_overrite_user():
31 ip.run_cell('_ = 42', store_history=True)
32 ip.run_cell('1+1', store_history=True)
33
34 with AssertPrints('42'):
35 ip.run_cell('print(_)', store_history=True)
36
37 ip.run_cell('del _', store_history=True)
38 ip.run_cell('6+6', store_history=True)
39 with AssertPrints('12'):
40 ip.run_cell('_', store_history=True)
41
42
43 def test_underscore_no_overrite_builtins():
44 ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
45 ip.run_cell('3+3', store_history=True)
46
47 with AssertPrints('gettext'):
48 ip.run_cell('print(_)', store_history=True)
49
50 ip.run_cell('_ = "userset"', store_history=True)
51
52 with AssertPrints('userset'):
53 ip.run_cell('print(_)', store_history=True)
54 ip.run_cell('import builtins; del builtins._')
55
General Comments 0
You need to be logged in to leave comments. Login now