| 1 |
# PYTHON_ARGCOMPLETE_OK |
| 2 |
""" |
| 3 |
IPython: tools for interactive and parallel computing in Python. |
| 4 |
|
| 5 |
https://ipython.org |
| 6 |
""" |
| 7 |
#----------------------------------------------------------------------------- |
| 8 |
# Copyright (c) 2008-2011, IPython Development Team. |
| 9 |
# Copyright (c) 2001-2007, Fernando Perez <[email protected]> |
| 10 |
# Copyright (c) 2001, Janko Hauser <[email protected]> |
| 11 |
# Copyright (c) 2001, Nathaniel Gray <[email protected]> |
| 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 |
#----------------------------------------------------------------------------- |
| 17 |
|
| 18 |
#----------------------------------------------------------------------------- |
| 19 |
# Imports |
| 20 |
#----------------------------------------------------------------------------- |
| 21 |
|
| 22 |
import sys |
| 23 |
import warnings |
| 24 |
|
| 25 |
#----------------------------------------------------------------------------- |
| 26 |
# Setup everything |
| 27 |
#----------------------------------------------------------------------------- |
| 28 |
|
| 29 |
# Don't forget to also update setup.py when this changes! |
| 30 |
if sys.version_info < (3, 12): |
| 31 |
raise ImportError( |
| 32 |
""" |
| 33 |
IPython 9.x supports Python 3.12 and above, following SPEC0 |
| 34 |
IPython 8.31+ supports Python 3.11 and above, following SPEC0 |
| 35 |
IPython 8.19+ supports Python 3.10 and above, following SPEC0. |
| 36 |
IPython 8.13+ supports Python 3.9 and above, following NEP 29. |
| 37 |
When using Python 2.7, please install IPython 5.x LTS Long Term Support version. |
| 38 |
Python 3.3 and 3.4 were supported up to IPython 6.x. |
| 39 |
Python 3.5 was supported with IPython 7.0 to 7.9. |
| 40 |
Python 3.6 was supported with IPython up to 7.16. |
| 41 |
Python 3.7 was still supported with the 7.x branch. |
| 42 |
|
| 43 |
See IPython `README.rst` file for more information: |
| 44 |
|
| 45 |
https://github.com/ipython/ipython/blob/main/README.rst |
| 46 |
|
| 47 |
""" |
| 48 |
) |
| 49 |
|
| 50 |
#----------------------------------------------------------------------------- |
| 51 |
# Setup the top level names |
| 52 |
#----------------------------------------------------------------------------- |
| 53 |
|
| 54 |
from .core.getipython import get_ipython |
| 55 |
from .core import release |
| 56 |
from .core.application import Application |
| 57 |
from .terminal.embed import embed |
| 58 |
|
| 59 |
from .core.interactiveshell import InteractiveShell |
| 60 |
from .utils.sysinfo import sys_info |
| 61 |
from .utils.frame import extract_module_locals |
| 62 |
|
| 63 |
__all__ = ["start_ipython", "embed", "embed_kernel"] |
| 64 |
|
| 65 |
# Release data |
| 66 |
__author__ = '%s <%s>' % (release.author, release.author_email) |
| 67 |
__license__ = release.license |
| 68 |
__version__ = release.version |
| 69 |
version_info = release.version_info |
| 70 |
# list of CVEs that should have been patched in this release. |
| 71 |
# this is informational and should not be relied upon. |
| 72 |
__patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"} |
| 73 |
|
| 74 |
|
| 75 |
def embed_kernel(module=None, local_ns=None, **kwargs): |
| 76 |
"""Embed and start an IPython kernel in a given scope. |
| 77 |
|
| 78 |
If you don't want the kernel to initialize the namespace |
| 79 |
from the scope of the surrounding function, |
| 80 |
and/or you want to load full IPython configuration, |
| 81 |
you probably want `IPython.start_kernel()` instead. |
| 82 |
|
| 83 |
This is a deprecated alias for `ipykernel.embed.embed_kernel()`, |
| 84 |
to be removed in the future. |
| 85 |
You should import directly from `ipykernel.embed`; this wrapper |
| 86 |
fails anyway if you don't have `ipykernel` package installed. |
| 87 |
|
| 88 |
Parameters |
| 89 |
---------- |
| 90 |
module : types.ModuleType, optional |
| 91 |
The module to load into IPython globals (default: caller) |
| 92 |
local_ns : dict, optional |
| 93 |
The namespace to load into IPython user namespace (default: caller) |
| 94 |
**kwargs : various, optional |
| 95 |
Further keyword args are relayed to the IPKernelApp constructor, |
| 96 |
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`), |
| 97 |
allowing configuration of the kernel. Will only have an effect |
| 98 |
on the first embed_kernel call for a given process. |
| 99 |
""" |
| 100 |
|
| 101 |
warnings.warn( |
| 102 |
"import embed_kernel from ipykernel.embed directly (since 2013)." |
| 103 |
" Importing from IPython will be removed in the future", |
| 104 |
DeprecationWarning, |
| 105 |
stacklevel=2, |
| 106 |
) |
| 107 |
|
| 108 |
(caller_module, caller_locals) = extract_module_locals(1) |
| 109 |
if module is None: |
| 110 |
module = caller_module |
| 111 |
if local_ns is None: |
| 112 |
local_ns = dict(**caller_locals) |
| 113 |
|
| 114 |
# Only import .zmq when we really need it |
| 115 |
from ipykernel.embed import embed_kernel as real_embed_kernel |
| 116 |
real_embed_kernel(module=module, local_ns=local_ns, **kwargs) |
| 117 |
|
| 118 |
def start_ipython(argv=None, **kwargs): |
| 119 |
"""Launch a normal IPython instance (as opposed to embedded) |
| 120 |
|
| 121 |
`IPython.embed()` puts a shell in a particular calling scope, |
| 122 |
such as a function or method for debugging purposes, |
| 123 |
which is often not desirable. |
| 124 |
|
| 125 |
`start_ipython()` does full, regular IPython initialization, |
| 126 |
including loading startup files, configuration, etc. |
| 127 |
much of which is skipped by `embed()`. |
| 128 |
|
| 129 |
This is a public API method, and will survive implementation changes. |
| 130 |
|
| 131 |
Parameters |
| 132 |
---------- |
| 133 |
argv : list or None, optional |
| 134 |
If unspecified or None, IPython will parse command-line options from sys.argv. |
| 135 |
To prevent any command-line parsing, pass an empty list: `argv=[]`. |
| 136 |
user_ns : dict, optional |
| 137 |
specify this dictionary to initialize the IPython user namespace with particular values. |
| 138 |
**kwargs : various, optional |
| 139 |
Any other kwargs will be passed to the Application constructor, |
| 140 |
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`), |
| 141 |
allowing configuration of the instance (see :ref:`terminal_options`). |
| 142 |
""" |
| 143 |
from IPython.terminal.ipapp import launch_new_instance |
| 144 |
return launch_new_instance(argv=argv, **kwargs) |