Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/__init__.py: 63%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# PYTHON_ARGCOMPLETE_OK
2"""
3IPython: tools for interactive and parallel computing in Python.
5https://ipython.org
6"""
7#-----------------------------------------------------------------------------
8# Copyright (c) 2008-2011, IPython Development Team.
9# Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
10# Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
11# Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
12#
13# Distributed under the terms of the Modified BSD License.
14#
15# The full license is in the file COPYING.txt, distributed with this software.
16#-----------------------------------------------------------------------------
18#-----------------------------------------------------------------------------
19# Imports
20#-----------------------------------------------------------------------------
22import sys
23import warnings
24from typing import Any
26#-----------------------------------------------------------------------------
27# Setup everything
28#-----------------------------------------------------------------------------
30# Don't forget to also update setup.py when this changes!
31#
32# NOTE: these imports look like they could be made lazy (PEP 562) to speed up
33# `import IPython` considerably, but downstream projects (pyflyby at least)
34# rely on the transitive side effects: they do `import IPython` and then
35# access attribute chains like `IPython.terminal.ipapp.TerminalIPythonApp`,
36# which only resolve because the imports below load those submodules.
37from .core.getipython import get_ipython
38from .core import release
39from .core.application import Application
40from .terminal.embed import embed
42from .core.interactiveshell import InteractiveShell
43from .utils.sysinfo import sys_info
44from .utils.frame import extract_module_locals
46__all__ = ["start_ipython", "embed", "embed_kernel"]
48# Release data
49__author__ = '{} <{}>'.format(release.author, release.author_email)
50__license__ = release.license
51__version__ = release.version
52version_info = release.version_info
53# list of CVEs that should have been patched in this release.
54# this is informational and should not be relied upon.
55__patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"}
58def embed_kernel(module=None, local_ns=None, **kwargs):
59 """Embed and start an IPython kernel in a given scope.
61 If you don't want the kernel to initialize the namespace
62 from the scope of the surrounding function,
63 and/or you want to load full IPython configuration,
64 you probably want `IPython.start_kernel()` instead.
66 This is a deprecated alias for `ipykernel.embed.embed_kernel()`,
67 to be removed in the future.
68 You should import directly from `ipykernel.embed`; this wrapper
69 fails anyway if you don't have `ipykernel` package installed.
71 Parameters
72 ----------
73 module : types.ModuleType, optional
74 The module to load into IPython globals (default: caller)
75 local_ns : dict, optional
76 The namespace to load into IPython user namespace (default: caller)
77 **kwargs : various, optional
78 Further keyword args are relayed to the IPKernelApp constructor,
79 such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
80 allowing configuration of the kernel. Will only have an effect
81 on the first embed_kernel call for a given process.
82 """
84 warnings.warn(
85 "import embed_kernel from ipykernel.embed directly (since 2013)."
86 " Importing from IPython will be removed in the future",
87 DeprecationWarning,
88 stacklevel=2,
89 )
91 (caller_module, caller_locals) = extract_module_locals(1)
92 if module is None:
93 module = caller_module
94 if local_ns is None:
95 local_ns = dict(**caller_locals)
97 # Only import .zmq when we really need it
98 from ipykernel.embed import embed_kernel as real_embed_kernel
99 real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
101def start_ipython(argv: list[str] | None = None, **kwargs: Any) -> Any:
102 """Launch a normal IPython instance (as opposed to embedded)
104 `IPython.embed()` puts a shell in a particular calling scope,
105 such as a function or method for debugging purposes,
106 which is often not desirable.
108 `start_ipython()` does full, regular IPython initialization,
109 including loading startup files, configuration, etc.
110 much of which is skipped by `embed()`.
112 This is a public API method, and will survive implementation changes.
114 Parameters
115 ----------
116 argv : list or None, optional
117 If unspecified or None, IPython will parse command-line options from sys.argv.
118 To prevent any command-line parsing, pass an empty list: `argv=[]`.
119 user_ns : dict, optional
120 specify this dictionary to initialize the IPython user namespace with particular values.
121 **kwargs : various, optional
122 Any other kwargs will be passed to the Application constructor,
123 such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
124 allowing configuration of the instance (see :ref:`terminal_options`).
125 """
126 from IPython.terminal.ipapp import launch_new_instance
127 return launch_new_instance(argv=argv, **kwargs)