Giter Site home page Giter Site logo

wexpect's Introduction

wexpect

Build status codecov Documentation Status

Wexpect is a Windows variant of pexpect.

Pexpect is a Python module for spawning child applications and controlling them automatically.

You need wexpect if...

  • you want to control any windows console application from python script.
  • you want to write test-automation script for a windows console application.
  • you want to automate your job by controlling multiple application parallel, synchronously.

Install

pip install wexpect

Usage

To interract with a child process use spawn method:

import wexpect

prompt = '[A-Z]\:.+>'

child = wexpect.spawn('cmd.exe')
child.expect(prompt)    # Wait for startup prompt

child.sendline('dir')   # List the current directory
child.expect(prompt)

print(child.before)     # Print the list
child.sendline('exit')

For more information see examples folder.


REFACTOR

Refactor has been finished!!! The default spawn class is SpawnPipe from now. For more information read history.


What is wexpect?

Wexpect is a Python module for spawning child applications and controlling them automatically. Wexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Wexpect is in the spirit of Don Libes' Expect, but Wexpect is pure Python. Other Expect-like modules for Python require TCL and Expect or require C extensions to be compiled. Wexpect does not use C, Expect, or TCL extensions.

Original Pexpect should work on any platform that supports the standard Python pty module. While Wexpect works on Windows platforms. The Wexpect interface focuses on ease of use so that simple tasks are easy.


Dev

Thanks for any contributing!

Test

To run test, enter into the folder of the wexpect's repo then:

python -m unittest

Deploy

The deployment itself is automated and done by appveyor. See after_test section in appveyor.yml for more details.

The wexpect uses pbr for managing releasing procedures. The versioning is handled by the pbr. The "master-version" is the git tag. Pbr derives the package version from the git tags.

wexpect's People

Contributors

anandphulwani avatar catslave007 avatar devjake avatar jeffwright13 avatar ka3bhy avatar raczben avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wexpect's Issues

Regex in expect fail in W10 and Python 3.9

Describe the bug
Regex is not working propertly when iterate lines from promt in Windows 10.

A command is sent to cmd, it is iterated, but never find EOF. Instead of this, the expect regex raises timeout.

After hour and hours spent trying to debug, i could not find the fix.

An example:

command = r"c:\mysql\bin\mysql_install_db.exe --service=comerzzia-mysql --password=admincmz --innodb_page_size=16K"
prompt = '[A-Za-z]\:.+>

child = wexpect.spawn('cmd.exe')
child.expect(prompt)

The output of this command is something like this:

c:\mysql\bin\mysql_install_db.exe --service=comerzzia-mysql --password=admincmz --innodb_page_size=16K
Default data directory is c:\mysql\data
Running bootstrap
2021-02-06 21:47:15 0 [Note] c:\mysql\bin\mysqld.exe (mysqld 10.4.12-MariaDB) starting as process 16012 ...
Removing default user
Setting root password
Creating my.ini file
Registering service 'comerzzia-mysql'
FATAL ERROR: CreateService failed (1073)
https://mariadb.com/kb/en/installation-issues-on-windows contains some help
for solving the most common problems.  If this doesn't help you, please
leave a comment in the Knowledge Base or file a bug report at
https://jira.mariadb.org

When the reader gets the last line, raises timeout, EOF is not being recogniced.

To try to fix, i've set delimiter to "prompt" value, but the regex seems to not being working well, because in the line that finds FATAL ERROR: it ends.

Instead of prompt, i've tried setting delimiter as > and it works, but i dont think its a good solution...

TIP
I'm having the same issue with pexpect library.

Environment:

  • Windows 10 20H2 64b
  • Python 3.9.1
  • wexpect 4.0.0

win32api does not contain module win32process

In version 224 of pywin32, the function TerminateProcess() is no longer located in
win32api.win32process
TerminateProcess() is now located under win32api and called like win32api.TerminateProcess()

spawn problems on Windows 10

On Windows 10, after calling spawn, nothing is received from spawned process except EOF.
Problem occurs from either a script or from shell commands.

To Reproduce enter in shell

import wexpect
child = wexpect.spawn('cmd.exe')
print(child)
_<wexpect.host.SpawnPipe object at 0x00ED83E8>
command: C:\WINDOWS\system32\cmd.exe
args: ['C:\\WINDOWS\\system32\\cmd.exe']
searcher: None
buffer (last 100 chars): 
before (last 100 chars): None
after: None
match: None
match_index: None
exitstatus: None
flag_eof: False
host_pid: 17168
child_fd: None
closed: False
timeout: 30
delimiter: <class 'wexpect.wexpect_util.EOF'>
maxread: 60000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.1
delayafterterminate: 2_
str = child.readline()
print(str)
child.expect('>')
_Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    child.expect('>')
  File "C:\Users\jweber\AppData\Local\Programs\Python\Python38-32\lib\site-packages\wexpect\host.py", line 732, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "C:\Users\jweber\AppData\Local\Programs\Python\Python38-32\lib\site-packages\wexpect\host.py", line 745, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "C:\Users\jweber\AppData\Local\Programs\Python\Python38-32\lib\site-packages\wexpect\host.py", line 806, in expect_loop
    raise EOF('EOF flag has been raised.')
wexpect.wexpect_util.EOF: EOF flag has been raised._

Environment:

  • Windows10 64-bit ( build17134.1425)
  • [Python 3.8.2]
  • [wexpect version]

sendline('') fails sometimes, but using interact and pressing enter works

My expected string is found (something like press enter to continue), but using sendline('') leaves the program hanging. Trying to use import pdb; pdb.set_trace() and manually re-sending the newline fails too (still hangs). If I use p.interact() instead of breaking with pdb.set_trace(), I get a new console/terminal popping up, showing the whole previous console output log, I see a lot of weird characters like ���������

