Skip to content

Commit d573261

Browse files
committed
Merge pull request #58 from jayvdb/api_main_args
Allow passing args to main()
2 parents 9a2698d + f008459 commit d573261

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

pyflakes/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def handler(sig, f):
169169
pass
170170

171171

172-
def main(prog=None):
172+
def main(prog=None, args=None):
173173
"""Entry point for the script "pyflakes"."""
174174
import optparse
175175

@@ -178,7 +178,7 @@ def main(prog=None):
178178
_exitOnSignal('SIGPIPE', 1)
179179

180180
parser = optparse.OptionParser(prog=prog, version=__version__)
181-
(__, args) = parser.parse_args()
181+
(__, args) = parser.parse_args(args=args)
182182
reporter = modReporter._makeDefaultReporter()
183183
if args:
184184
warnings = checkRecursive(args, reporter)

pyflakes/test/test_api.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pyflakes.messages import UnusedImport
1212
from pyflakes.reporter import Reporter
1313
from pyflakes.api import (
14+
main,
1415
checkPath,
1516
checkRecursive,
1617
iterSourceCode,
@@ -52,6 +53,33 @@ def __init__(self, lineno, col_offset=0):
5253
self.col_offset = col_offset
5354

5455

56+
class SysStreamCapturing(object):
57+
58+
"""Replaces sys.stdin, sys.stdout and sys.stderr with StringIO objects."""
59+
60+
def __init__(self, stdin):
61+
self._stdin = StringIO(stdin or '')
62+
63+
def __enter__(self):
64+
self._orig_stdin = sys.stdin
65+
self._orig_stdout = sys.stdout
66+
self._orig_stderr = sys.stderr
67+
68+
sys.stdin = self._stdin
69+
sys.stdout = self._stdout_stringio = StringIO()
70+
sys.stderr = self._stderr_stringio = StringIO()
71+
72+
return self
73+
74+
def __exit__(self, *args):
75+
self.output = self._stdout_stringio.getvalue()
76+
self.error = self._stderr_stringio.getvalue()
77+
78+
sys.stdin = self._orig_stdin
79+
sys.stdout = self._orig_stdout
80+
sys.stderr = self._orig_stderr
81+
82+
5583
class LoggingReporter(object):
5684
"""
5785
Implementation of Reporter that just appends any error to a list.
@@ -588,8 +616,8 @@ def runPyflakes(self, paths, stdin=None):
588616
"""
589617
Launch a subprocess running C{pyflakes}.
590618
591-
@param args: Command-line arguments to pass to pyflakes.
592-
@param kwargs: Options passed on to C{subprocess.Popen}.
619+
@param paths: Command-line arguments to pass to pyflakes.
620+
@param stdin: Text to use as stdin.
593621
@return: C{(returncode, stdout, stderr)} of the completed pyflakes
594622
process.
595623
"""
@@ -600,7 +628,7 @@ def runPyflakes(self, paths, stdin=None):
600628
if stdin:
601629
p = subprocess.Popen(command, env=env, stdin=subprocess.PIPE,
602630
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
603-
(stdout, stderr) = p.communicate(stdin)
631+
(stdout, stderr) = p.communicate(stdin.encode('ascii'))
604632
else:
605633
p = subprocess.Popen(command, env=env,
606634
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -648,6 +676,21 @@ def test_readFromStdin(self):
648676
"""
649677
If no arguments are passed to C{pyflakes} then it reads from stdin.
650678
"""
651-
d = self.runPyflakes([], stdin='import contraband'.encode('ascii'))
679+
d = self.runPyflakes([], stdin='import contraband')
652680
expected = UnusedImport('<stdin>', Node(1), 'contraband')
653681
self.assertEqual(d, ("%s%s" % (expected, os.linesep), '', 1))
682+
683+
684+
class TestMain(IntegrationTests):
685+
"""
686+
Tests of the pyflakes main function.
687+
"""
688+
689+
def runPyflakes(self, paths, stdin=None):
690+
try:
691+
with SysStreamCapturing(stdin) as capture:
692+
main(args=paths)
693+
except SystemExit as e:
694+
return (capture.output, capture.error, e.code)
695+
else:
696+
raise RuntimeError('SystemExit not raised')

0 commit comments

Comments
 (0)