From 23c70f609d82ac280da282d5801e7acffc9b5db5 Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Thu, 28 Feb 2013 19:14:37 -0800 Subject: [PATCH] Add px command which uses exec() vs. py's eval() This is necessary since exec() isn't really a function, and eval can't evaluate statements. fixes #104 --- mininet/cli.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index efb1808..91f44e9 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -121,12 +121,12 @@ class CLI( Cmd ): "Run an external shell command" call( line, shell=True ) - # do_py() needs to catch any exception during eval() + # do_py() and do_px() need to catch any exception during eval()/exec() # pylint: disable-msg=W0703 def do_py( self, line ): """Evaluate a Python expression. - Node names may be used, e.g.: h1.cmd('ls')""" + Node names may be used, e.g.: py h1.cmd('ls')""" try: result = eval( line, globals(), self.locals ) if not result: @@ -138,7 +138,18 @@ class CLI( Cmd ): except Exception, e: output( str( e ) + '\n' ) - # pylint: enable-msg=W0703 + # We are in fact using the exec() pseudo-function + # pylint: disable-msg=W0122 + + def do_px( self, line ): + """Execute a Python statement. + Node names may be used, e.g.: px print h1.cmd('ls')""" + try: + exec( line, globals(), self.locals ) + except Exception, e: + output( str( e ) + '\n' ) + + # pylint: enable-msg=W0703,W0122 def do_pingall( self, _line ): "Ping between all hosts."