##// END OF EJS Templates
- More fixes for doctest support....
fperez -
Show More
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 2754 2007-09-09 10:16:59Z fperez $"""
4 $Id: Magic.py 2763 2007-09-14 06:35:44Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -1038,6 +1038,10 b' Currently the magic system has the following functions:\\n"""'
1038 1038 for i in self.magic_who_ls():
1039 1039 del(user_ns[i])
1040 1040
1041 # Also flush the private list of module references kept for script
1042 # execution protection
1043 self.shell._user_main_modules[:] = []
1044
1041 1045 def magic_logstart(self,parameter_s=''):
1042 1046 """Start logging anywhere in a session.
1043 1047
@@ -1519,11 +1523,13 b' Currently the magic system has the following functions:\\n"""'
1519 1523 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1520 1524
1521 1525 if opts.has_key('i'):
1526 # Run in user's interactive namespace
1522 1527 prog_ns = self.shell.user_ns
1523 1528 __name__save = self.shell.user_ns['__name__']
1524 1529 prog_ns['__name__'] = '__main__'
1525 1530 main_mod = FakeModule(prog_ns)
1526 1531 else:
1532 # Run in a fresh, empty namespace
1527 1533 if opts.has_key('n'):
1528 1534 name = os.path.splitext(os.path.basename(filename))[0]
1529 1535 else:
@@ -1531,6 +1537,10 b' Currently the magic system has the following functions:\\n"""'
1531 1537 main_mod = FakeModule()
1532 1538 prog_ns = main_mod.__dict__
1533 1539 prog_ns['__name__'] = name
1540 # The shell MUST hold a reference to main_mod so after %run exits,
1541 # the python deletion mechanism doesn't zero it out (leaving
1542 # dangling references)
1543 self.shell._user_main_modules.append(main_mod)
1534 1544
1535 1545 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1536 1546 # set the __file__ global in the script's namespace
@@ -1594,6 +1604,7 b' Currently the magic system has the following functions:\\n"""'
1594 1604 if runner is None:
1595 1605 runner = self.shell.safe_execfile
1596 1606 if opts.has_key('t'):
1607 # timed execution
1597 1608 try:
1598 1609 nruns = int(opts['N'][0])
1599 1610 if nruns < 1:
@@ -1627,6 +1638,7 b' Currently the magic system has the following functions:\\n"""'
1627 1638 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1628 1639
1629 1640 else:
1641 # regular execution
1630 1642 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1631 1643 if opts.has_key('i'):
1632 1644 self.shell.user_ns['__name__'] = __name__save
@@ -5,7 +5,7 b' General purpose utilities.'
5 5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 6 these things are also convenient when working at the command line.
7 7
8 $Id: genutils.py 2727 2007-09-07 15:32:08Z vivainio $"""
8 $Id: genutils.py 2763 2007-09-14 06:35:44Z fperez $"""
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -22,6 +22,7 b' __license__ = Release.license'
22 22 # required modules from the Python standard library
23 23 import __main__
24 24 import commands
25 import doctest
25 26 import os
26 27 import re
27 28 import shlex
@@ -836,6 +837,36 b' def dhook_wrap(func,*a,**k):'
836 837 return f
837 838
838 839 #----------------------------------------------------------------------------
840 def doctest_reload():
841 """Properly reload doctest to reuse it interactively.
842
843 This routine:
844
845 - reloads doctest
846
847 - resets its global 'master' attribute to None, so that multiple uses of
848 the module interactively don't produce cumulative reports.
849
850 - Monkeypatches its core test runner method to protect it from IPython's
851 modified displayhook. Doctest expects the default displayhook behavior
852 deep down, so our modification breaks it completely. For this reason, a
853 hard monkeypatch seems like a reasonable solution rather than asking
854 users to manually use a different doctest runner when under IPython."""
855
856 import doctest
857 reload(doctest)
858 doctest.master=None
859
860 try:
861 doctest.DocTestRunner
862 except AttributeError:
863 # This is only for python 2.3 compatibility, remove once we move to
864 # 2.4 only.
865 pass
866 else:
867 doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run)
868
869 #----------------------------------------------------------------------------
839 870 class HomeDirError(Error):
840 871 pass
841 872
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 2754 2007-09-09 10:16:59Z fperez $
9 $Id: iplib.py 2763 2007-09-14 06:35:44Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -41,7 +41,6 b' import StringIO'
41 41 import bdb
42 42 import cPickle as pickle
43 43 import codeop
44 import doctest
45 44 import exceptions
46 45 import glob
47 46 import inspect
@@ -344,6 +343,20 b' class InteractiveShell(object,Magic):'
344 343 #print 'main_name:',main_name # dbg
345 344 sys.modules[main_name] = FakeModule(self.user_ns)
346 345
346 # Now that FakeModule produces a real module, we've run into a nasty
347 # problem: after script execution (via %run), the module where the user
348 # code ran is deleted. Now that this object is a true module (needed
349 # so docetst and other tools work correctly), the Python module
350 # teardown mechanism runs over it, and sets to None every variable
351 # present in that module. This means that later calls to functions
352 # defined in the script (which have become interactively visible after
353 # script exit) fail, because they hold references to objects that have
354 # become overwritten into None. The only solution I see right now is
355 # to protect every FakeModule used by %run by holding an internal
356 # reference to it. This private list will be used for that. The
357 # %reset command will flush it as well.
358 self._user_main_modules = []
359
347 360 # List of input with multi-line handling.
348 361 # Fill its zero entry, user counter starts at 1
349 362 self.input_hist = InputList(['\n'])
@@ -681,20 +694,9 b' class InteractiveShell(object,Magic):'
681 694 self.sys_displayhook = sys.displayhook
682 695 sys.displayhook = self.outputcache
683 696
684 # Monkeypatch doctest so that its core test runner method is protected
685 # from IPython's modified displayhook. Doctest expects the default
686 # displayhook behavior deep down, so our modification breaks it
687 # completely. For this reason, a hard monkeypatch seems like a
688 # reasonable solution rather than asking users to manually use a
689 # different doctest runner when under IPython.
690 try:
691 doctest.DocTestRunner
692 except AttributeError:
693 # This is only for python 2.3 compatibility, remove once we move to
694 # 2.4 only.
695 pass
696 else:
697 doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run)
697 # Do a proper resetting of doctest, including the necessary displayhook
698 # monkeypatching
699 doctest_reload()
698 700
699 701 # Set user colors (don't do it in the constructor above so that it
700 702 # doesn't crash if colors option is invalid)
@@ -1,3 +1,15 b''
1 2007-09-14 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/genutils.py (doctest_reload): expose the doctest
4 reloader to the user so that people can easily reset doctest while
5 using it interactively. Fixes a problem reported by Jorgen.
6
7 * IPython/iplib.py (InteractiveShell.__init__): protect the
8 FakeModule instances used for __main__ in %run calls from
9 deletion, so that user code defined in them isn't left with
10 dangling references due to the Python module deletion machinery.
11 This should fix the problems reported by Darren.
12
1 13 2007-09-10 Darren Dale <dd55@cornell.edu>
2 14
3 15 * Cleanup of IPShellQt and IPShellQt4
General Comments 0
You need to be logged in to leave comments. Login now