Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Travis CI: Add flake8 tests for syntax errors and undefined names #536

Closed
wants to merge 17 commits into from
Closed
20 changes: 13 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ python:

matrix:
include:
- python 3.7
dist: xenial # required for Python 3.7 (travis-ci/travis-ci#9069)
sudo: required # required for Python 3.7 (travis-ci/travis-ci#9069)
- python: 3.7
dist: xenial # required for Python 3.7 (travis-ci/travis-ci#9069)
sudo: required # required for Python 3.7 (travis-ci/travis-ci#9069)
allow_failures:
# PyPy on Travis is currently incompatible with Cryptography.
- python: pypy

install:
- export PYTHONIOENCODING=UTF8
- pip install coveralls pytest-cov ptyprocess
- pip install coveralls flake8 pytest-cov ptyprocess

before_script:
# stop the build if there are Python syntax errors or undefined names
- flake8 . --count --exclude=./tests --select=E901,E999,F821,F822,F823 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

script:
- ./tools/display-sighandlers.py
- ./tools/display-terminalinfo.py
- py.test --cov pexpect --cov-config .coveragerc
- ./tools/display-sighandlers.py
- ./tools/display-terminalinfo.py
- py.test --cov pexpect --cov-config .coveragerc

after_success:
- coverage combine
Expand Down
5 changes: 4 additions & 1 deletion examples/chess.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,7 @@ def quit(self):
white.do_move (move_black)
print('tail of loop')

g.quit()
try:
g.quit()
except NameError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the linter identified a real problem with the example here - this fix is just sweeping it under the carpet.

pass
7 changes: 4 additions & 3 deletions examples/chess2.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def LOG (s):

white.do_move (move_black, 1)

g.quit()


try:
g.quit()
except NameError:
pass
5 changes: 4 additions & 1 deletion examples/chess3.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,7 @@ def quit(self):
white.do_move (move_black)
print('tail of loop')

