##// 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 from IPython.core.displaypub import DisplayPublisher
47 from IPython.core.displaypub import DisplayPublisher
48 from IPython.core.error import UsageError
48 from IPython.core.error import UsageError
49 from IPython.core.extensions import ExtensionManager
49 from IPython.core.extensions import ExtensionManager
50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
51 from IPython.core.formatters import DisplayFormatter
50 from IPython.core.formatters import DisplayFormatter
52 from IPython.core.history import HistoryManager
51 from IPython.core.history import HistoryManager
53 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
52 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
@@ -826,15 +825,18 b' class InteractiveShell(SingletonConfigurable):'
826 # Things related to the "main" module
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 """Return a new 'main' module object for user code execution.
829 """Return a new 'main' module object for user code execution.
831
830
832 ``filename`` should be the path of the script which will be run in the
831 ``filename`` should be the path of the script which will be run in the
833 module. Requests with the same filename will get the same module, with
832 module. Requests with the same filename will get the same module, with
834 its namespace cleared.
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 When scripts are executed via %run, we must keep a reference to their
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 clear it, rendering references to module globals useless.
840 clear it, rendering references to module globals useless.
839
841
840 This method keeps said reference in a private dict, keyed by the
842 This method keeps said reference in a private dict, keyed by the
@@ -847,9 +849,16 b' class InteractiveShell(SingletonConfigurable):'
847 try:
849 try:
848 main_mod = self._main_mod_cache[filename]
850 main_mod = self._main_mod_cache[filename]
849 except KeyError:
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 else:
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 return main_mod
863 return main_mod
855
864
@@ -863,7 +872,7 b' class InteractiveShell(SingletonConfigurable):'
863
872
864 In [15]: import IPython
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 In [17]: len(_ip._main_mod_cache) > 0
877 In [17]: len(_ip._main_mod_cache) > 0
869 Out[17]: True
878 Out[17]: True
@@ -550,6 +550,11 b' python-profiler package from non-free.""")'
550 __name__save = self.shell.user_ns['__name__']
550 __name__save = self.shell.user_ns['__name__']
551 prog_ns['__name__'] = '__main__'
551 prog_ns['__name__'] = '__main__'
552 main_mod = self.shell.user_module
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 else:
558 else:
554 # Run in a fresh, empty namespace
559 # Run in a fresh, empty namespace
555 if 'n' in opts:
560 if 'n' in opts:
@@ -560,13 +565,8 b' python-profiler package from non-free.""")'
560 # The shell MUST hold a reference to prog_ns so after %run
565 # The shell MUST hold a reference to prog_ns so after %run
561 # exits, the python deletion mechanism doesn't zero it out
566 # exits, the python deletion mechanism doesn't zero it out
562 # (leaving dangling references). See interactiveshell for details
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 prog_ns = main_mod.__dict__
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 # pickle fix. See interactiveshell for an explanation. But we need to
571 # pickle fix. See interactiveshell for an explanation. But we need to
572 # make sure that, if we overwrite __main__, we replace it at the end
572 # make sure that, if we overwrite __main__, we replace it at the end
@@ -9,9 +9,6 b' def test_import_crashhandler():'
9 def test_import_debugger():
9 def test_import_debugger():
10 from IPython.core import debugger
10 from IPython.core import debugger
11
11
12 def test_import_fakemodule():
13 from IPython.core import fakemodule
14
15 def test_import_excolors():
12 def test_import_excolors():
16 from IPython.core import excolors
13 from IPython.core import excolors
17
14
@@ -15,6 +15,7 b' from __future__ import absolute_import'
15
15
16 import functools
16 import functools
17 import os
17 import os
18 from os.path import join as pjoin
18 import random
19 import random
19 import sys
20 import sys
20 import tempfile
21 import tempfile
@@ -359,6 +360,17 b' tclass.py: deleting object: C-third'
359 self.mktmp(src)
360 self.mktmp(src)
360 _ip.magic('run -t -N 1 %s' % self.fname)
361 _ip.magic('run -t -N 1 %s' % self.fname)
361 _ip.magic('run -t -N 10 %s' % self.fname)
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 class TestMagicRunWithPackage(unittest.TestCase):
376 class TestMagicRunWithPackage(unittest.TestCase):
@@ -398,6 +410,7 b' class TestMagicRunWithPackage(unittest.TestCase):'
398 self.tempdir.cleanup()
410 self.tempdir.cleanup()
399
411
400 def check_run_submodule(self, submodule, opts=''):
412 def check_run_submodule(self, submodule, opts=''):
413 _ip.user_ns.pop('x', None)
401 _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts))
414 _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts))
402 self.assertEqual(_ip.user_ns['x'], self.value,
415 self.assertEqual(_ip.user_ns['x'], self.value,
403 'Variable `x` is not loaded from module `{0}`.'
416 'Variable `x` is not loaded from module `{0}`.'
@@ -430,3 +443,16 b' class TestMagicRunWithPackage(unittest.TestCase):'
430 @with_fake_debugger
443 @with_fake_debugger
431 def test_debug_run_submodule_with_relative_import(self):
444 def test_debug_run_submodule_with_relative_import(self):
432 self.check_run_submodule('relative', '-d')
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 # Our own
27 # Our own
28 from IPython.config.configurable import Configurable
28 from IPython.config.configurable import Configurable
29 from IPython.core.error import UsageError
29 from IPython.core.error import UsageError
30 from IPython.core.fakemodule import FakeModule
31 from IPython.core.magic import Magics, magics_class, line_magic
30 from IPython.core.magic import Magics, magics_class, line_magic
32 from IPython.testing.skipdoctest import skip_doctest
31 from IPython.testing.skipdoctest import skip_doctest
33 from IPython.utils.traitlets import Bool
32 from IPython.utils.traitlets import Bool
@@ -224,7 +223,8 b' class StoreMagics(Magics, Configurable):'
224 raise UsageError("Unknown variable '%s'" % args[0])
223 raise UsageError("Unknown variable '%s'" % args[0])
225
224
226 else:
225 else:
227 if isinstance(inspect.getmodule(obj), FakeModule):
226 modname = getattr(inspect.getmodule(obj), '__name__', '')
227 if modname == '__main__':
228 print textwrap.dedent("""\
228 print textwrap.dedent("""\
229 Warning:%s is %s
229 Warning:%s is %s
230 Proper storage of interactively declared classes (or instances
230 Proper storage of interactively declared classes (or instances
@@ -35,7 +35,7 b" if __name__ == '__main__':"
35 r'\.zmq',
35 r'\.zmq',
36 ]
36 ]
37
37
38 docwriter.module_skip_patterns += [ r'\.core\.fakemodule',
38 docwriter.module_skip_patterns += [
39 r'\.testing\.iptest',
39 r'\.testing\.iptest',
40 # Keeping these disabled is OK
40 # Keeping these disabled is OK
41 r'\.parallel\.controller\.mongodb',
41 r'\.parallel\.controller\.mongodb',
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now