summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-04-20 22:55:29 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-04-20 22:55:29 -0400
commitf05dfe73d675e198ad271fa23c7dcdcd1f988551 (patch)
tree2fbca752e6cae94dcfd2ce37286a4c296b82d633
parent0ddf61a2b8640d38b182be9100f35350cb2c96f8 (diff)
downloadcmd2-git-f05dfe73d675e198ad271fa23c7dcdcd1f988551.tar.gz
cmd2 now considers ipy a pyscript environment
-rw-r--r--CHANGELOG.md6
-rw-r--r--cmd2/cmd2.py25
-rw-r--r--tests/test_run_pyscript.py2
3 files changed, 22 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 32935891..01180001 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,9 @@
## 1.0.3 (TBD, 2020)
* Enhancements
- * `ipy` now returns whether any of the commands run in it returned True to stop command loop. This is consistent
- with the `py` command.
+ * Made `ipy` consistent with `py` in the following ways
+ * `ipy` returns whether any of the commands run in it returned True to stop command loop
+ * `Cmd.in_pyscript()` returns True while in `ipy`.
+ * Starting `ipy` when `Cmd.in_pyscript()` is already True is not allowed.
## 1.0.2 (April 06, 2020)
* Bug Fixes
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 99ddffa3..c22cb76d 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3191,7 +3191,7 @@ class Cmd(cmd.Cmd):
saved_sys_path = None
if self.in_pyscript():
- err = "Recursively entering interactive Python consoles is not allowed."
+ err = "Recursively entering interactive Python shells is not allowed."
self.perror(err)
return
@@ -3341,9 +3341,6 @@ class Cmd(cmd.Cmd):
:return: True if running of commands should stop
"""
from .py_bridge import PyBridge
- banner = ('Entering an embedded IPython shell. Type quit or <Ctrl>-d to exit.\n'
- 'Run Python code from external files with: run filename.py\n')
- exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0])
# noinspection PyUnusedLocal
def load_ipy(cmd2_app: Cmd, py_bridge: PyBridge):
@@ -3364,11 +3361,23 @@ class Cmd(cmd.Cmd):
del cmd2_app
del py_bridge
- embed(banner1=banner, exit_msg=exit_msg)
+ # Start ipy shell
+ embed(banner1=('Entering an embedded IPython shell. Type quit or <Ctrl>-d to exit.\n'
+ 'Run Python code from external files with: run filename.py\n'),
+ exit_msg='Leaving IPython, back to {}'.format(sys.argv[0]))
- new_py_bridge = PyBridge(self)
- load_ipy(self, new_py_bridge)
- return new_py_bridge.stop
+ if self.in_pyscript():
+ err = "Recursively entering interactive Python shells is not allowed."
+ self.perror(err)
+ return
+
+ try:
+ self._in_py = True
+ new_py_bridge = PyBridge(self)
+ load_ipy(self, new_py_bridge)
+ return new_py_bridge.stop
+ finally:
+ self._in_py = False
history_description = "View, run, edit, save, or clear previously entered commands"
diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py
index 12257fc5..1776614f 100644
--- a/tests/test_run_pyscript.py
+++ b/tests/test_run_pyscript.py
@@ -35,7 +35,7 @@ def test_run_pyscript(base_app, request):
def test_run_pyscript_recursive_not_allowed(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
python_script = os.path.join(test_dir, 'pyscript', 'recursive.py')
- expected = 'Recursively entering interactive Python consoles is not allowed.'
+ expected = 'Recursively entering interactive Python shells is not allowed.'
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
assert err[0] == expected