From 853815500d4fc20239d7a8ba65584c9a4e2f1463 Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Thu, 12 Jul 2018 03:05:39 +0000 Subject: [PATCH] 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. --- mininet/util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mininet/util.py b/mininet/util.py index fd70690..851df34 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -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