g.quit()
try:
g.quit()
except NameError:
pass
2 changes: 1 addition & 1 deletion examples/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def login (args, cli_username=None, cli_password=None):
for hostname in host_names:
print('connecting to', hostname)
try:
fout = file("log_"+hostname, "w")
fout = open("log_"+hostname, "w")
hive[hostname] = pxssh.pxssh()
# Disable host key checking.
hive[hostname].SSH_OPTS = (hive[hostname].SSH_OPTS
Expand Down
3 changes: 2 additions & 1 deletion examples/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ def main():
index = child.expect([pexpect.EOF, "(?i)there are stopped jobs"])
if index==1:
child.sendline("exit")
child.expect(EOF)
child.expect(pexpect.EOF)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion examples/passmass.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
def login(host, user, password):

child = pexpect.spawn('ssh -l %s %s'%(user, host))
fout = file ("LOG.TXT","wb")
fout = open("LOG.TXT","wb")
child.logfile_read = fout #use child.logfile to also log writes (passwords!)

i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
Expand Down
6 changes: 3 additions & 3 deletions examples/topip.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def main():

# load the stats from the last run.
try:
last_stats = pickle.load(file(TOPIP_LAST_RUN_STATS))
last_stats = pickle.load(open(TOPIP_LAST_RUN_STATS))
except:
last_stats = {'maxip':None}

Expand All @@ -280,7 +280,7 @@ def main():
% hostname, alert_addr_from, alert_addr_to)
if log_flag:
if verbose: print('LOGGING THIS EVENT')
fout = file(TOPIP_LOG_FILE,'a')
fout = open(TOPIP_LOG_FILE,'a')
#dts = time.strftime('%Y:%m:%d:%H:%M:%S', time.localtime())
dts = time.asctime()
fout.write ('%s - %d connections from %s\n'
Expand All @@ -289,7 +289,7 @@ def main():

# save state to TOPIP_LAST_RUN_STATS
try:
pickle.dump(s, file(TOPIP_LAST_RUN_STATS,'w'))
pickle.dump(s, open(TOPIP_LAST_RUN_STATS,'w'))
os.chmod (TOPIP_LAST_RUN_STATS, 0o664)
except:
pass
Expand Down
11 changes: 7 additions & 4 deletions notes/my_forkpty.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import print_function
import os, fcntl, termios
import time

from pexpect import ExceptionPexpect


def my_forkpty():

(master_fd, slave_fd) = os.openpty()
Expand Down Expand Up @@ -80,10 +84,9 @@ def my_forkpty():

pid, fd = my_forkpty ()
if pid == 0: # child
print 'I am not a robot!'
print('I am not a robot!')
else:
print '(pid, fd) = (%d, %d)' % (pid, fd)
print('(pid, fd) = (%d, %d)' % (pid, fd))
time.sleep(1) # Give the child a chance to print.
print 'Robots always say:', os.read(fd,100)
print('Robots always say:', os.read(fd,100))
os.close(fd)

5 changes: 4 additions & 1 deletion pexpect/FSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ def main():
print('Use the = sign to evaluate and print the expression.')
print('For example: ')
print(' 167 3 2 2 * * * 1 - =')
inputstr = (input if PY3 else raw_input)('> ') # analysis:ignore
try:
inputstr = raw_input('> ')
except NameError:
inputstr = input('> ')
f.process_list(inputstr)


Expand Down
3 changes: 3 additions & 0 deletions pexpect/_async.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# flake8: noqa This file is Python 3-only and async yield is a Py2 syntax error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way to keep linting it on Python 3?

# TODO: Remove these two lines when support for legacy Python is dropped.

import asyncio
import errno
import signal
Expand Down
17 changes: 11 additions & 6 deletions pexpect/spawnbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from .expect import Expecter, searcher_string, searcher_re

PY3 = (sys.version_info[0] >= 3)
text_type = str if PY3 else unicode
try:
text_type = unicode
except NameError:
text_type = str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually prefer the explicit PY3 check here. It's frustrating if we have to make the code worse in my opinion just to satisfy an automated tool. As a compromise, we could add a dependency on six and just use six.text_type.



class _NullCoder(object):
"""Pass bytes through unchanged."""
Expand Down Expand Up @@ -88,7 +92,11 @@ def __init__(self, timeout=30, maxread=2000, searchwindowsize=None,
self.string_type = bytes
self.buffer_type = BytesIO
self.crlf = b'\r\n'
if PY3:
try: # Python 2
self.allowed_string_types = (basestring, )
self.linesep = os.linesep
self.write_to_stdout = sys.stdout.write
except NameError: # Python 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly prefer the if PY3 check here. Catching NameError is a bug waiting to happen. If we can't find a way to use flake8 with the explicit if PY3, I'd rather not use it at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: I'd rather not use it automated in CI. It's still fine to run it manually and fix problems.

self.allowed_string_types = (bytes, str)
self.linesep = os.linesep.encode('ascii')
def write_to_stdout(b):
Expand All @@ -98,10 +106,7 @@ def write_to_stdout(b):
# If stdout has been replaced, it may not have .buffer
return sys.stdout.write(b.decode('ascii', 'replace'))
self.write_to_stdout = write_to_stdout
else:
self.allowed_string_types = (basestring,) # analysis:ignore
self.linesep = os.linesep
self.write_to_stdout = sys.stdout.write

else:
# unicode mode
self._encoder = codecs.getincrementalencoder(encoding)(codec_errors)
Expand Down
6 changes: 3 additions & 3 deletions pexpect/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
# Alias Python2 exception to Python3
InterruptedError = select.error

if sys.version_info[0] >= 3:
string_types = (str,)
else:
try:
string_types = (unicode, str)
except NameError:
string_types = (str,)


def is_executable_file(path):
Expand Down
5 changes: 3 additions & 2 deletions tests/platform_checks/check.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python
import signal
import os
import signal
import sys
import time
import pty


def signal_handler (signum, frame):
print 'Signal handler called with signal:', signum
print 'signal.SIGCHLD=', signal.SIGKILL
Expand Down Expand Up @@ -74,4 +76,3 @@ def signal_handler (signum, frame):
print 'Child is alive. This is ambiguous because it may be a Zombie.'
except OSError as e:
print 'Child appears to be dead.'

2 changes: 1 addition & 1 deletion tests/platform_checks/check_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
break
os.close(fd_in)
d = os.read(fd_in, 1) # fd_in should be closed now...
if s == '':
if d == '':
print 'd is empty. good.'
1 change: 1 addition & 0 deletions tests/test_run_out_of_pty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

'''
import pexpect
import sys
import unittest
from . import PexpectTestCase

Expand Down