Create util.pexpect which is compatible with Python 3 strings

Unfortunately pexpect isn't compatible with Python 3 strings
unless you specify unicode encoding, which isn't the default.

With this helper code, you can import pexpect from mininet.util
and get default pexpect.spawn() behavior that works with Python 3
strings.
This commit is contained in:
Bob Lantz
2018-07-25 19:44:44 -07:00
parent 1a2925923f
commit 853815500d
+12
View File
@@ -24,6 +24,18 @@ def decode( s ):
def encode( s ):
"Encode a byte string if needed for Python 3"
return s.encode( Encoding ) if Python3 else s
# Make pexpect compatible with Python 3 strings
try:
import pexpect as oldpexpect
pexpect, oldspawn = oldpexpect, oldpexpect.spawn
def spawn( self, *args, **kwargs):
"Let pexpect work with Python3 utf-8 strings"
if Python3:
kwargs.update( encoding='utf-8' )
return oldspawn( self, *args, **kwargs )
oldpexpect.spawn = spawn
except:
pass
# Command execution support