##// END OF EJS Templates
Limit special-casing of _ variable to doctests....
Fernando Perez -
Show More
@@ -68,11 +68,19 b' class ipnsdict(dict):'
68 This subclass adds a simple checkpointing capability so that when testing
68 This subclass adds a simple checkpointing capability so that when testing
69 machinery clears it (we use it as the test execution context), it doesn't
69 machinery clears it (we use it as the test execution context), it doesn't
70 get completely destroyed.
70 get completely destroyed.
71
72 In addition, it can handle the presence of the '_' key in a special manner,
73 which is needed because of how Python's doctest machinery operates with
74 '_'. See constructor and :meth:`update` for details.
71 """
75 """
72
76
73 def __init__(self,*a):
77 def __init__(self,*a):
74 dict.__init__(self,*a)
78 dict.__init__(self,*a)
75 self._savedict = {}
79 self._savedict = {}
80 # If this flag is True, the .update() method will unconditionally
81 # remove a key named '_'. This is so that such a dict can be used as a
82 # namespace in doctests that call '_'.
83 self.protect_underscore = False
76
84
77 def clear(self):
85 def clear(self):
78 dict.clear(self)
86 dict.clear(self)
@@ -86,12 +94,15 b' class ipnsdict(dict):'
86 self._checkpoint()
94 self._checkpoint()
87 dict.update(self,other)
95 dict.update(self,other)
88
96
89 # If '_' is in the namespace, python won't set it when executing code
97 if self.protect_underscore:
90 # *in doctests*, and we have multiple doctests that use '_'. So we
98 # If '_' is in the namespace, python won't set it when executing
91 # ensure that the namespace is always 'clean' of it before it's used
99 # code *in doctests*, and we have multiple doctests that use '_'.
92 # for test code execution. Note: this means that outside of doctests,
100 # So we ensure that the namespace is always 'clean' of it before
93 # our own testing
101 # it's used for test code execution.
94 self.pop('_',None)
102 # This flag is only turned on by the doctest machinery, so that
103 # normal test code can assume the _ key is updated like any other
104 # key and can test for its presence after cell executions.
105 self.pop('_', None)
95
106
96 # The builtins namespace must *always* be the real __builtin__ module,
107 # The builtins namespace must *always* be the real __builtin__ module,
97 # else weird stuff happens. The main ipython code does have provisions
108 # else weird stuff happens. The main ipython code does have provisions
@@ -273,6 +273,10 b' class DocTestCase(doctests.DocTestCase):'
273 # fills with the necessary info from the module being tested).
273 # fills with the necessary info from the module being tested).
274 _ip.user_ns.update(self._dt_test.globs)
274 _ip.user_ns.update(self._dt_test.globs)
275 self._dt_test.globs = _ip.user_ns
275 self._dt_test.globs = _ip.user_ns
276 # IPython must protect the _ key in the namespace (it can't exist)
277 # so that Python's doctest code sets it naturally, so we enable
278 # this feature of our testing namespace.
279 _ip.user_ns.protect_underscore = True
276
280
277 super(DocTestCase, self).setUp()
281 super(DocTestCase, self).setUp()
278
282
@@ -282,6 +286,9 b' class DocTestCase(doctests.DocTestCase):'
282 # teardown doesn't destroy the ipython namespace
286 # teardown doesn't destroy the ipython namespace
283 if isinstance(self._dt_test.examples[0],IPExample):
287 if isinstance(self._dt_test.examples[0],IPExample):
284 self._dt_test.globs = self._dt_test_globs_ori
288 self._dt_test.globs = self._dt_test_globs_ori
289 # Restore the behavior of the '_' key in the user namespace to
290 # normal after each doctest, so that unittests behave normally
291 _ip.user_ns.protect_underscore = False
285
292
286 # XXX - fperez: I am not sure if this is truly a bug in nose 0.11, but
293 # XXX - fperez: I am not sure if this is truly a bug in nose 0.11, but
287 # it does look like one to me: its tearDown method tries to run
294 # it does look like one to me: its tearDown method tries to run
General Comments 0
You need to be logged in to leave comments. Login now