Skip to content

Commit fbdbc79

Browse files
authored
test_benchmark.py: Make hardcode_arguments aware of args (#26293)
Previously we were just always embedding `DEFAULT_ARG`, but the caller can pass whatever args they like. Since `hardcode_arguments` is already best effort (it doesn't work unless it can fine `int main` in the source code and when it cannot it silently does nothing) it seems reasonable to also bail out in the same way if we have more than one argument (which we do not support). See #8081
1 parent 0a2a215 commit fbdbc79

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

test/test_benchmark.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,19 @@ def tearDownClass(cls):
408408
}
409409
utils.write_file('stats.json', json.dumps(output, indent=2) + '\n')
410410

411-
# avoid depending on argument reception from the commandline
412-
def hardcode_arguments(self, code):
411+
def hardcode_arguments(self, code, args):
412+
"""Avoid depending on argument reception from the commandline, where possible.
413+
414+
Here we take the command line arguments and embed them directly into `main` function.
415+
If we cannot find a `main` function, or if we have more than one argument, we
416+
do not do any embedding, and the resulting test will depend on arguments being
417+
passed via argv (which works in most environments).
418+
"""
413419
if not code or 'int main()' in code:
414420
return code
421+
# We only know how to embed a single argument
422+
if len(args) != 1:
423+
return code
415424
main_pattern = 'int main(int argc, char **argv)'
416425
assert main_pattern in code
417426
code = code.replace(main_pattern, 'int benchmark_main(int argc, char **argv)')
@@ -422,7 +431,7 @@ def hardcode_arguments(self, code):
422431
int ret = benchmark_main(newArgc, newArgv);
423432
return ret;
424433
}
425-
''' % DEFAULT_ARG
434+
''' % args[0]
426435
return code
427436

428437
def do_benchmark(self, name, src, expected_output='FAIL', args=None,
@@ -436,7 +445,7 @@ def do_benchmark(self, name, src, expected_output='FAIL', args=None,
436445
args = args or [DEFAULT_ARG]
437446
dirname = self.get_dir()
438447
filename = os.path.join(dirname, name + '.c' + ('' if force_c else 'pp'))
439-
src = self.hardcode_arguments(src)
448+
src = self.hardcode_arguments(src, args)
440449
utils.write_file(filename, src)
441450

442451
print()

0 commit comments

Comments
 (0)