In json5, the -c option tells the command-line tool to parse a string supplied directly on the command line. But passing -c '' could make it read from stdin instead. Add a filename, and it would read that file.
I came across this while working on pyjson5, the Python implementation of JSON5. The parser itself was working as expected. The problem was in how the CLI chose its input.
Reproducing the Input Fallback
The CLI accepts an inline string, a file, or stdin. When -c is provided, its value should take precedence over the other two sources.
On the affected implementation, this command reads from stdin despite the explicit empty argument:
printf '"from-stdin"\n' | python -m json5 -c ''
Output:
"from-stdin"
It also exits with status 0, even though the empty string passed to -c is not valid JSON5.
A file argument produces the same kind of fallback:
printf '"from-file"\n' > example.json5
python -m json5 -c '' example.json5
Output:
"from-file"
These examples use POSIX shell syntax. I checked the behavior using json5 0.14.0 with Python 3.13.5 on Linux.
The bug is also present in the tagged source for json5 0.17.3, the latest PyPI release as of November 18, 2026. The fix was merged on November 14 but has not yet appeared in a published release.
Why the Empty Argument Was Ignored
The original input-selection code in json5/tool.py was:
if args.cmd:
inp = args.cmd
elif args.file == '-':
inp = host.stdin.read()
else:
inp = host.read_text_file(args.file)
Here, args.cmd comes from argparse. If -c is omitted, its value is None. If the user explicitly passes -c '', its value is an empty string.
The first condition treats both values the same way because '' is falsy in Python.
For an empty -c argument, execution therefore skips the first branch. If no filename was given, args.file defaults to '-', so the CLI reads stdin. Otherwise, it reads the specified file.
Whatever it reads is then passed to the JSON5 parser. The original empty argument never gets there.
The fix was small:
- if args.cmd:
+ if args.cmd is not None:
This checks whether the argument is absent, rather than whether its value is truthy.
With that change, -c '' passes the empty string to json5.loads(). The parser rejects it, and the CLI reports:
Empty strings are not legal JSON5
The command exits with status 1. When -c is omitted, the existing file and stdin behavior is unchanged.
Writing a Regression Test That Catches the Fallback
The regression tests turned out to be more interesting than the fix.
Suppose a test runs the CLI with -c '' and supplies no stdin content. The broken implementation would read empty stdin and report a parsing error anyway. A test that only expects an error might pass, even though the CLI selected the wrong input source.
The new test avoids that problem by giving stdin valid JSON5:
self.check(
['-c', ''],
stdin='"foo"',
returncode=1,
out='',
err='Empty strings are not legal JSON5\n',
)
Before the fix, the CLI would read "foo", parse it successfully, and exit with status 0. The test expects the empty inline string to be rejected instead.
A second test covers the filename case:
self.check(
['-c', '', 'foo.json5'],
files={'foo.json5': '"foo"\n'},
returncode=1,
out='',
err='Empty strings are not legal JSON5\n',
)
Here, the file also contains valid JSON5. If the CLI falls back to that file, the test fails.
The project's test helper runs these cases both in-process with a fake host and through a subprocess. It can therefore check the return code, stdout, and stderr without relying on an interactive terminal.
Using valid alternative input is what makes these tests effective. The incorrect fallback produces a different result from the intended behavior, so the test can distinguish the two.
A Small Distinction Worth Checking
The same issue can appear in other tools that accept optional string arguments:
if args.value:
process(args.value)
There is nothing inherently wrong with that condition. It works if empty strings are supposed to be ignored.
But if an empty string is a supplied value that should be processed or rejected, the condition loses information. For arguments where None represents absence, an explicit is not None check preserves that distinction.
In this case, one changed condition fixed the CLI. The regression tests made sure an invalid inline argument could no longer be replaced by valid input from somewhere else.
Top comments (0)