embed.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. # encoding: utf-8
  2. """
  3. An embedded IPython shell.
  4. """
  5. # Copyright (c) IPython Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. import sys
  8. import warnings
  9. from IPython.core import ultratb, compilerop
  10. from IPython.core import magic_arguments
  11. from IPython.core.magic import Magics, magics_class, line_magic
  12. from IPython.core.interactiveshell import DummyMod, InteractiveShell
  13. from IPython.terminal.interactiveshell import TerminalInteractiveShell
  14. from IPython.terminal.ipapp import load_default_config
  15. from traitlets import Bool, CBool, Unicode
  16. from IPython.utils.io import ask_yes_no
  17. from typing import Set
  18. class KillEmbedded(Exception):pass
  19. # kept for backward compatibility as IPython 6 was released with
  20. # the typo. See https://github.com/ipython/ipython/pull/10706
  21. KillEmbeded = KillEmbedded
  22. # This is an additional magic that is exposed in embedded shells.
  23. @magics_class
  24. class EmbeddedMagics(Magics):
  25. @line_magic
  26. @magic_arguments.magic_arguments()
  27. @magic_arguments.argument('-i', '--instance', action='store_true',
  28. help='Kill instance instead of call location')
  29. @magic_arguments.argument('-x', '--exit', action='store_true',
  30. help='Also exit the current session')
  31. @magic_arguments.argument('-y', '--yes', action='store_true',
  32. help='Do not ask confirmation')
  33. def kill_embedded(self, parameter_s=''):
  34. """%kill_embedded : deactivate for good the current embedded IPython
  35. This function (after asking for confirmation) sets an internal flag so
  36. that an embedded IPython will never activate again for the given call
  37. location. This is useful to permanently disable a shell that is being
  38. called inside a loop: once you've figured out what you needed from it,
  39. you may then kill it and the program will then continue to run without
  40. the interactive shell interfering again.
  41. Kill Instance Option:
  42. If for some reasons you need to kill the location where the instance
  43. is created and not called, for example if you create a single
  44. instance in one place and debug in many locations, you can use the
  45. ``--instance`` option to kill this specific instance. Like for the
  46. ``call location`` killing an "instance" should work even if it is
  47. recreated within a loop.
  48. .. note::
  49. This was the default behavior before IPython 5.2
  50. """
  51. args = magic_arguments.parse_argstring(self.kill_embedded, parameter_s)
  52. print(args)
  53. if args.instance:
  54. # let no ask
  55. if not args.yes:
  56. kill = ask_yes_no(
  57. "Are you sure you want to kill this embedded instance? [y/N] ", 'n')
  58. else:
  59. kill = True
  60. if kill:
  61. self.shell._disable_init_location()
  62. print("This embedded IPython instance will not reactivate anymore "
  63. "once you exit.")
  64. else:
  65. if not args.yes:
  66. kill = ask_yes_no(
  67. "Are you sure you want to kill this embedded call_location? [y/N] ", 'n')
  68. else:
  69. kill = True
  70. if kill:
  71. self.shell.embedded_active = False
  72. print("This embedded IPython call location will not reactivate anymore "
  73. "once you exit.")
  74. if args.exit:
  75. # Ask-exit does not really ask, it just set internals flags to exit
  76. # on next loop.
  77. self.shell.ask_exit()
  78. @line_magic
  79. def exit_raise(self, parameter_s=''):
  80. """%exit_raise Make the current embedded kernel exit and raise and exception.
  81. This function sets an internal flag so that an embedded IPython will
  82. raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
  83. useful to permanently exit a loop that create IPython embed instance.
  84. """
  85. self.shell.should_raise = True
  86. self.shell.ask_exit()
  87. class _Sentinel:
  88. def __init__(self, repr):
  89. assert isinstance(repr, str)
  90. self.repr = repr
  91. def __repr__(self):
  92. return repr
  93. class InteractiveShellEmbed(TerminalInteractiveShell):
  94. dummy_mode = Bool(False)
  95. exit_msg = Unicode('')
  96. embedded = CBool(True)
  97. should_raise = CBool(False)
  98. # Like the base class display_banner is not configurable, but here it
  99. # is True by default.
  100. display_banner = CBool(True)
  101. exit_msg = Unicode()
  102. # When embedding, by default we don't change the terminal title
  103. term_title = Bool(False,
  104. help="Automatically set the terminal title"
  105. ).tag(config=True)
  106. _inactive_locations: Set[str] = set()
  107. def _disable_init_location(self):
  108. """Disable the current Instance creation location"""
  109. InteractiveShellEmbed._inactive_locations.add(self._init_location_id)
  110. @property
  111. def embedded_active(self):
  112. return (self._call_location_id not in InteractiveShellEmbed._inactive_locations)\
  113. and (self._init_location_id not in InteractiveShellEmbed._inactive_locations)
  114. @embedded_active.setter
  115. def embedded_active(self, value):
  116. if value:
  117. InteractiveShellEmbed._inactive_locations.discard(
  118. self._call_location_id)
  119. InteractiveShellEmbed._inactive_locations.discard(
  120. self._init_location_id)
  121. else:
  122. InteractiveShellEmbed._inactive_locations.add(
  123. self._call_location_id)
  124. def __init__(self, **kw):
  125. assert (
  126. "user_global_ns" not in kw
  127. ), "Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0."
  128. # temporary fix for https://github.com/ipython/ipython/issues/14164
  129. cls = type(self)
  130. if cls._instance is None:
  131. for subclass in cls._walk_mro():
  132. subclass._instance = self
  133. cls._instance = self
  134. clid = kw.pop('_init_location_id', None)
  135. if not clid:
  136. frame = sys._getframe(1)
  137. clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
  138. self._init_location_id = clid
  139. super(InteractiveShellEmbed,self).__init__(**kw)
  140. # don't use the ipython crash handler so that user exceptions aren't
  141. # trapped
  142. sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
  143. mode=self.xmode,
  144. call_pdb=self.pdb)
  145. def init_sys_modules(self):
  146. """
  147. Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
  148. """
  149. pass
  150. def init_magics(self):
  151. super(InteractiveShellEmbed, self).init_magics()
  152. self.register_magics(EmbeddedMagics)
  153. def __call__(
  154. self,
  155. header="",
  156. local_ns=None,
  157. module=None,
  158. dummy=None,
  159. stack_depth=1,
  160. compile_flags=None,
  161. **kw,
  162. ):
  163. """Activate the interactive interpreter.
  164. __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
  165. the interpreter shell with the given local and global namespaces, and
  166. optionally print a header string at startup.
  167. The shell can be globally activated/deactivated using the
  168. dummy_mode attribute. This allows you to turn off a shell used
  169. for debugging globally.
  170. However, *each* time you call the shell you can override the current
  171. state of dummy_mode with the optional keyword parameter 'dummy'. For
  172. example, if you set dummy mode on with IPShell.dummy_mode = True, you
  173. can still have a specific call work by making it as IPShell(dummy=False).
  174. """
  175. # we are called, set the underlying interactiveshell not to exit.
  176. self.keep_running = True
  177. # If the user has turned it off, go away
  178. clid = kw.pop('_call_location_id', None)
  179. if not clid:
  180. frame = sys._getframe(1)
  181. clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
  182. self._call_location_id = clid
  183. if not self.embedded_active:
  184. return
  185. # Normal exits from interactive mode set this flag, so the shell can't
  186. # re-enter (it checks this variable at the start of interactive mode).
  187. self.exit_now = False
  188. # Allow the dummy parameter to override the global __dummy_mode
  189. if dummy or (dummy != 0 and self.dummy_mode):
  190. return
  191. # self.banner is auto computed
  192. if header:
  193. self.old_banner2 = self.banner2
  194. self.banner2 = self.banner2 + '\n' + header + '\n'
  195. else:
  196. self.old_banner2 = ''
  197. if self.display_banner:
  198. self.show_banner()
  199. # Call the embedding code with a stack depth of 1 so it can skip over
  200. # our call and get the original caller's namespaces.
  201. self.mainloop(
  202. local_ns, module, stack_depth=stack_depth, compile_flags=compile_flags
  203. )
  204. self.banner2 = self.old_banner2
  205. if self.exit_msg is not None:
  206. print(self.exit_msg)
  207. if self.should_raise:
  208. raise KillEmbedded('Embedded IPython raising error, as user requested.')
  209. def mainloop(
  210. self,
  211. local_ns=None,
  212. module=None,
  213. stack_depth=0,
  214. compile_flags=None,
  215. ):
  216. """Embeds IPython into a running python program.
  217. Parameters
  218. ----------
  219. local_ns, module
  220. Working local namespace (a dict) and module (a module or similar
  221. object). If given as None, they are automatically taken from the scope
  222. where the shell was called, so that program variables become visible.
  223. stack_depth : int
  224. How many levels in the stack to go to looking for namespaces (when
  225. local_ns or module is None). This allows an intermediate caller to
  226. make sure that this function gets the namespace from the intended
  227. level in the stack. By default (0) it will get its locals and globals
  228. from the immediate caller.
  229. compile_flags
  230. A bit field identifying the __future__ features
  231. that are enabled, as passed to the builtin :func:`compile` function.
  232. If given as None, they are automatically taken from the scope where
  233. the shell was called.
  234. """
  235. # Get locals and globals from caller
  236. if ((local_ns is None or module is None or compile_flags is None)
  237. and self.default_user_namespaces):
  238. call_frame = sys._getframe(stack_depth).f_back
  239. if local_ns is None:
  240. local_ns = call_frame.f_locals
  241. if module is None:
  242. global_ns = call_frame.f_globals
  243. try:
  244. module = sys.modules[global_ns['__name__']]
  245. except KeyError:
  246. warnings.warn("Failed to get module %s" % \
  247. global_ns.get('__name__', 'unknown module')
  248. )
  249. module = DummyMod()
  250. module.__dict__ = global_ns
  251. if compile_flags is None:
  252. compile_flags = (call_frame.f_code.co_flags &
  253. compilerop.PyCF_MASK)
  254. # Save original namespace and module so we can restore them after
  255. # embedding; otherwise the shell doesn't shut down correctly.
  256. orig_user_module = self.user_module
  257. orig_user_ns = self.user_ns
  258. orig_compile_flags = self.compile.flags
  259. # Update namespaces and fire up interpreter
  260. # The global one is easy, we can just throw it in
  261. if module is not None:
  262. self.user_module = module
  263. # But the user/local one is tricky: ipython needs it to store internal
  264. # data, but we also need the locals. We'll throw our hidden variables
  265. # like _ih and get_ipython() into the local namespace, but delete them
  266. # later.
  267. if local_ns is not None:
  268. reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
  269. self.user_ns = reentrant_local_ns
  270. self.init_user_ns()
  271. # Compiler flags
  272. if compile_flags is not None:
  273. self.compile.flags = compile_flags
  274. # make sure the tab-completer has the correct frame information, so it
  275. # actually completes using the frame's locals/globals
  276. self.set_completer_frame()
  277. with self.builtin_trap, self.display_trap:
  278. self.interact()
  279. # now, purge out the local namespace of IPython's hidden variables.
  280. if local_ns is not None:
  281. local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
  282. # Restore original namespace so shell can shut down when we exit.
  283. self.user_module = orig_user_module
  284. self.user_ns = orig_user_ns
  285. self.compile.flags = orig_compile_flags
  286. def embed(*, header="", compile_flags=None, **kwargs):
  287. """Call this to embed IPython at the current point in your program.
  288. The first invocation of this will create a :class:`terminal.embed.InteractiveShellEmbed`
  289. instance and then call it. Consecutive calls just call the already
  290. created instance.
  291. If you don't want the kernel to initialize the namespace
  292. from the scope of the surrounding function,
  293. and/or you want to load full IPython configuration,
  294. you probably want `IPython.start_ipython()` instead.
  295. Here is a simple example::
  296. from IPython import embed
  297. a = 10
  298. b = 20
  299. embed(header='First time')
  300. c = 30
  301. d = 40
  302. embed()
  303. Parameters
  304. ----------
  305. header : str
  306. Optional header string to print at startup.
  307. compile_flags
  308. Passed to the `compile_flags` parameter of :py:meth:`terminal.embed.InteractiveShellEmbed.mainloop()`,
  309. which is called when the :class:`terminal.embed.InteractiveShellEmbed` instance is called.
  310. **kwargs : various, optional
  311. Any other kwargs will be passed to the :class:`terminal.embed.InteractiveShellEmbed` constructor.
  312. Full customization can be done by passing a traitlets :class:`Config` in as the
  313. `config` argument (see :ref:`configure_start_ipython` and :ref:`terminal_options`).
  314. """
  315. config = kwargs.get('config')
  316. if config is None:
  317. config = load_default_config()
  318. config.InteractiveShellEmbed = config.TerminalInteractiveShell
  319. kwargs['config'] = config
  320. using = kwargs.get('using', 'sync')
  321. if using :
  322. kwargs['config'].update({'TerminalInteractiveShell':{'loop_runner':using, 'colors':'NoColor', 'autoawait': using!='sync'}})
  323. #save ps1/ps2 if defined
  324. ps1 = None
  325. ps2 = None
  326. try:
  327. ps1 = sys.ps1
  328. ps2 = sys.ps2
  329. except AttributeError:
  330. pass
  331. #save previous instance
  332. saved_shell_instance = InteractiveShell._instance
  333. if saved_shell_instance is not None:
  334. cls = type(saved_shell_instance)
  335. cls.clear_instance()
  336. frame = sys._getframe(1)
  337. shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
  338. frame.f_code.co_filename, frame.f_lineno), **kwargs)
  339. shell(header=header, stack_depth=2, compile_flags=compile_flags,
  340. _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
  341. InteractiveShellEmbed.clear_instance()
  342. #restore previous instance
  343. if saved_shell_instance is not None:
  344. cls = type(saved_shell_instance)
  345. cls.clear_instance()
  346. for subclass in cls._walk_mro():
  347. subclass._instance = saved_shell_instance
  348. if ps1 is not None:
  349. sys.ps1 = ps1
  350. sys.ps2 = ps2