embed.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 InteractiveShell, make_main_module_type
  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(
  143. theme_name=self.colors,
  144. mode=self.xmode,
  145. call_pdb=self.pdb,
  146. )
  147. def init_sys_modules(self):
  148. """
  149. Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
  150. """
  151. pass
  152. def init_magics(self):
  153. super(InteractiveShellEmbed, self).init_magics()
  154. self.register_magics(EmbeddedMagics)
  155. def __call__(
  156. self,
  157. header="",
  158. local_ns=None,
  159. module=None,
  160. dummy=None,
  161. stack_depth=1,
  162. compile_flags=None,
  163. **kw,
  164. ):
  165. """Activate the interactive interpreter.
  166. __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
  167. the interpreter shell with the given local and global namespaces, and
  168. optionally print a header string at startup.
  169. The shell can be globally activated/deactivated using the
  170. dummy_mode attribute. This allows you to turn off a shell used
  171. for debugging globally.
  172. However, *each* time you call the shell you can override the current
  173. state of dummy_mode with the optional keyword parameter 'dummy'. For
  174. example, if you set dummy mode on with IPShell.dummy_mode = True, you
  175. can still have a specific call work by making it as IPShell(dummy=False).
  176. """
  177. # we are called, set the underlying interactiveshell not to exit.
  178. self.keep_running = True
  179. # If the user has turned it off, go away
  180. clid = kw.pop('_call_location_id', None)
  181. if not clid:
  182. frame = sys._getframe(1)
  183. clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
  184. self._call_location_id = clid
  185. if not self.embedded_active:
  186. return
  187. # Normal exits from interactive mode set this flag, so the shell can't
  188. # re-enter (it checks this variable at the start of interactive mode).
  189. self.exit_now = False
  190. # Allow the dummy parameter to override the global __dummy_mode
  191. if dummy or (dummy != 0 and self.dummy_mode):
  192. return
  193. # self.banner is auto computed
  194. if header:
  195. self.old_banner2 = self.banner2
  196. self.banner2 = self.banner2 + '\n' + header + '\n'
  197. else:
  198. self.old_banner2 = ''
  199. if self.display_banner:
  200. self.show_banner()
  201. # Call the embedding code with a stack depth of 1 so it can skip over
  202. # our call and get the original caller's namespaces.
  203. self.mainloop(
  204. local_ns, module, stack_depth=stack_depth, compile_flags=compile_flags
  205. )
  206. self.banner2 = self.old_banner2
  207. if self.exit_msg is not None:
  208. print(self.exit_msg)
  209. if self.should_raise:
  210. raise KillEmbedded('Embedded IPython raising error, as user requested.')
  211. def mainloop(
  212. self,
  213. local_ns=None,
  214. module=None,
  215. stack_depth=0,
  216. compile_flags=None,
  217. ):
  218. """Embeds IPython into a running python program.
  219. Parameters
  220. ----------
  221. local_ns, module
  222. Working local namespace (a dict) and module (a module or similar
  223. object). If given as None, they are automatically taken from the scope
  224. where the shell was called, so that program variables become visible.
  225. stack_depth : int
  226. How many levels in the stack to go to looking for namespaces (when
  227. local_ns or module is None). This allows an intermediate caller to
  228. make sure that this function gets the namespace from the intended
  229. level in the stack. By default (0) it will get its locals and globals
  230. from the immediate caller.
  231. compile_flags
  232. A bit field identifying the __future__ features
  233. that are enabled, as passed to the builtin :func:`compile` function.
  234. If given as None, they are automatically taken from the scope where
  235. the shell was called.
  236. """
  237. # Get locals and globals from caller
  238. if ((local_ns is None or module is None or compile_flags is None)
  239. and self.default_user_namespaces):
  240. call_frame = sys._getframe(stack_depth).f_back
  241. if local_ns is None:
  242. local_ns = call_frame.f_locals
  243. if module is None:
  244. global_ns = call_frame.f_globals
  245. try:
  246. module = sys.modules[global_ns['__name__']]
  247. except KeyError:
  248. warnings.warn("Failed to get module %s" % \
  249. global_ns.get('__name__', 'unknown module')
  250. )
  251. module = make_main_module_type(global_ns)()
  252. if compile_flags is None:
  253. compile_flags = (call_frame.f_code.co_flags &
  254. compilerop.PyCF_MASK)
  255. # Save original namespace and module so we can restore them after
  256. # embedding; otherwise the shell doesn't shut down correctly.
  257. orig_user_module = self.user_module
  258. orig_user_ns = self.user_ns
  259. orig_compile_flags = self.compile.flags
  260. # Update namespaces and fire up interpreter
  261. # The global one is easy, we can just throw it in
  262. if module is not None:
  263. self.user_module = module
  264. # But the user/local one is tricky: ipython needs it to store internal
  265. # data, but we also need the locals. We'll throw our hidden variables
  266. # like _ih and get_ipython() into the local namespace, but delete them
  267. # later.
  268. if local_ns is not None:
  269. reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
  270. self.user_ns = reentrant_local_ns
  271. self.init_user_ns()
  272. # Compiler flags
  273. if compile_flags is not None:
  274. self.compile.flags = compile_flags
  275. # make sure the tab-completer has the correct frame information, so it
  276. # actually completes using the frame's locals/globals
  277. self.set_completer_frame()
  278. with self.builtin_trap, self.display_trap:
  279. self.interact()
  280. # now, purge out the local namespace of IPython's hidden variables.
  281. if local_ns is not None:
  282. local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
  283. # Restore original namespace so shell can shut down when we exit.
  284. self.user_module = orig_user_module
  285. self.user_ns = orig_user_ns
  286. self.compile.flags = orig_compile_flags
  287. def embed(*, header="", compile_flags=None, **kwargs):
  288. """Call this to embed IPython at the current point in your program.
  289. The first invocation of this will create a :class:`terminal.embed.InteractiveShellEmbed`
  290. instance and then call it. Consecutive calls just call the already
  291. created instance.
  292. If you don't want the kernel to initialize the namespace
  293. from the scope of the surrounding function,
  294. and/or you want to load full IPython configuration,
  295. you probably want `IPython.start_ipython()` instead.
  296. Here is a simple example::
  297. from IPython import embed
  298. a = 10
  299. b = 20
  300. embed(header='First time')
  301. c = 30
  302. d = 40
  303. embed()
  304. Parameters
  305. ----------
  306. header : str
  307. Optional header string to print at startup.
  308. compile_flags
  309. Passed to the `compile_flags` parameter of :py:meth:`terminal.embed.InteractiveShellEmbed.mainloop()`,
  310. which is called when the :class:`terminal.embed.InteractiveShellEmbed` instance is called.
  311. **kwargs : various, optional
  312. Any other kwargs will be passed to the :class:`terminal.embed.InteractiveShellEmbed` constructor.
  313. Full customization can be done by passing a traitlets :class:`Config` in as the
  314. `config` argument (see :ref:`configure_start_ipython` and :ref:`terminal_options`).
  315. """
  316. config = kwargs.get('config')
  317. if config is None:
  318. config = load_default_config()
  319. config.InteractiveShellEmbed = config.TerminalInteractiveShell
  320. kwargs["config"] = config
  321. using = kwargs.get("using", "sync")
  322. colors = kwargs.pop("colors", "nocolor")
  323. if using:
  324. kwargs["config"].update(
  325. {
  326. "TerminalInteractiveShell": {
  327. "loop_runner": using,
  328. "colors": colors,
  329. "autoawait": using != "sync",
  330. }
  331. }
  332. )
  333. # save ps1/ps2 if defined
  334. ps1 = None
  335. ps2 = None
  336. try:
  337. ps1 = sys.ps1
  338. ps2 = sys.ps2
  339. except AttributeError:
  340. pass
  341. #save previous instance
  342. saved_shell_instance = InteractiveShell._instance
  343. if saved_shell_instance is not None:
  344. cls = type(saved_shell_instance)
  345. cls.clear_instance()
  346. frame = sys._getframe(1)
  347. shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
  348. frame.f_code.co_filename, frame.f_lineno), **kwargs)
  349. shell(header=header, stack_depth=2, compile_flags=compile_flags,
  350. _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
  351. InteractiveShellEmbed.clear_instance()
  352. #restore previous instance
  353. if saved_shell_instance is not None:
  354. cls = type(saved_shell_instance)
  355. cls.clear_instance()
  356. for subclass in cls._walk_mro():
  357. subclass._instance = saved_shell_instance
  358. if ps1 is not None:
  359. sys.ps1 = ps1
  360. sys.ps2 = ps2