If I press enter in the popped-up console, then my program continues running (but I don't know how to return control to Python).

How do I get sendline working properly?

(Sorry this is a work project which I can't post code of... I'll try to replicate with something I can share)

unclear how to stop/kill/terminate/close the shell

I see several functions in the source code, but the documentation/README nor examples show what is the proper way to terminate a shell and all it's subprocesses.

Please add this to the main README, etc...

pywintypes.error: (2, 'CreateFile', '系统找不到指定的文件')

Describe the bug

Traceback (most recent call last):
File "C:\Users\dream\PycharmProjects\pythonProject\venv\lib\site-packages\wexpect\host.py", line 878, in connect_to_child
self.pipe = win32file.CreateFile(
pywintypes.error: (2, 'CreateFile', '系统找不到指定的文件。')

To Reproduce
Steps to reproduce the behavior:

Expected behavior
Traceback (most recent call last):
File "C:\Users\dream\PycharmProjects\pythonProject\venv\lib\site-packages\wexpect\host.py", line 878, in connect_to_child
self.pipe = win32file.CreateFile(
pywintypes.error: (2, 'CreateFile', '系统找不到指定的文件。')

Screenshots
If applicable, add screenshots to help explain your problem.
图片

Environment:

  • [windows version] Windows 10 专业版(19042.508)
  • [Python version] 3.9.12
  • [wexpect version] 4.0.0

Additional context
Add any other context about the problem here.

Unable to catch Control-C while using wexpect

Describe the bug
The program using wexpect needs to do some final cleanup whenever the user chooses to end the program with Control-C. However, the Python code normally used for catching Control-C does not appear to work after wexpect is used.

To Reproduce
Steps to reproduce the behavior are as follows.
(1) Save the code below in a file called control_c.py
(2) Bring wexpect into the Python environment (pip install wexpect)
(3) Run the code using "python control_c.py"
(4) Wait for the program to output "Starting", then enter Control-C within 5 seconds. This will show the program responding to Control-C in the desired manner,
(4) Run the code again using "python control_c.py"
(5) This time, allow the program to run more than 5 seconds, until the output from a 'ping' command appears, then enter Control-C within 5 seconds. This will show the program ending abruptly, without executing the code associated with catching Control-C.

import signal       # Control-C handling
import sys          # Control-C handling
import time         # Delay timing

import wexpect

# Control-C handler
def signal_handler(sig, frame):
    print("\n---- Stopped with Control-C ----")
    sys.exit(0)

# Initialize Control-C catching
signal.signal(signal.SIGINT, signal_handler)

# main()
def main():
    print('Starting')
    time.sleep(5)       # Try Control-C here

    output, status = wexpect.run("ping localhost -n 1", \
                                 withexitstatus = True)
    print('ouput =', output, '\nstatus =', status)

    time.sleep(5)       # Try Control-C here
    print('Ending')

if __name__ == "__main__":
    main()
else:
    pass

Expected behavior
There is a need to be able to catch Control-C in a Python program while using wexpect, because there is no other way to make sure some final cleanup code is executed before the program ends.

Screenshots
None. The code provided above should demonstrate the issue.

Environment:

  • Windows 10 Pro Version 1903
  • Python 3.6.1
  • wexpect==2.3.9

Additional context
I'm hoping there's an easy fix or a reasonable work-around for this issue. (And if I'm doing something wrong, then I wouldn't mind at all if you can tell me what that is!)

ReadConsoleOutputCharacter got float - crashes

Describe the bug
after or while running certain commands, the child object dies with this error... in this case, an expect had just finished, and I was about to run another sendline but it was hanging... when I tried debugging with import pdb; pdb.set_trace() and then printing the c.before I got the crash too:

(Pdb) c.before
Traceback (most recent call last):
  File "C:\Users\myusername\git\test\wexpect.py", line 2304, in read_nonblocking
    s = self.readConsoleToCursor()
  File "C:\Users\myusername\git\test\wexpect.py", line 2234, in readConsoleToCursor
    raw = self.readConsole(self.__currentReadCo, cursorPos)
  File "C:\Users\myusername\git\test\wexpect.py", line 2177, in readConsole
    s = self.__consout.ReadConsoleOutputCharacter(readlen, startCo)
TypeError: integer argument expected, got float

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\myusername\git\test\wexpect.py", line 1433, in expect_loop
    c = self.read_nonblocking(self.maxread, timeout)
  File "C:\Users\myusername\git\test\wexpect.py", line 1776, in read_nonblocking
    s = self.wtty.read_nonblocking(timeout, size)
  File "C:\Users\myusername\git\test\wexpect.py", line 2325, in read_nonblocking
    raise EOF('End Of File (EOF) in Wtty.read_nonblocking().')
wexpect.EOF: End Of File (EOF) in Wtty.read_nonblocking().

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dflauncher.py", line 197, in <module>
    automate_script()
  File "dflauncher.py", line 155, in automate_script
    c.sendline('some_command')
  File "C:\Users\myusername\git\test\wexpect.py", line 1366, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "C:\Users\myusername\git\test\wexpect.py", line 1380, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "C:\Users\myusername\git\test\wexpect.py", line 1451, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
wexpect.EOF: End Of File (EOF) in Wtty.read_nonblocking().
<wexpect.spawn_windows object at 0x000002328FA14908>
version: 0.0.1.unkown0 ($Revision: 399 $)
command: C:\windows\system32\cmd.exe
args: ['C:\\windows\\system32\\cmd.exe']
searcher: searcher_re:
    0: re.compile("123123expect123123")
buffer (last 100 chars):
before (last 100 chars):  yes

after: <class 'wexpect.EOF'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 12760
child_fd: None
closed: False
timeout: 30
delimiter: <class 'wexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

To Reproduce
lots of long-running commands where I have to watch for timeout and continue

c.sendline('some_command_that_might_do_svn_checkout repo_URL')
expect_msg = [TIMEOUT, 'Press ENTER to continue', 'Run SVN Update\?\(\[yes\]/no\)']
i=0
while i!=3:
    i = c.expect(expect_msg)
    print('expect I is : {}'.format(i))
    if i == 1:
        c.sendline('')
    elif i == 2:
        c.sendline(' yes') #note the leading space is required!!!
        expect_msg.append(unique_prompt)

Expected behavior
doesn't crash

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

  • [windows version] 10
  • [Python version] 3.6.8
  • [wexpect version] latest from PyPi source-download:
    __version__ = '0.0.1.unkown0'
__revision__ = '$Revision: 399 $'

AttributeError: 'Wtty' object has no attribute '_Wtty__consout'

Describe the bug

Reported by Shay Ceasary.

>>> import wexpect
>>> child = wexpect.spawn('cmd.exe')
>>> child.expect('>')

Traceback (most recent call last):
  File "C:\Users\shayc\PycharmProjects\Training\venv\lib\site-packages\wexpect.py", line 2323, in read_nonblocking
    s = self.readConsoleToCursor()
  File "C:\Users\shayc\PycharmProjects\Training\venv\lib\site-packages\wexpect.py", line 2229, in readConsoleToCursor
    if not self.__consout:

AttributeError: 'Wtty' object has no attribute '_Wtty__consout'

To Reproduce

import wexpect
child = wexpect.spawn('cmd.exe')
child.expect('>')

Environment:

  • Microsoft Windows 10 Pro (10.0.17134 Build 17134)
  • Python 3.7
  • wexpect version ???

Seeing weird character in interact mode

I am trying to automate ssh terminal operations. Currently facing couple of issues

  1. Weird character appear in console filled with ���������. I am not able to figure out what is actually present in current screen since the complete screen is filled with these characters . Is there a away to avoid these characters getting displayed
  2. neither expect method nor the realine()/readlines() read the complete console. I tried setting the window size (400,400) it did not make any difference. Also tried increasing the maxread property, did not help either
    Please provide some suggestions to resolve these. Thanks in Advance!!

The logger writes everything twice into the logfile.

Describe the bug
The logger writes everything twice into the logfile.

Snippet:

2020-02-23 00:00:46,657 - __main__.py:39 - INFO - Starter arguments: Namespace(buffer_size_x=80, buffer_size_y=16000, codepage=None, command=['C:\\Program Files\\Git\\usr\\bin\\cat.EXE'], console_reader_class='ConsoleReaderPipe', host_pid=19440, interact='False', local_echo='True', window_size_x=80, window_size_y=25)
2020-02-23 00:00:46,657 - __main__.py:39 - INFO - Starter arguments: Namespace(buffer_size_x=80, buffer_size_y=16000, codepage=None, command=['C:\\Program Files\\Git\\usr\\bin\\cat.EXE'], console_reader_class='ConsoleReaderPipe', host_pid=19440, interact='False', local_echo='True', window_size_x=80, window_size_y=25)
2020-02-23 00:00:46,657 - console_reader.py:110 - INFO - ConsoleReader started. location D:\betonra\wexpect-tox\dist\wexpect\wexpect\console_reader.pyc
2020-02-23 00:00:46,657 - console_reader.py:110 - INFO - ConsoleReader started. location D:\betonra\wexpect-tox\dist\wexpect\wexpect\console_reader.pyc
2020-02-23 00:00:46,657 - console_reader.py:116 - INFO - Setting console output code page to 1250
2020-02-23 00:00:46,657 - console_reader.py:116 - INFO - Setting console output code page to 1250
2020-02-23 00:00:46,657 - console_reader.py:119 - INFO - Console output code page: 1250
2020-02-23 00:00:46,657 - console_reader.py:119 - INFO - Console output code page: 1250

To Reproduce
Switch on logger with set WEXPECT_LOGGER_LEVEL=INFO and run anything.

Expected behavior
Write each log-item once.

Environment:

  • windows 10.0.18362 Build 18362
  • Python 3.7.5
  • wexpect 22b2e2e

Confused `before` string

Describe the bug
Sometimes the before string is confused. Some characters missing.

To Reproduce

import wexpect
import time

print(wexpect.__version__)


def testPath():
    # Path of cmd executable:
    cmdPath = 'cmd'
    cmdPrompt = '>'
    referenceOut = None

    while True:
        # Start the child process
        p = wexpect.spawn(cmdPath)

        # Wait for prompt
        p.expect(cmdPrompt)

        # Send a command
        p.sendline('ls')
        # time.sleep(0.6) <= this sleep solve...
        p.expect(cmdPrompt)
        
        print(cmdPath + " >>" + p.before + '<<')
        if referenceOut:
            if referenceOut != p.before:
                p.interact()
                time.sleep(5)
                raise Exception()
        else:
            referenceOut = p.before
        
testPath()

Expected behavior
Script above should never stop.

Environment:

  • OS Name Microsoft Windows 10 Pro
  • Version 10.0.18362 Build 18362
  • Python 3.7.4
  • wexpect 2.3.6

Additional context

  • The interact() console shows correct behavior.
  • Only cmd.exe can reproduce this issue for now. (Any python simulation works correctly)
  • sleep before the p.expect(cmdPrompt) solve.

Critical Performance Issue

Describe the bug
if I don't interact with the process, expect() takes increasing time to return.
what is most strange is that elapsed time will slowly approach the time between two sendline (inactivity time).
More time passes between two sendline, more times it takes for returning after I execute a new sendline().

To Reproduce

import wexpect
import time

child = wexpect.spawn('cmd.exe')
child.expect('>')
for _ in range(10):
    child.sendline('echo "hello"')
    starting_time = time.time()
    child.expect('>')
    print("Expect Elapsed time:",time.time() -starting_time)
    time.sleep(5) #if you execute sleep(10) expect will soon take 10 seconds instead of 5
child.sendline('exit')
child.wait()

Expected behavior
"Expect elapsed time" shouldn't depend on how much time I didn't iteracted with the process.

Output
Expect Elapsed time: 0.07507729530334473
Expect Elapsed time: 2.6089940071105957
Expect Elapsed time: 3.860004425048828
Expect Elapsed time: 4.498199224472046
Expect Elapsed time: 4.816983938217163
Expect Elapsed time: 4.947844982147217
Expect Elapsed time: 5.021565198898315
Expect Elapsed time: 5.082917213439941
Expect Elapsed time: 5.092726707458496
Expect Elapsed time: 5.106329441070557

Environment:

  • [windows version] 21H1 - Build 19043.1348
  • [Python version] 3.85
  • [wexpect version] 4.0.0

Multiple expect pattern fails in race conditions

Describe the bug
"Greed tests" fails

To Reproduce

import wexpect

p = wexpect.spawn('python list100.py', maxread=1)
assert p.expect(['1, 2, 3', '2,']), 1

The list100.py can be found in tests directory.

Expected behavior
To pass the assertion.

Environment:

  • OS Name Microsoft Windows 10 Home
  • Version 10.0.18362 Build 18362
  • Python 3.7.5
  • wexpect v2.3.9

Sometimes tests.test_expect.ExpectTestCase.test_expect_order_exact test fails

Describe the bug

Sometimes tests.test_expect.ExpectTestCase.test_expect_order_exact test fails using legacy_wexpect (I have seen with new structure too, but rarely)
See build 1.0.201 and 1.0.203, which builds the same commit, however, the first fails, while the second passes.

To Reproduce
Run unittest tests.test_expect.ExpectTestCase.test_expect_order_exact several times, and it will fails sometimes.

Expected behavior
Never fail.

Environment:

  • windows version: see Appveyor
  • Python version: all the same (see Appveyor)
  • wexpect version >3.0

PyInstaller and Wexpect

Describe the bug
The problem I am facing is that whenever I convert my app using PyInstaller to an EXE. I get the following error.

Traceback (most recent call last):
     File "site-packages\wexpect.py", line 307, in spawn
     File "site-packages\wexpect.py", line 1648, in __init__
     File "site-packages\wexpect.py", line 480, in __init__
     File "site-packages\wexpect.py", line 1709, in _spawn
     File "site-packages\wexpect.py", line 1939, in spawn
     File "site-packages\wexpect.py", line 2028, in startChild
pywintypes.error (2, 'CreateProcess', "The system cannot find the file specified.')

To Reproduce
Steps to reproduce the behavior:

1- Create a new script.
2- Import wexpect.
3- Spawn cmd or any application in that script.
4- from the command line run "pyinstaller scriptname.py" to convert it to exe.
5- Finally, run the executable.

Expected behavior
Spawn should run without errors.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

  • [windows version] Windows 10
  • [Python version] 3.7.4
  • [wexpect version] 2.3.6
  • [pyinstaller version] 4.0.dev

Additional context
I understand that the problem here is with the variable 'commandLine'. I noticed in the code that it already check for py2exe frozen attribute.

All the best,
A

logfile parameter not working in version 4.0.0

Describe the bug
It seems that logfile parameter in wexpect.host.SpawnPipe does not work

To Reproduce

import wexpect
f = open("test.txt","w")
child = wexpect.spawn("cmd.exe",encoding="UTF-8",logfile=f)
child.expect(">")
child.send("dir")
child.sendeof()
f.close()

Expected behavior
command output should be written to logfile
open the file after running script only to get a blank logfile

Environment:

  • Windows 10
  • Python 3.10
  • wexpect 4.0.0

setecho doesn't work

Describe the bug
setecho() has no effect at all.

To Reproduce

import wexpect
import time
import os

print(wexpect.__version__)

def test_setecho():
    # Path of cmd executable:
    cmdPath = 'cmd'
    cmdPrompt = '>'
    referenceOut = None
    
    # Start the child process
    p = wexpect.spawn(cmdPath)
    p.expect(cmdPrompt)
    
    p.setecho(0)                # SETECHO
    
    p.interact()
    for c in 'echo Hello':
        p.send(c)
        time.sleep(0.2)
    p.send(os.linesep)
    
    time.sleep(2)
    p.stop_interact()
    p.sendline('exit')
    
    
    # Start the child process
    p = wexpect.spawn(cmdPath)
    p.expect(cmdPrompt)
    
    p.setecho(1)                # SETECHO
    
    p.interact()
    for c in 'echo Hello':
        p.send(c)
        time.sleep(0.2)
    p.send(os.linesep)
    
    time.sleep(2)
    p.stop_interact()
    p.sendline('exit')
    
    
test_setecho()    

Expected behavior
The console should echo the sent characters in setecho(1) case.

Environment:

  • OS Name Microsoft Windows 10 Pro
  • Version 10.0.18363 Build 18363
  • Python 3.7.4
  • wexpect 2.3.9

Cannot be used with pytest

Describe the bug
Wexpect cannot be used in project which uses pytest

To Reproduce

import wexpect

def test_wexpect():
    executable = 'tclsh'
    prompt = '% '

    child_proc = wexpect.spawn(executable)
    child_proc.expect(prompt, timeout=3)

    child_proc.terminate()

Run this script in pytest.

Expected behavior
Pytest should print the results. Wexpect eat the console.

Environment:

  • OS Name Microsoft Windows 10 Pro
  • Version 10.0.18363 Build 18363
  • Python 3.7.4
  • 3.0.0

wexpect throw UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 2999: unexpected end of data

Describe the bug
when i use " ret = EnvManage.child_ex.expect([patter, wexpect.TIMEOUT, wexpect.EOF], timeout=time)" to expect something。 sometimes it throw UnicodeDecodeError.

File "D:\SoftWare\WorkSpace\GitCode\AutoTest\framework\baseLib\checkLib\baseExpect.py", line 20, in check_expect
ret = EnvManage.child_ex.expect([potter, wexpect.TIMEOUT, wexpect.EOF], timeout=time)
File "D:\SoftWare\IDE\Python\Python3.7.9\lib\site-packages\wexpect\host.py", line 732, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "D:\SoftWare\IDE\Python\Python3.7.9\lib\site-packages\wexpect\host.py", line 745, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
File "D:\SoftWare\IDE\Python\Python3.7.9\lib\site-packages\wexpect\host.py", line 812, in expect_loop
c = self.read_nonblocking(self.maxread)
File "D:\SoftWare\IDE\Python\Python3.7.9\lib\site-packages\wexpect\host.py", line 930, in read_nonblocking
return s.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 2999: unexpected end of data

Expected behavior
my patter is regular expression : "LogicSectionId" + str(logic_id) + "=([0-9 ]+)"

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

  • [windows version]: windows 10 2004H
  • [Python version]: Python 3.7.9

wexpect can't find tool command, if the env is passed as argument

Problem/Request
The feature request I want to make is due to a problem I get, when I use wexpect.spawn and give this method the prepared environment variables with the env option. I would expect that spawn will use the PATH variable which is passed as env to it. (I'm using wexpect 3.3.0 with Python 3.6.5)

env = os.environ.copy()
env['PATH'] += f';{os.path.dirname(self.pcfg.vivado_config.vivado)}'
os.environ['WEXPECT_SPAWN_CLASS'] = 'SpawnPipe'
from wexpect import spawn
self.proc = spawn(command=cmd, cwd=self.cwd, env=env)

Unfortunately it just ignores it, when it comes to the shutil.which() routine, e.g. inside the host.py.

I would like to have that the spawn method is evaluating the environment's PATH value instead only using the PATH from system variables.

So the shutil.which command should be extended with the path argument from self.env if it is set.

if self.env:
    command_with_path = shutil.which(self.command, path=self.env['PATH'])
else:
    command_with_path = shutil.which(self.command)

Benefits
If you pass the env to the spawn method, you don't have to manipulate the PATH variable. Otherwise I have to manipulate the path variable which could lead to other problems.
This would be a clean solution.

The exit status of a killed process should be `None`

Describe the bug
The exit status of a killed process should be None

To Reproduce

import wexpect
import time

def test_expect_wait_after_termination():
    """Ensure wait on a process terminated by kill -9.
    based on IsAliveTestCase::test_expect_wait_after_termination()"""
    
    p = wexpect.spawn('sleep 3')
    assert p.isalive()
    p.kill(9)
    time.sleep(1)

    # when terminated, the exitstatus is None, but p.signalstatus
    # and p.terminated reflects that the kill -9 nature.
    exitstat = p.wait()
    print('exitstat: {}'.format(exitstat))
    assert exitstat is None
        
test_expect_wait_after_termination()

Expected behavior
The exit status of a killed process should be None

Environment:

  • OS Name Microsoft Windows 10 Home
  • Version 10.0.18362 Build 18362
  • Python 3.7.5
  • wexpect v2.3.9

An environment variable becomes too big and the program fails completely

Describe the bug
If a Windows Python program calls 'spawn' or 'run' too many times (with WEXPECT_SPAWN_CLASS=SpawnPipe), the program halts with an error on the console that says 'the environment variable is longer than 32767 characters'.

To Reproduce
Steps to reproduce the behavior are as follows.
(1) Save the code below in a file called run_check.py
(2) Bring wexpect into the Python environment (pip install wexpect)
(3) Set the WEXPECT_SPAWN_CLASS environment variable (set WEXPECT_SPAWN_CLASS=SpawnPipe)
(4) Run the code using "python run_check.py"
(5) Wait for the program to end or fail. This will likely take a few minutes, so the code includes a simple progress indicator that lets you know it is still running.

import wexpect

def main():
    print('---- run_check.py ----')
    for loop in range(1, 1000):
        try:
            x = wexpect.run('tasklist')
        except Exception as error:
            print('\n---- Unable to run command ----')
            print('loop =', loop, '\nerror =', error)
            exit(1)
        print('.', sep='', end='', flush=True)  # Progress indicator

if __name__ == "__main__":
    main()
else:
    pass

Expected behavior
The program should print 1,000 dot characters and then stop if the issue does not occur. If the program encounters the issue, it will print the loop number and the error.

Screenshots
None. The code provided above should demonstrate the issue.

Environment:

  • Windows 10 Pro Version 1903
  • Python 3.8.1
  • wexpect==3.3.0

Additional context
When I run the program with the environment variable WEXPECT_SPAWN_CLASS not set (legacy wexpect), it completes without any error. (I can't use wexpect this way because I need to be able to catch Control-C, and that means I need to set WEXPECT_SPAWN_CLASS=SpawnPipe.)

When I run the program with WEXPECT_SPAWN_CLASS=SpawnPipe, it fails after calling 'run' roughly 650 times, and the error says: "the environment variable is longer than 32767 characters".

I've seen these same results on two different Windows systems. On one system, 'loop' reaches 656 before the program fails, and on the other system 'loop' reaches 643 before the failure occurs.

Note: The program given above only calls 'run', but I've gotten exactly the same results with a longer program that uses 'spawn'/'terminate' instead of 'run'. (I thought it would be better to use the shorter program for this Issue. :-)

I'm guessing, but it's like there's an environment variable that grows by ~50 bytes every time 'run' or 'spawn' is called, and when the environment variable becomes larger than 2^15, the program stops with an error.

Asyncio support

The Pexpect library supports passing an async_ argument that runs the command in async mode, compatible with asyncio. This seems like a great option for an expect type system as it inherently has a lot of waiting. Would it be possible to implement this?

sendline error in Spyder3 IDE

Issue: Wexpect's sendline method fails calling from Spyder3 IDE.

Reproduce: Run cmd.py example code from exampes folder from Spyder.

runfile('D:/bt/wexpect/examples/cmd.py', wdir='D:/bt/wexpect/examples')
Microsoft Windows [Version 10.0.17763.437]
(c) 2018 Microsoft Corporation. All rights reserved.

D:\bt\wexpect\examples>[PyINPUT_RECORD(EventType=1) (KEY_EVENT), PyINPUT_RECORD(EventType=1) (KEY_EVENT)]
{'Size': PyCOORDType(X=80,Y=16000), 'CursorPosition': PyCOORDType(X=23,Y=3), 'Attributes': 7, 'Window': PySMALL_RECTType(Left=0,Top=5,Right=79,Bottom=29), 'MaximumWindowSize': PyCOORDType(X=80,Y=64)}
Traceback (most recent call last):

  File "<ipython-input-1-5e47c42ff79e>", line 1, in <module>
    runfile('D:/bt/wexpect/examples/cmd.py', wdir='D:/bt/wexpect/examples')

  File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/bt/wexpect/examples/cmd.py", line 30, in <module>
    p.sendline('ls')

  File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect.py", line 997, in sendline
    n = self.send(s)

  File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect.py", line 1824, in send
    c = self.wtty.write(s)

  File "C:\Users\usr\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect.py", line 2140, in write
    wrote = self.__consin.WriteConsoleInput(records)

error: (6, 'WriteConsoleInput', 'The handle is invalid.')

always get Error: no spawn class: {spawn_class_name} when import wexpect

Describe the bug
always get Error: no spawn class: {spawn_class_name} when import wexpect

To Reproduce

    Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import wexpect
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\site-packages\wexpect\__init__.py", line 48
        print(f'Error: no spawn class: {spawn_class_name}')
                                                         ^
    SyntaxError: invalid syntax
    >>>

Expected behavior
import wexpect without error

Environment:

  • [windows 10]
  • [Python 2.7.15]
  • [wexpect 4.0.0]

Windows 10 wexpect.spawn('telnet') doesn't seem to work

Describe the bug
When I run a telnet client session through wexpect.spawm API, it gives timeout and doesn't match the prompt pattern that I provided. I am using Python 3.7.4.

My sample code:

def wexpect_telnet():
    print("In wexpect_telnet")
    child = wexpect.spawn('telnet')
    child.expect("Microsoft Telnet> ")

I got following error.

C:\PythonWork\Sandbox1>python Keywords.py
In wexpect_telnet
Traceback (most recent call last):
  File "C:\Users\bwn6286\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect\host.py", line 809, in expect_loop
    raise TIMEOUT('Timeout exceeded in expect_any().')
wexpect.wexpect_util.TIMEOUT: Timeout exceeded in expect_any().

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Keywords.py", line 52, in <module>
    wexpect_telnet()
  File "Keywords.py", line 36, in wexpect_telnet
    child.expect("Microsoft Telnet> ")
  File "C:\Users\bwn6286\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect\host.py", line 732, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "C:\Users\bwn6286\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect\host.py", line 745, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "C:\Users\bwn6286\AppData\Local\Programs\Python\Python37\lib\site-packages\wexpect\host.py", line 843, in expect_loop
    raise TIMEOUT(f'{e}\n{self}')
wexpect.wexpect_util.TIMEOUT: Timeout exceeded in expect_any().
<wexpect.host.SpawnPipe object at 0x0000019DEB6E7E08>
command: C:\WINDOWS\system32\telnet.EXE
args: ['C:\\WINDOWS\\system32\\telnet.EXE']
searcher: searcher_re:
    0: re.compile("Microsoft Telnet> ")
buffer (last 100 chars):
before (last 100 chars):
after: <class 'wexpect.wexpect_util.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
host_pid: 4540
child_fd: None
closed: False
timeout: 30
delimiter: <class 'wexpect.wexpect_util.EOF'>
maxread: 60000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.1
delayafterterminate: 2

C:\Users\bwn6286\PythonWork\Sandbox1> 

To Reproduce
Steps to reproduce the behavior:

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

  • [windows version]
  • [Python version]
  • [wexpect version]

Additional context
Add any other context about the problem here.

How to replicate Fn+p key

I am trying to automate ssh terminal operations. I am trying to replicate a Fn+P key on the ssh console using child.send()

The terminal I use has a macro recording option, from that I was able to capture the character it sends which is chr(27) + "~[29~~".
I tried to use the same in child.send(chr(27) + "~[29~~") in wexpect, the console throws a message saying unmapped key
I do not understand what "~[29~~" means.

switchBack with logger failes during reading from a dead process

Description
Logger prints into child's console (while it should print only into file), in the following case:

  • The first process dies before we read from it
  • The second process logger prints into the child's console.

To Reproduce

import wexpect
import time

def switch_back_error ():
    p2 = wexpect.spawn('echo blabla')
    
    # This is an essential wait:
    time.sleep(1)
    p2.expect(wexpect.EOF)
    
    
    # Path of cmd executable:
    cmd_exe = 'cmd'
    cmdPrompt = '>'
    
    # Start the child process
    p = wexpect.spawn(cmd_exe)

    # Wait for prompt
    p.expect(cmdPrompt)

    # Send a command
    p.sendline('echo hello')
    p.expect(cmdPrompt)
    
    p.interact()
    time.sleep(2)
        
    if 'hello' != p.before.splitlines()[1]:
        raise Exception("'hello' != p.before.splitlines()[1]")

if __name__ == '__main__':
    switch_back_error()

Expected behavior
Script above should not raise any exception.

Environment:

  • OS Name: Microsoft Windows 10 Pro
  • Version: 10.0.18362 Build 18362
  • Python 3.7.4
  • 3484a98

Additional context
Add logging statement between switchTo() and switchBack()

Cannot catch `before` if program exits immediately

Issue: wexpect cannot wait on EOF properly. The before field wont contain the program's output if it has no prompt.

Reproduce:

import wexpect
import time
p = wexpect.spawn('uname')
time.sleep(1)
p.expect(wexpect.EOF)
p.before

Is there any child process console output limit?

somewhere during processing, I think around about 20000 lines of child output (hard to tell since I have expect-loop print statements interleaved in with my child output logfile), I'm getting a freeze in processing. Basically I'm running the same set of commands and expectations, but in different Python virtualenvs which the spawned shell is starting. So the same set of commands works in about 8 environments, and then freezes in the 9th. The specific command running when it gets stuck is a pip install --editable git+https://gitlab.devserver.myhost.com/my_project/my_package.git#egg=my_package.
I'm not sure if it's related, but I see this output a lot beforehand:

  Running command git fetch -q --tags
  bash: /dev/tty: No such device or address
install_pip_package expectation I==6 (child is asking for username)
   error: failed to execute prompt script (exit code 1)
my_username
  bash: /dev/tty: No such device or address
  error: failed to execute prompt script (exit code 1)
  stty: 'standard input': Inappropriate ioctl for device

where install_pip_package expectation is printed from my expect loop, showing the expectation list index, and my_username is the sendline response to the child.

So I'm wondering if I'm hitting some output buffer limit, etc...

spawn hangs when run in virtual environment

Describe the bug

When run in cmd window, the spawn command works fine, but if in a virtual environment, it hangs forever.

To Reproduce

This works

C:\Users\csullivan\Documents\Projects\netlog>python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from wexpect import spawn
>>> spawn("cmd.exe")
<wexpect.host.SpawnPipe object at 0x0000019F7040DBB0>
>>>

but this does not return from spawn.

(env) C:\Users\csullivan\Documents\Projects\netlog>python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from wexpect import spawn
>>> spawn("cmd.exe")

Expected behavior
I would expect identical behaviour in a Python Virtual Environment if software versions are the same, which they are.

Environment:
Microsoft Windows 10 Pro 10.0.19043 Build 19043
Dell Latitude 7490
Python 3.9.5
wexpect 4.0.0

Additional context
Submitted this as a question to Stack Overflow.

wexpect fails in venv

Describe the bug
Wexpect cannot run inside venv. (Note wexpect works inside virtualenv)

To Reproduce
Create...:
python -m venv myvenv

...and activate venv
myvenv\Scripts\activate

Install wexpect:
python -m pip install wexpect

Run any of from wexpect examples:
python examples\hello_wexpect.py

Expected behavior
Not to crash with EOF

Environment:

  • Win10.0.18363 Build 18363
  • Python 3.7.4
  • wexpect version: Doesnt care, wexpect==2.3.6, wexpect==3.1.0

new method failing to work (ENV var)

Here's the setup

from __future__ import unicode_literals

import os
import sys
import time
import json
import shutil
import getpass
import argparse
import subprocess
from ast import literal_eval
from datetime import datetime
import collections
import re

os.environ['WEXPECT_SPAWN_CLASS'] = 'SpawnSocket'
from wexpect import spawn
from wexpect import TIMEOUT, EOF

class stream_splitter(object):
    def __init__(self):
        self.log = open('debug_log.log', 'wb')
        self.last_string_buffer = collections.deque(maxlen=2)

    def multiple_wheel_building_lines(self, string):
        self.last_string_buffer.append(string.strip())
        if all([s.startswith('Building wheel for') for s in self.last_string_buffer]):
            return True
        return False

    def force_decode(self, string, codecs=['cp1252', 'utf8', 'utf-8']):
        if isinstance(string, str):
            return string.encode('utf8')
        for i in codecs:
            try:
                return string.decode(i, errors='replace').encode('utf8')
            except UnicodeDecodeError:
                pass

    def write(self, val):
        if not val:
            return
        val = self.force_decode(val)
        string_val = val.decode('utf8')
        if self.multiple_wheel_building_lines(string_val):
            return
        print(string_val)
        self.log.write(val)

    def read(self):
        pass

    def flush(self):
        self.log.flush()


DEBUG=True
if DEBUG:
    log = stream_splitter()


# later on in the file
shell = spawn('cmd.exe')
time.sleep(1)
shell.sendline('PROMPT {}'.format(unique_prompt))
shell.expect(unique_prompt)
print('*** got cmd prompt')

if DEBUG:
    shell.logfile_read = log

# do more work, running pip installs in the spawned shell, running git clone, running Python based unit-tests of my packages

and here's the error:

  File "my_script.py", line 453, in start_automation
    shell = spawn('cmd.exe')
  File "C:\misc\my_script_dir\wexpect\host.py", line 1030, in __init__
    interact=interact)
  File "C:\misc\my_script_dir\wexpect\host.py", line 305, in __init__
    self.connect_to_child()
  File "C:\misc\my_script_dir\wexpect\host.py", line 1047, in connect_to_child
    self.sock.connect((self.host, self.port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Traceback (most recent call last):
  File "C:\misc\my_script_dir\wexpect\host.py", line 314, in __del__
    self.terminate()
  File "C:\misc\my_script_dir\wexpect\host.py", line 453, in terminate
    self.kill()
  File "C:\misc\my_script_dir\wexpect\host.py", line 1096, in kill
    self.send(SIGNAL_CHARS[sig])
  File "C:\misc\my_script_dir\wexpect\host.py", line 624, in send
    return self._send_impl(s)
  File "C:\misc\my_script_dir\wexpect\host.py", line 1042, in _send_impl
    self.sock.sendall(s)
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

If I change the ENV var to os.environ['WEXPECT_SPAWN_CLASS'] = 'SpawnPipe'

then my logging from the spawned shell doesn't print with the previously working code in the "old" method (using the stream_splitter "object with a write method").

Internal structure is appended the beginning of the `before`

Report a bug

Some unwanted data is appended the beginning of the before field.

Reproduce

Run the hello_wexpect examlpe
python examples\hello_wexpect.py

Output

D:\bt\wexpect
yINPUT_RECORD(EventType=1) (KEY_EVENT), PyINPUT_RECORD(EventType=1) (KEY_EVENT)]
{'Size': PyCOORDType(X=80,Y=16000), 'CursorPosition': PyCOORDType(X=14,Y=3), 'Attributes': 7, 'Window': PySMALL_RECTType(Left=0,Top=0,Right=79,Bottom=24), 'MaximumWindowSize': PyCOORDType(X=80,Y=64)}
lsPyINPUT_RECORD(EventType=1) (KEY_EVENT)]
{'Size': PyCOORDType(X=80,Y=16000), 'CursorPosition': PyCOORDType(X=2,Y=8), 'Attributes': 7, 'Window': PySMALL_RECTType(Left=0,Top=0,Right=79,Bottom=24), 'MaximumWindowSize': PyCOORDType(X=80,Y=64)}

__pycache__  coverage.xml  LICENSE     setup.cfgwexpect.egg-info
AUTHORS      examples   README.md     setup.pywexpect.py
ChangeLog    htmlcov   requirements.txt  testswexpect.pyc

D:\bt\wexpect

Expected

The following is unwanted

yINPUT_RECORD(EventType=1) (KEY_EVENT), PyINPUT_RECORD(EventType=1) (KEY_EVENT)]
{'Size': PyCOORDType(X=80,Y=16000), 'CursorPosition': PyCOORDType(X=14,Y=3), 'Attributes': 7, 'Window': PySMALL_RECTType(Left=0,Top=0,Right=79,Bottom=24), 'MaximumWindowSize': PyCOORDType(X=80,Y=64)}
lsPyINPUT_RECORD(EventType=1) (KEY_EVENT)]
{'Size': PyCOORDType(X=80,Y=16000), 'CursorPosition': PyCOORDType(X=2,Y=8), 'Attributes': 7, 'Window': PySMALL_RECTType(Left=0,Top=0,Right=79,Bottom=24), 'MaximumWindowSize': PyCOORDType(X=80,Y=64)}

site package wexpect.py should not auto logger with wexpect.log

Is your feature request related to a problem? Please describe.
When working with windows server, as it is now, when you import wexpect, a log file is generate even if you don't desire it. This causes Windows Server file permission erros for users that doesn't have permission to file write.

Describe the solution you'd like
Log file should be generate as request

Describe alternatives you've considered
Comment all logger lines to avoid issues with Wi

Additional context

logger = logging.getLogger('wexpect')
if 'dev' in __version__ :
    logger.setLevel(logging.DEBUG)
else:
    logger.setLevel(logging.INFO)
fh = logging.FileHandler('wexpect.log', 'w', 'utf-8')
formatter = logging.Formatter('%(asctime)s - %(filename)s::%(funcName)s - %(levelname)s - %>(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

logger.info('wexpect imported; logger working')

How to get the matching print?

Describe the bug
I make a test on win10, try to send a few commands and get their feedback printing.
code like this

import wexpect
import re

cmdPrompt = re.compile('[A-Z]\:.+>')
p = wexpect.spawn('cmd.exe')
p.expect(cmdPrompt, timeout = 50)

p.sendline('VER')
p.expect(cmdPrompt, timeout = 50)
print('========1 VER')
print(p.before)
print(p.after)

p.sendline('VOL')
p.expect(cmdPrompt, timeout = 50)
print('========2 VOL')
print(p.before)
print(p.after)

p.sendline('ECHO 1')
p.expect(cmdPrompt, timeout = 50)
print('========3 ECHO 1')
print(p.before)
print(p.after)

To Reproduce
run this script on win10

Expected behavior
every step send the command, I can get the matching feedback printing

Screenshots
Now seems the feedback printing is always to be showing later
The execution results are as follows

D:\CODE>python test.py
========1 VER
D:\CODE>
========2 VOL
VER

Microsoft Windows [Version 10.0.18363.1110]

D:\CODE>
========3 ECHO 1

D:\CODE>
D:\CODE>

Environment:

  • [windows version] win10 10.0.18363.1110
  • [Python version] Python 3.8.5
  • [wexpect version] wexpect 4.0.0

UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-19: character maps to <undefined>

Describe the bug

Traceback (most recent call last):
  File "dflauncher.py", line 242, in <module>
    automate_script(3)
  File "dflauncher.py", line 167, in automate_script
    'Username:'], timeout=4)
  File "C:\Users\myusername\git\test\wexpect.py", line 1366, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "C:\Users\myusername\git\test\wexpect.py", line 1380, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "C:\Users\myusername\git\test\wexpect.py", line 1433, in expect_loop
    c = self.read_nonblocking(self.maxread, timeout)
  File "C:\Users\myusername\git\test\wexpect.py", line 1793, in read_nonblocking
    self.logfile_read.write (s)
  File "C:\Python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-19: character maps to <undefined>

To Reproduce
I've got no clue... use a colon in the expectation?

Expected behavior
Not crash.

Environment:

  • [windows version] 10
  • [Python version] 3.6
  • [wexpect version] latest from pypi source, v2.3.6

timeout None can crack

Describe the bug
Sometimes if timeout is None wexpect crashes.

To Reproduce
Run test_timeout_none testcase.

Expected behavior
Not to crash, should wait forever (EOF).

Environment:

  • OS Name: Microsoft Windows 10 Pro
  • Python 3.7
  • 3.0.1.dev

STDERR isn't handled properly

Issue: When the program prints to stderr there are many rubbish information in before field.

Reproduce:

p = wexpect.spawn('cmd')
p.expect('>')
p.before
p.sendline('asd')
p.expect('>')
p.before

Fix hello_wexpect

It would be nice if the hello_wexpect example worked on Windows.

  • There is no "ls" on Windows.
  • The "dir" output contains '>' characters.
# run list directory command
child.sendline('dir')

# Waiting for 'C:\>' prompt
child.expect('[A-Z]\:.+>')

sendline() fails in Cygwin

Sendline fails in cygwin

To Reproduce
Cygwin console:

PC /cygdrive/d/wexpect
$ python examples/cmd_wrapper.py
Microsoft Windows [Version 10.0.18362.356]
(c) 2019 Microsoft Corporation. All rights reserved.

D:\wexpect>ls
Traceback (most recent call last):
  File "examples/cmd_wrapper.py", line 34, in <module>
    p.sendline(command)
  File "D:\wexpect\wexpect.py", line 761, in sendline
    n = self.send(s)
  File "D:\wexpect\wexpect.py", line 1447, in send
    c = self.wtty.write(s)
  File "D:\wexpect\wexpect.py", line 1775, in write
    wrote = self.__consin.WriteConsoleInput(records)
pywintypes.error: (6, 'WriteConsoleInput', 'The handle is invalid.')

Expected behavior
Not to crash

Environment:

  • OS Name Microsoft Windows 10 Home
  • Version 10.0.18362 Build 18362
  • Python 3.7.5
  • 2.3.7.dev8
  • Cygwwin: uname -r : 2.11.2(0.329/5/3)

The python examples failes

Report of a bug

Reproduce

Run the python.py example

python examples\python.py

Output:

D:\bt\wexpect>python examples\python.py
Traceback (most recent call last):
  File "examples\python.py", line 26, in <module>
    p.sendline('Bob')
  File "C:\Users\usr\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wexpect.py", line 999, in sendline
    n = n + self.send (os.linesep)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Always TIMEOUT

Describe the bug
I cannot call wexpect.spawn because I always get a timeout error.

To Reproduce

>>> wexpect.spawn('cmd', timeout=10)
Traceback (most recent call last):
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 256, in __del__        
    self.terminate()
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 416, in terminate      
    self.kill()
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 446, in kill
    self.send(SIGNAL_CHARS[sig])
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 600, in send
    return self._send_impl(s)
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 977, in _send_impl     
    win32file.WriteFile(self.pipe, s)
pywintypes.error: (6, 'WriteFile', 'Controlador no válido.')

Exception ignored in: <function SpawnBase.__del__ at 0x000001F77D5E89D0>
Traceback (most recent call last):
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 256, in __del__        
    self.terminate()
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 416, in terminate
    self.kill()
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 446, in kill
    self.send(SIGNAL_CHARS[sig])
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 600, in send
    return self._send_impl(s)
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 977, in _send_impl     
    win32file.WriteFile(self.pipe, s)
pywintypes.error: (6, 'WriteFile', 'Controlador no válido.')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 875, in __init__       
    super().__init__(
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 247, in __init__       
    self.connect_to_child()
  File "c:\users\user\source\repos\copier\copier\.venv\src\wexpect\wexpect\host.py", line 897, in connect_to_child
    raise TIMEOUT('Connect to child has been timed out.')
wexpect.wexpect_util.TIMEOUT: Connect to child has been timed out.

Expected behavior
It should spawn the process and let me do stuff 😅

Screenshots

Environment:

  • Windows 10
  • Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)]
  • Tried with latest release from pypi and installing latest commit from git: c8977d8

Additional context
I'm mainly trying to spawn a python CLI app that runs in a Poetry venv. I guess I could hit #22 or #26, but this one is another bug, as it seems because running cmd also fails with the same problem. 🤷‍♂️

Add "encoding" and "codec_errors" arguments to "spawn"

Is your feature request related to a problem? Please describe.
I am trying to add windows support to my project and encoding-related args of spawn are not implemented.
To be specific, encoding and codec_errors are not implemented. I experienced this with WEXPECT_SPAWN_CLASS=SpawnSocket, probably true with other spawn classes.

Describe the solution you'd like
encoding and codec_errors arguments of spawn should be added and behave as in pexpect.

logger crashes at the end of the program

trying to set that ENV var in the script:

import os
os.environ['WEXPECT_SPAWN_CLASS'] = 'SpawnPipe'
os.environ['WEXPECT_LOGGER_LEVEL']='INFO'
from wexpect import spawn
from wexpect import TIMEOUT, EOF

yields this error at the end of my script

Traceback (most recent call last):
  File "C:\misc\my_script_dir\wexpect\host.py", line 312, in __del__
    logger.info('Deleting...')
  File "C:\Python36\lib\logging\__init__.py", line 1308, in info
    self._log(INFO, msg, args, **kwargs)
  File "C:\Python36\lib\logging\__init__.py", line 1444, in _log
    self.handle(record)
  File "C:\Python36\lib\logging\__init__.py", line 1454, in handle
    self.callHandlers(record)
  File "C:\Python36\lib\logging\__init__.py", line 1516, in callHandlers
    hdlr.handle(record)
  File "C:\Python36\lib\logging\__init__.py", line 865, in handle
    self.emit(record)
  File "C:\Python36\lib\logging\__init__.py", line 1071, in emit
    self.stream = self._open()
  File "C:\Python36\lib\logging\__init__.py", line 1061, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
NameError: name 'open' is not defined
Exception ignored in: <bound method SpawnBase.__del__ of <wexpect.host.SpawnPipe object at 0x000002444AD03E48>>
Traceback (most recent call last):
  File "C:\misc\my_script_dir\wexpect\host.py", line 320, in __del__
  File "C:\Python36\lib\logging\__init__.py", line 1320, in warning
  File "C:\Python36\lib\logging\__init__.py", line 1444, in _log
  File "C:\Python36\lib\logging\__init__.py", line 1454, in handle
  File "C:\Python36\lib\logging\__init__.py", line 1516, in callHandlers
  File "C:\Python36\lib\logging\__init__.py", line 865, in handle
  File "C:\Python36\lib\logging\__init__.py", line 1071, in emit
  File "C:\Python36\lib\logging\__init__.py", line 1061, in _open
NameError: name 'open' is not defined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.