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:
@@ -24,6 +24,18 @@ def decode( s ):
|
|||||||
def encode( s ):
|
def encode( s ):
|
||||||
"Encode a byte string if needed for Python 3"
|
"Encode a byte string if needed for Python 3"
|
||||||
return s.encode( Encoding ) if Python3 else s
|
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
|
# Command execution support
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user