aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2023-01-15 20:15:03 -0500
committerNirbheek Chauhan <nirbheek@centricular.com>2023-02-06 23:37:55 +0530
commit049f4888ca99c3d6ad1a474becd634dbc07480c5 (patch)
treebb8d09eb1bbdcc1f094cf82daf61cfc6d26d4d84
parentf8f2f16d825c1990b877e1802b680810c7bd907d (diff)
downloadmeson-049f4888ca99c3d6ad1a474becd634dbc07480c5.zip
meson-049f4888ca99c3d6ad1a474becd634dbc07480c5.tar.gz
meson-049f4888ca99c3d6ad1a474becd634dbc07480c5.tar.bz2
runpython: make it work for -c as well
In commit 4e4f97edb3d475273108b203bc02b04bd6840b06 we added support for runpython to accept `-c 'code to execute'` in addition to just script files. However, doing so would mangle the sys.argv in the executed code -- which assumes, as python itself does, that argv is the stuff after the code to execute. We correctly handled this for script files, but the original addition of -c support pushed this handling into a script-file specific block.
-rw-r--r--mesonbuild/mesonmain.py4
-rwxr-xr-xrun_meson_command_tests.py19
2 files changed, 19 insertions, 4 deletions
diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py
index ca38f29..1054e61 100644
--- a/mesonbuild/mesonmain.py
+++ b/mesonbuild/mesonmain.py
@@ -139,11 +139,11 @@ class CommandLineParser:
parser.add_argument('script_args', nargs=argparse.REMAINDER)
def run_runpython_command(self, options):
- import runpy
+ sys.argv[1:] = options.script_args
if options.eval_arg:
exec(options.script_file)
else:
- sys.argv[1:] = options.script_args
+ import runpy
sys.path.insert(0, os.path.dirname(options.script_file))
runpy.run_path(options.script_file, run_name='__main__')
return 0
diff --git a/run_meson_command_tests.py b/run_meson_command_tests.py
index e044af5..040105a 100755
--- a/run_meson_command_tests.py
+++ b/run_meson_command_tests.py
@@ -84,7 +84,7 @@ class CommandTests(unittest.TestCase):
os.chdir(str(self.orig_dir))
super().tearDown()
- def _run(self, command, workdir=None):
+ def _run(self, command, workdir=None, env=None):
'''
Run a command while printing the stdout, and also return a copy of it
'''
@@ -92,7 +92,7 @@ class CommandTests(unittest.TestCase):
# between CI issue and test bug in that case. Set timeout and fail loud
# instead.
p = subprocess.run(command, stdout=subprocess.PIPE,
- env=os.environ.copy(), text=True,
+ env=env, text=True,
cwd=workdir, timeout=60 * 5)
print(p.stdout)
if p.returncode != 0:
@@ -210,6 +210,21 @@ class CommandTests(unittest.TestCase):
self._run([script.as_posix(), source, '--outfile', target, '--interpreter', python_command[0]])
self._run([target.as_posix(), '--help'])
+ def test_meson_runpython(self):
+ meson_command = str(self.src_root / 'meson.py')
+ script_file = str(self.src_root / 'foo.py')
+ test_command = 'import sys; print(sys.argv[1])'
+ env = os.environ.copy()
+ del env['MESON_COMMAND_TESTS']
+ with open(script_file, 'w') as f:
+ f.write('#!/usr/bin/env python3\n\n')
+ f.write(f'{test_command}\n')
+
+ for cmd in [['-c', test_command, 'fake argument'], [script_file, 'fake argument']]:
+ pyout = self._run(python_command + cmd)
+ mesonout = self._run(python_command + [meson_command, 'runpython'] + cmd, env=env)
+ self.assertEqual(pyout, mesonout)
+
if __name__ == '__main__':
print('Meson build system', meson_version, 'Command Tests')