diff --git a/tests/test_interact.py b/tests/test_interact.py index 953c733..fc7c16d 100644 --- a/tests/test_interact.py +++ b/tests/test_interact.py @@ -33,6 +33,8 @@ import unittest from . import PexpectTestCase class InteractTestCase(PexpectTestCase.PexpectTestCase): + + @unittest.skipIf(not hasattr(wexpect.spawn, 'interact'), "spawn does not support runtime interact switching.") def test_interact(self): # Path of cmd executable: cmd_exe = 'cmd' @@ -54,7 +56,8 @@ class InteractTestCase(PexpectTestCase.PexpectTestCase): p.stop_interact() self.assertEqual('hello', p.before.splitlines()[1]) - + + @unittest.skipIf(not hasattr(wexpect.spawn, 'interact'), "spawn does not support runtime interact switching.") def test_interact_dead(self): # Path of cmd executable: echo = 'echo hello' diff --git a/wexpect/__init__.py b/wexpect/__init__.py index 27ffa56..1aa7e38 100644 --- a/wexpect/__init__.py +++ b/wexpect/__init__.py @@ -1,5 +1,7 @@ # __init__.py +import os + from .wexpect_util import split_command_line from .wexpect_util import join_args from .wexpect_util import ExceptionPexpect @@ -11,8 +13,18 @@ from .console_reader import ConsoleReaderPipe from .spawn import SpawnSocket from .spawn import SpawnPipe -from .spawn import SpawnSocket as spawn from .spawn import run +try: + spawn_class_name = os.environ['WEXPECT_SPAWN_CLASS'] + try: + spawn = globals()[spawn_class_name] + except KeyError: + print(f'Error: no spawn class: {spawn_class_name}') + print('Using SpawnSocket.') + spawn = SpawnSocket +except KeyError: + spawn = SpawnSocket + __all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT', 'ConsoleReaderSocket', 'ConsoleReaderPipe', 'spawn', 'SpawnSocket', 'SpawnPipe', 'run'] diff --git a/wexpect/spawn.py b/wexpect/spawn.py index 4b29eab..388621e 100644 --- a/wexpect/spawn.py +++ b/wexpect/spawn.py @@ -249,7 +249,7 @@ class SpawnBase: self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status. Time in seconds. self.buffer = '' # This is the read buffer. See maxread. self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched. - self.interact = interact + self.interact_state = interact # If command is an int type then it may represent a file descriptor. @@ -356,7 +356,15 @@ class SpawnBase: else: python_executable = os.path.join(os.path.dirname(sys.executable), 'python.exe') - child_class_initializator = self.get_child_class_initializator(args) + self.console_class_parameters.update({ + 'host_pid': self.host_pid, + 'local_echo': self.echo, + 'interact': self.interact_state + }) + console_class_parameters_kv_pairs = [f'{k}={v}' for k,v in self.console_class_parameters.items() ] + console_class_parameters_str = ', '.join(console_class_parameters_kv_pairs) + + child_class_initializator = f"wexpect.{self.console_class_name}(wexpect.join_args({args}), {console_class_parameters_str});" commandLine = '"%s" %s "%s"' % (python_executable, ' '.join(pyargs), @@ -817,6 +825,8 @@ class SpawnPipe(SpawnBase): def __init__(self, command, args=[], timeout=30, maxread=60000, searchwindowsize=None, logfile=None, cwd=None, env=None, codepage=None, echo=True, port=4321, host='localhost', interact=False): self.pipe = None + self.console_class_name = 'ConsoleReaderPipe' + self.console_class_parameters = {} super().__init__(command=command, args=args, timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact) @@ -922,9 +932,6 @@ class SpawnPipe(SpawnBase): raise return len(s) - def get_child_class_initializator(self, args, **kwargs): - child_class_initializator = f"wexpect.ConsoleReaderPipe(wexpect.join_args({args}), {self.host_pid}, local_echo={self.echo}, interact={self.interact});" - return child_class_initializator class SpawnSocket(SpawnBase): @@ -933,6 +940,8 @@ class SpawnSocket(SpawnBase): self.port = port self.host = host self.sock = None + self.console_class_name = 'ConsoleReaderPipe' + self.console_class_parameters = {'port': port} super().__init__(command=command, args=args, timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact) @@ -996,10 +1005,6 @@ class SpawnSocket(SpawnBase): return s.decode() - def get_child_class_initializator(self, args, **kwargs): - child_class_initializator = f"wexpect.ConsoleReaderSocket(wexpect.join_args({args}), {self.host_pid}, port={self.port}, local_echo={self.echo}, interact={self.interact});" - return child_class_initializator - class searcher_re (object): """This is regular expression string search helper for the