diff --git a/tests/test_constructor.py b/tests/test_constructor.py index 9ba69fd..ac1de8e 100644 --- a/tests/test_constructor.py +++ b/tests/test_constructor.py @@ -28,9 +28,9 @@ class TestCaseConstructor(PexpectTestCase.PexpectTestCase): the same results for different styles of invoking __init__(). This assumes that the root directory / is static during the test. ''' - p1 = wexpect.spawn('uname -m -n -p -r -s -v') + p1 = wexpect.spawn('uname -m -n -p -r -s -v', timeout=5, port=4321) p1.expect(wexpect.EOF) - p2 = wexpect.spawn('uname', ['-m', '-n', '-p', '-r', '-s', '-v']) + p2 = wexpect.spawn('uname', ['-m', '-n', '-p', '-r', '-s', '-v'], timeout=5, port=4322) p2.expect(wexpect.EOF) self.assertEqual(p1.before, p2.before) self.assertEqual(str(p1).splitlines()[1:9], str(p2).splitlines()[1:9]) diff --git a/wexpect/__init__.py b/wexpect/__init__.py index 9f62368..27ffa56 100644 --- a/wexpect/__init__.py +++ b/wexpect/__init__.py @@ -11,7 +11,7 @@ from .console_reader import ConsoleReaderPipe from .spawn import SpawnSocket from .spawn import SpawnPipe -from .spawn import SpawnPipe as spawn +from .spawn import SpawnSocket as spawn from .spawn import run __all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT', diff --git a/wexpect/console_reader.py b/wexpect/console_reader.py index 8261b50..c429133 100644 --- a/wexpect/console_reader.py +++ b/wexpect/console_reader.py @@ -157,14 +157,6 @@ class ConsoleReaderBase: time.sleep(.1) - self.terminate_child() - time.sleep(.1) - self.send_to_host(self.readConsoleToCursor()) - self.sendeof() - time.sleep(.1) - self.close_connection() - logger.info('Console finished.') - def read_loop(self): paused = False @@ -201,14 +193,10 @@ class ConsoleReaderBase: def terminate_child(self): try: - win32process.TerminateProcess(self.__childProcess, 0) - except pywintypes.error as e: - """ 'Access denied' happens always? Perhaps if not running as admin (or UAC - enabled under Vista/7). Don't log. Child process will exit regardless when - calling sys.exit - """ - # if e.args[0] != winerror.ERROR_ACCESS_DENIED: - logger.info(e) + if self.child_process: + self.child_process.kill() + except psutil.NoSuchProcess: + logger.info('The process has already died.') return def isalive(self, process): @@ -477,12 +465,13 @@ class ConsoleReaderSocket(ConsoleReaderBase): def send_to_host(self, msg): # convert to bytes - msg_bytes = str.encode(msg) - if msg_bytes: - logger.debug(f'Sending msg: {msg_bytes}') + if isinstance(msg, str): + msg = str.encode(msg) + if msg: + logger.debug(f'Sending msg: {msg}') else: - logger.spam(f'Sending msg: {msg_bytes}') - self.connection.sendall(msg_bytes) + logger.spam(f'Sending msg: {msg}') + self.connection.sendall(msg) def get_from_host(self): try: @@ -524,12 +513,13 @@ class ConsoleReaderPipe(ConsoleReaderBase): def send_to_host(self, msg): # convert to bytes - msg_bytes = str.encode(msg) - if msg_bytes: - logger.debug(f'Sending msg: {msg_bytes}') + if isinstance(msg, str): + msg = str.encode(msg) + if msg: + logger.debug(f'Sending msg: {msg}') else: - logger.spam(f'Sending msg: {msg_bytes}') - win32file.WriteFile(self.pipe, msg_bytes) + logger.spam(f'Sending msg: {msg}') + win32file.WriteFile(self.pipe, msg) def get_from_host(self): data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096) diff --git a/wexpect/spawn.py b/wexpect/spawn.py index 1ead324..4b29eab 100644 --- a/wexpect/spawn.py +++ b/wexpect/spawn.py @@ -291,12 +291,14 @@ class SpawnBase: garbage collects Python objects, not the child console.""" try: + logger.info('Deleting...') self.terminate() self.disconnect_from_child() if self.safe_exit: self.wait() except: traceback.print_exc() + logger.warning(traceback.format_exc()) def __str__(self): """This returns a human-readable string that represents the state of