@@ -94,6 +94,28 @@ def _run_ns_sync(self,arg_s,runner=None):
9494 return out
9595
9696
97+ # XXX1 - namespace handling
98+ class ncdict (dict ):
99+ def __init__ (self ,* a ):
100+ dict .__init__ (self ,* a )
101+ self ._savedict = {}
102+
103+ def copy (self ):
104+ return self
105+
106+ def clear (self ):
107+ import IPython
108+
109+ print 'NCDICT - clear' # dbg
110+ dict .clear (self )
111+ self .update (IPython .ipapi .make_user_ns ())
112+ self .update (self ._savedict )
113+
114+ def remember (self ,adict ):
115+ self ._savedict = adict
116+
117+ #class ncdict(dict): pass
118+
97119def start_ipython ():
98120 """Start a global IPython shell, which we need for IPython-specific syntax.
99121 """
@@ -117,7 +139,10 @@ def xsys(cmd):
117139 _main = sys .modules .get ('__main__' )
118140
119141 # Start IPython instance. We customize it to start with minimal frills.
120- IPython .Shell .IPShell (['--classic' ,'--noterm_title' ])
142+ user_ns = IPython .ipapi .make_user_ns (ncdict ())
143+
144+ IPython .Shell .IPShell (['--classic' ,'--noterm_title' ],
145+ user_ns )
121146
122147 # Deactivate the various python system hooks added by ipython for
123148 # interactive convenience so we don't confuse the doctest system
@@ -250,6 +275,85 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen):
250275 globs , seen )
251276
252277
278+ # XXX1 - namespace handling
279+ def Xfind (self , obj , name = None , module = None , globs = None , extraglobs = None ):
280+ """
281+ Return a list of the DocTests that are defined by the given
282+ object's docstring, or by any of its contained objects'
283+ docstrings.
284+
285+ The optional parameter `module` is the module that contains
286+ the given object. If the module is not specified or is None, then
287+ the test finder will attempt to automatically determine the
288+ correct module. The object's module is used:
289+
290+ - As a default namespace, if `globs` is not specified.
291+ - To prevent the DocTestFinder from extracting DocTests
292+ from objects that are imported from other modules.
293+ - To find the name of the file containing the object.
294+ - To help find the line number of the object within its
295+ file.
296+
297+ Contained objects whose module does not match `module` are ignored.
298+
299+ If `module` is False, no attempt to find the module will be made.
300+ This is obscure, of use mostly in tests: if `module` is False, or
301+ is None but cannot be found automatically, then all objects are
302+ considered to belong to the (non-existent) module, so all contained
303+ objects will (recursively) be searched for doctests.
304+
305+ The globals for each DocTest is formed by combining `globs`
306+ and `extraglobs` (bindings in `extraglobs` override bindings
307+ in `globs`). A new copy of the globals dictionary is created
308+ for each DocTest. If `globs` is not specified, then it
309+ defaults to the module's `__dict__`, if specified, or {}
310+ otherwise. If `extraglobs` is not specified, then it defaults
311+ to {}.
312+
313+ """
314+
315+ # Find the module that contains the given object (if obj is
316+ # a module, then module=obj.). Note: this may fail, in which
317+ # case module will be None.
318+ if module is False :
319+ module = None
320+ elif module is None :
321+ module = inspect .getmodule (obj )
322+
323+ # always build our own globals
324+ if globs is None :
325+ if module is None :
326+ globs = {}
327+ else :
328+ globs = module .__dict__ .copy ()
329+ else :
330+ globs .update (module .__dict__ .copy ())
331+
332+ print 'globs is:' ,globs .keys ()
333+
334+ if extraglobs is not None :
335+ globs .update (extraglobs )
336+
337+ try :
338+ globs .remember (module .__dict__ )
339+ except :
340+ pass
341+
342+ ## # Initialize globals, and merge in extraglobs.
343+ ## if globs is None:
344+ ## if module is None:
345+ ## globs = {}
346+ ## else:
347+ ## globs = module.__dict__.copy()
348+ ## else:
349+ ## globs = globs.copy()
350+ ## if extraglobs is not None:
351+ ## globs.update(extraglobs)
352+
353+ return doctest .DocTestFinder .find (self ,obj ,name ,module ,globs ,
354+ extraglobs )
355+
356+
253357class IPDoctestOutputChecker (doctest .OutputChecker ):
254358 """Second-chance checker with support for random tests.
255359
@@ -342,6 +446,12 @@ def runTest(self):
342446 if failures :
343447 raise self .failureException (self .format_failure (new .getvalue ()))
344448
449+ # XXX1 - namespace handling
450+ def XtearDown (self ):
451+ print '!! teardown!' # dbg
452+ doctests .DocTestCase .tearDown (self )
453+
454+
345455
346456# A simple subclassing of the original with a different class name, so we can
347457# distinguish and treat differently IPython examples from pure python ones.
@@ -749,5 +859,9 @@ def configure(self, options, config):
749859 self .extension = tolist (options .doctestExtension )
750860 self .parser = IPDocTestParser ()
751861 self .finder = DocTestFinder (parser = self .parser )
862+
863+ # XXX1 - namespace handling
752864 self .globs = None
865+ #self.globs = _ip.IP.user_ns
866+
753867 self .extraglobs = None
0 commit comments