Commit 838f26c9 authored by realead's avatar realead Committed by Stefan Behnel

Fix interspersed positional arguments in cythonize.py (GH-2988)

parent f46b869f
......@@ -201,8 +201,17 @@ def create_args_parser():
def parse_args_raw(parser, args):
options = parser.parse_args(args)
return (options, options.sources)
options, unknown = parser.parse_known_args(args)
sources = options.sources
# if positional arguments were interspersed
# some of them are in unknown
for option in unknown:
if option.startswith('-'):
parser.error("unknown option "+option)
else:
sources.append(option)
delattr(options, 'sources')
return (options, sources)
def parse_args(args):
......
......@@ -4,7 +4,12 @@ from Cython.Build.Cythonize import (
)
from unittest import TestCase
import argparse
import sys
try:
from StringIO import StringIO
except ImportError:
from io import StringIO # doesn't accept 'str' in Py2
class TestCythonizeArgsParser(TestCase):
......@@ -392,6 +397,46 @@ class TestCythonizeArgsParser(TestCase):
self.assertEqual(options.build_inplace, True)
self.assertTrue(self.are_default(options, ['build_inplace']))
def test_interspersed_positional(self):
options, sources = self.parse_args([
'file1.pyx', '-a',
'file2.pyx'
])
self.assertEqual(sources, ['file1.pyx', 'file2.pyx'])
self.assertEqual(options.annotate, 'default')
self.assertTrue(self.are_default(options, ['annotate']))
def test_interspersed_positional2(self):
options, sources = self.parse_args([
'file1.pyx', '-a',
'file2.pyx', '-a', 'file3.pyx'
])
self.assertEqual(sources, ['file1.pyx', 'file2.pyx', 'file3.pyx'])
self.assertEqual(options.annotate, 'default')
self.assertTrue(self.are_default(options, ['annotate']))
def test_interspersed_positional3(self):
options, sources = self.parse_args([
'-f', 'f1', 'f2', '-a',
'f3', 'f4', '-a', 'f5'
])
self.assertEqual(sources, ['f1', 'f2', 'f3', 'f4', 'f5'])
self.assertEqual(options.annotate, 'default')
self.assertEqual(options.force, True)
self.assertTrue(self.are_default(options, ['annotate', 'force']))
def test_wrong_option(self):
old_stderr = sys.stderr
stderr = sys.stderr = StringIO()
try:
self.assertRaises(SystemExit, self.parse_args,
['--unknown-option']
)
finally:
sys.stderr = old_stderr
self.assertTrue(stderr.getvalue())
class TestParseArgs(TestCase):
def test_build_set_for_inplace(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment