Change pexpect hackery to use a custom class

So the original pexpect is unperturbed
This commit is contained in:
Bob Lantz
2018-07-25 19:44:46 -07:00
parent 3a0ef258d1
commit 7b48460e9e
+12 -9
View File
@@ -16,7 +16,7 @@ import sys
# Python 2/3 compatibility # Python 2/3 compatibility
Python3 = sys.version_info[0] == 3 Python3 = sys.version_info[0] == 3
BaseString = str if Python3 else basestring BaseString = str if Python3 else str.__base__
Encoding = 'utf-8' if Python3 else None Encoding = 'utf-8' if Python3 else None
def decode( s ): def decode( s ):
"Decode a byte string if needed for Python 3" "Decode a byte string if needed for Python 3"
@@ -24,19 +24,22 @@ 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: try:
import pexpect as oldpexpect import pexpect as oldpexpect
pexpect, oldspawn = oldpexpect, oldpexpect.spawn class Pexpect( object ):
def spawn( self, *args, **kwargs): "Custom pexpect that is compatible with str"
"Let pexpect work with Python3 utf-8 strings" def spawn( self, *args, **kwargs):
if Python3: "pexpect.spawn that is compatible with str"
kwargs.update( encoding='utf-8' ) if Python3 and 'encoding' not in kwargs:
return oldspawn( self, *args, **kwargs ) kwargs.update( encoding='utf-8' )
oldpexpect.spawn = spawn return oldpexpect.spawn( *args, **kwargs )
def __getattr__( self, name ):
return getattr( oldpexpect, name )
pexpect = Pexpect()
except: except:
pass pass
# Command execution support # Command execution support
def run( cmd ): def run( cmd ):