Skip to content

Commit 0af8018

Browse files
authored
Merge pull request #124 from python-cmd2/py_ruggedization
Py command ruggedization
2 parents 4dd457a + 5c3d057 commit 0af8018

1 file changed

Lines changed: 50 additions & 46 deletions

File tree

cmd2.py

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,67 +1516,71 @@ def do_py(self, arg):
15161516
py: Enters interactive Python mode.
15171517
End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
15181518
Non-python commands can be issued with ``cmd("your command")``.
1519-
Run python code from external script files with ``run("filename.py")``
1519+
Run python code from external script files with ``run("script.py")``
15201520
"""
15211521
if self._in_py:
15221522
self.perror("Recursively entering interactive Python consoles is not allowed.", traceback_war=False)
15231523
return
15241524
self._in_py = True
15251525

1526-
self.pystate['self'] = self
1527-
arg = arg.parsed.raw[2:].strip()
1526+
try:
1527+
self.pystate['self'] = self
1528+
arg = arg.strip()
15281529

1529-
# Support the run command even if called prior to invoking an interactive interpreter
1530-
def run(filename):
1531-
"""Run a Python script file in the interactive console.
1530+
# Support the run command even if called prior to invoking an interactive interpreter
1531+
def run(filename):
1532+
"""Run a Python script file in the interactive console.
15321533
1533-
:param filename: str - filename of *.py script file to run
1534-
"""
1535-
try:
1536-
with open(filename) as f:
1537-
interp.runcode(f.read())
1538-
except IOError as e:
1539-
self.perror(e)
1534+
:param filename: str - filename of *.py script file to run
1535+
"""
1536+
try:
1537+
with open(filename) as f:
1538+
interp.runcode(f.read())
1539+
except IOError as e:
1540+
self.perror(e)
15401541

1541-
def onecmd_plus_hooks(cmd_plus_args):
1542-
"""Run a cmd2.Cmd command from a Python script or the interactive Python console.
1542+
def onecmd_plus_hooks(cmd_plus_args):
1543+
"""Run a cmd2.Cmd command from a Python script or the interactive Python console.
15431544
1544-
:param cmd_plus_args: str - command line including command and arguments to run
1545-
:return: bool - True if cmdloop() should exit once leaving the interactive Python console, False otherwise.
1546-
"""
1547-
return self.onecmd_plus_hooks(cmd_plus_args + '\n')
1545+
:param cmd_plus_args: str - command line including command and arguments to run
1546+
:return: bool - True if cmdloop() should exit once leaving the interactive Python console, False otherwise.
1547+
"""
1548+
return self.onecmd_plus_hooks(cmd_plus_args + '\n')
15481549

1549-
self.pystate['run'] = run
1550-
self.pystate['cmd'] = onecmd_plus_hooks
1550+
self.pystate['run'] = run
1551+
self.pystate['cmd'] = onecmd_plus_hooks
15511552

1552-
localvars = (self.locals_in_py and self.pystate) or {}
1553-
interp = InteractiveConsole(locals=localvars)
1554-
interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())')
1553+
localvars = (self.locals_in_py and self.pystate) or {}
1554+
interp = InteractiveConsole(locals=localvars)
1555+
interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())')
15551556

1556-
if arg.strip():
1557-
interp.runcode(arg)
1558-
else:
1559-
# noinspection PyShadowingBuiltins
1560-
def quit():
1561-
"""Function callable from the interactive Python console to exit that environment and return to cmd2."""
1562-
raise EmbeddedConsoleExit
1557+
if arg:
1558+
interp.runcode(arg)
1559+
else:
1560+
# noinspection PyShadowingBuiltins
1561+
def quit():
1562+
"""Function callable from the interactive Python console to exit that environment and return to cmd2."""
1563+
raise EmbeddedConsoleExit
15631564

1564-
self.pystate['quit'] = quit
1565-
self.pystate['exit'] = quit
1565+
self.pystate['quit'] = quit
1566+
self.pystate['exit'] = quit
15661567

1567-
keepstate = None
1568-
try:
1569-
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
1570-
keepstate = Statekeeper(sys, ('stdin', 'stdout'))
1571-
sys.stdout = self.stdout
1572-
sys.stdin = self.stdin
1573-
interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" %
1574-
(sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__))
1575-
except EmbeddedConsoleExit:
1576-
pass
1577-
if keepstate is not None:
1578-
keepstate.restore()
1579-
self._in_py = False
1568+
keepstate = None
1569+
try:
1570+
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
1571+
keepstate = Statekeeper(sys, ('stdin', 'stdout'))
1572+
sys.stdout = self.stdout
1573+
sys.stdin = self.stdin
1574+
interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" %
1575+
(sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__))
1576+
except EmbeddedConsoleExit:
1577+
pass
1578+
if keepstate is not None:
1579+
keepstate.restore()
1580+
except Exception:
1581+
pass
1582+
finally:
1583+
self._in_py = False
15801584
return self._should_quit
15811585

15821586
# Only include the do_ipy() method if IPython is available on the system

0 commit comments

Comments
 (0)