##// END OF EJS Templates
Merge pull request #3622 from takluyver/drop-fakemodule...
Min RK -
r12569:2e41daba merge
parent child Browse files
Show More
@@ -0,0 +1,1 b''
1 * The module ``IPython.core.fakemodule`` has been removed.
@@ -47,7 +47,6 b' from IPython.core.displayhook import DisplayHook'
47 47 from IPython.core.displaypub import DisplayPublisher
48 48 from IPython.core.error import UsageError
49 49 from IPython.core.extensions import ExtensionManager
50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
51 50 from IPython.core.formatters import DisplayFormatter
52 51 from IPython.core.history import HistoryManager
53 52 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
@@ -826,15 +825,18 b' class InteractiveShell(SingletonConfigurable):'
826 825 # Things related to the "main" module
827 826 #-------------------------------------------------------------------------
828 827
829 def new_main_mod(self, filename):
828 def new_main_mod(self, filename, modname):
830 829 """Return a new 'main' module object for user code execution.
831 830
832 831 ``filename`` should be the path of the script which will be run in the
833 832 module. Requests with the same filename will get the same module, with
834 833 its namespace cleared.
835 834
835 ``modname`` should be the module name - normally either '__main__' or
836 the basename of the file without the extension.
837
836 838 When scripts are executed via %run, we must keep a reference to their
837 __main__ module (a FakeModule instance) around so that Python doesn't
839 __main__ module around so that Python doesn't
838 840 clear it, rendering references to module globals useless.
839 841
840 842 This method keeps said reference in a private dict, keyed by the
@@ -847,9 +849,16 b' class InteractiveShell(SingletonConfigurable):'
847 849 try:
848 850 main_mod = self._main_mod_cache[filename]
849 851 except KeyError:
850 main_mod = self._main_mod_cache[filename] = FakeModule()
852 main_mod = self._main_mod_cache[filename] = types.ModuleType(modname,
853 doc="Module created for script run in IPython")
851 854 else:
852 init_fakemod_dict(main_mod)
855 main_mod.__dict__.clear()
856 main_mod.__name__ = modname
857
858 main_mod.__file__ = filename
859 # It seems pydoc (and perhaps others) needs any module instance to
860 # implement a __nonzero__ method
861 main_mod.__nonzero__ = lambda : True
853 862
854 863 return main_mod
855 864
@@ -863,7 +872,7 b' class InteractiveShell(SingletonConfigurable):'
863 872
864 873 In [15]: import IPython
865 874
866 In [16]: m = _ip.new_main_mod(IPython.__file__)
875 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
867 876
868 877 In [17]: len(_ip._main_mod_cache) > 0
869 878 Out[17]: True
@@ -550,6 +550,11 b' python-profiler package from non-free.""")'
550 550 __name__save = self.shell.user_ns['__name__']
551 551 prog_ns['__name__'] = '__main__'
552 552 main_mod = self.shell.user_module
553
554 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
555 # set the __file__ global in the script's namespace
556 # TK: Is this necessary in interactive mode?
557 prog_ns['__file__'] = filename
553 558 else:
554 559 # Run in a fresh, empty namespace
555 560 if 'n' in opts:
@@ -560,13 +565,8 b' python-profiler package from non-free.""")'
560 565 # The shell MUST hold a reference to prog_ns so after %run
561 566 # exits, the python deletion mechanism doesn't zero it out
562 567 # (leaving dangling references). See interactiveshell for details
563 main_mod = self.shell.new_main_mod(filename)
568 main_mod = self.shell.new_main_mod(filename, name)
564 569 prog_ns = main_mod.__dict__
565 prog_ns['__name__'] = name
566
567 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
568 # set the __file__ global in the script's namespace
569 prog_ns['__file__'] = filename
570 570
571 571 # pickle fix. See interactiveshell for an explanation. But we need to
572 572 # make sure that, if we overwrite __main__, we replace it at the end
@@ -9,9 +9,6 b' def test_import_crashhandler():'
9 9 def test_import_debugger():
10 10 from IPython.core import debugger
11 11
12 def test_import_fakemodule():
13 from IPython.core import fakemodule
14
15 12 def test_import_excolors():
16 13 from IPython.core import excolors
17 14
@@ -15,6 +15,7 b' from __future__ import absolute_import'
15 15
16 16 import functools
17 17 import os
18 from os.path import join as pjoin
18 19 import random
19 20 import sys
20 21 import tempfile
@@ -359,6 +360,17 b' tclass.py: deleting object: C-third'
359 360 self.mktmp(src)
360 361 _ip.magic('run -t -N 1 %s' % self.fname)
361 362 _ip.magic('run -t -N 10 %s' % self.fname)
363
364 def test_ignore_sys_exit(self):
365 """Test the -e option to ignore sys.exit()"""
366 src = "import sys; sys.exit(1)"
367 self.mktmp(src)
368 with tt.AssertPrints('SystemExit'):
369 _ip.magic('run %s' % self.fname)
370
371 with tt.AssertNotPrints('SystemExit'):
372 _ip.magic('run -e %s' % self.fname)
373
362 374
363 375
364 376 class TestMagicRunWithPackage(unittest.TestCase):
@@ -398,6 +410,7 b' class TestMagicRunWithPackage(unittest.TestCase):'
398 410 self.tempdir.cleanup()
399 411
400 412 def check_run_submodule(self, submodule, opts=''):
413 _ip.user_ns.pop('x', None)
401 414 _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts))
402 415 self.assertEqual(_ip.user_ns['x'], self.value,
403 416 'Variable `x` is not loaded from module `{0}`.'
@@ -430,3 +443,16 b' class TestMagicRunWithPackage(unittest.TestCase):'
430 443 @with_fake_debugger
431 444 def test_debug_run_submodule_with_relative_import(self):
432 445 self.check_run_submodule('relative', '-d')
446
447 def test_run__name__():
448 with TemporaryDirectory() as td:
449 path = pjoin(td, 'foo.py')
450 with open(path, 'w') as f:
451 f.write("q = __name__")
452
453 _ip.user_ns.pop('q', None)
454 _ip.magic('run {}'.format(path))
455 nt.assert_equal(_ip.user_ns.pop('q'), '__main__')
456
457 _ip.magic('run -n {}'.format(path))
458 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
@@ -27,7 +27,6 b' import inspect, os, sys, textwrap'
27 27 # Our own
28 28 from IPython.config.configurable import Configurable
29 29 from IPython.core.error import UsageError
30 from IPython.core.fakemodule import FakeModule
31 30 from IPython.core.magic import Magics, magics_class, line_magic
32 31 from IPython.testing.skipdoctest import skip_doctest
33 32 from IPython.utils.traitlets import Bool
@@ -224,7 +223,8 b' class StoreMagics(Magics, Configurable):'
224 223 raise UsageError("Unknown variable '%s'" % args[0])
225 224
226 225 else:
227 if isinstance(inspect.getmodule(obj), FakeModule):
226 modname = getattr(inspect.getmodule(obj), '__name__', '')
227 if modname == '__main__':
228 228 print textwrap.dedent("""\
229 229 Warning:%s is %s
230 230 Proper storage of interactively declared classes (or instances
@@ -35,7 +35,7 b" if __name__ == '__main__':"
35 35 r'\.zmq',
36 36 ]
37 37
38 docwriter.module_skip_patterns += [ r'\.core\.fakemodule',
38 docwriter.module_skip_patterns += [
39 39 r'\.testing\.iptest',
40 40 # Keeping these disabled is OK
41 41 r'\.parallel\.controller\.mongodb',
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now