Skip to content

Commit fe79282

Browse files
author
Matthias Koeppe
committed
sage -fixdoctests: Fix handling of 2 file arguments
1 parent f631de2 commit fe79282

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/bin/sage-fixdoctests

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ def process_block(block, src_in_lines, file_optional_tags):
314314
def output_filename(filename):
315315
if len(args.filename) == 2 and not args.overwrite and not args.no_overwrite:
316316
if args.filename[0] == filename:
317-
print("sage-fixdoctests: When passing two filenames, the second one is taken as an output filename; "
318-
"this is deprecated. To pass two input filenames, use the option --overwrite.")
319317
return args.filename[1]
318+
else:
319+
return None
320320
return filename + ".fixed"
321321
if args.no_overwrite:
322322
return filename + ".fixed"
@@ -340,7 +340,14 @@ else:
340340
print(f'Doctester exited with error status {status}')
341341
sys.exit(status)
342342
# Run the doctester, putting the output of the test into sage's temporary directory
343-
input_args = " ".join(shlex.quote(f) for f in args.filename)
343+
if len(args.filename) == 2 and not args.overwrite and not args.no_overwrite:
344+
print("sage-fixdoctests: When passing two filenames, the second one is taken as an output filename; "
345+
"this is deprecated. To pass two input filenames, use the option --overwrite.")
346+
347+
input_filenames = [args.filename[0]]
348+
else:
349+
input_filenames = args.filename
350+
input_args = " ".join(shlex.quote(f) for f in input_filenames)
344351
cmdline = f'{shlex.quote(executable)} -t -p {environment_args}{long_args}{probe_args}{lib_args}{input_args}'
345352
print(f'Running "{cmdline}"')
346353
os.system(f'{cmdline} > {shlex.quote(doc_file)}')
@@ -364,7 +371,7 @@ def block_filename(block):
364371

365372
def expanded_filename_args():
366373
DD = DocTestDefaults(optional='all')
367-
DC = DocTestController(DD, args.filename)
374+
DC = DocTestController(DD, input_filenames)
368375
DC.add_files()
369376
DC.expand_files_into_sources()
370377
for source in DC.sources:
@@ -418,22 +425,23 @@ def process_grouped_blocks(grouped_iterator):
418425
persistent_optional_tags = {}
419426

420427
if src_in_lines != shallow_copy_of_src_in_lines:
421-
output = output_filename(input)
422-
with open(output, 'w') as test_output:
423-
for line in src_in_lines:
424-
if line is None:
425-
continue
426-
test_output.write(line)
427-
test_output.write('\n')
428-
429-
# Show summary of changes
430-
if input != output:
431-
print("The fixed doctests have been saved as '{0}'.".format(output))
428+
if (output := output_filename(input)) is None:
429+
print(f"Not saving modifications made in '{input}'")
432430
else:
433-
relative = os.path.relpath(output, SAGE_ROOT)
434-
print(f"The input file '{output}' has been overwritten.")
435-
if not relative.startswith('..'):
436-
subprocess.call(['git', '--no-pager', 'diff', relative], cwd=SAGE_ROOT)
431+
with open(output, 'w') as test_output:
432+
for line in src_in_lines:
433+
if line is None:
434+
continue
435+
test_output.write(line)
436+
test_output.write('\n')
437+
# Show summary of changes
438+
if input != output :
439+
print("The fixed doctests have been saved as '{0}'.".format(output))
440+
else:
441+
relative = os.path.relpath(output, SAGE_ROOT)
442+
print(f"The input file '{output}' has been overwritten.")
443+
if not relative.startswith('..'):
444+
subprocess.call(['git', '--no-pager', 'diff', relative], cwd=SAGE_ROOT)
437445
else:
438446
print(f"No fixes made in '{input}'")
439447

0 commit comments

Comments
 (0)