From 7b48460e9e4f3ff54ac9165579b293d08989b409 Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Tue, 24 Jul 2018 22:58:44 -0700 Subject: [PATCH] Change pexpect hackery to use a custom class So the original pexpect is unperturbed --- mininet/util.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/mininet/util.py b/mininet/util.py index ae552f4..7397651 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -16,7 +16,7 @@ import sys # Python 2/3 compatibility 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 def decode( s ): "Decode a byte string if needed for Python 3" @@ -24,19 +24,22 @@ 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 + class Pexpect( object ): + "Custom pexpect that is compatible with str" + def spawn( self, *args, **kwargs): + "pexpect.spawn that is compatible with str" + if Python3 and 'encoding' not in kwargs: + kwargs.update( encoding='utf-8' ) + return oldpexpect.spawn( *args, **kwargs ) + def __getattr__( self, name ): + return getattr( oldpexpect, name ) + pexpect = Pexpect() except: pass + # Command execution support def run( cmd ):