summaryrefslogtreecommitdiffstats
path: root/Lib/json
diff options
context:
space:
mode:
authorTrey Hunner <trey@treyhunner.com>2024-08-13 16:09:38 (GMT)
committerGitHub <noreply@github.com>2024-08-13 16:09:38 (GMT)
commit906b796af8388174cf493e23f29720eaed9fdf03 (patch)
tree4deb9e533a004fb3ec740b2d1b2ac060255704c2 /Lib/json
parentdb6f5e193315a3bbfa7b0b6644203bae3f76b638 (diff)
downloadcpython-906b796af8388174cf493e23f29720eaed9fdf03.zip
cpython-906b796af8388174cf493e23f29720eaed9fdf03.tar.gz
cpython-906b796af8388174cf493e23f29720eaed9fdf03.tar.bz2
gh-122873: Allow "python -m json" to work (#122884)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Diffstat (limited to 'Lib/json')
-rw-r--r--Lib/json/__init__.py6
-rw-r--r--Lib/json/__main__.py20
-rw-r--r--Lib/json/tool.py17
3 files changed, 28 insertions, 15 deletions
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
index ed2c747..1d972d2 100644
--- a/Lib/json/__init__.py
+++ b/Lib/json/__init__.py
@@ -86,13 +86,13 @@ Specializing JSON object encoding::
'[2.0, 1.0]'
-Using json.tool from the shell to validate and pretty-print::
+Using json from the shell to validate and pretty-print::
- $ echo '{"json":"obj"}' | python -m json.tool
+ $ echo '{"json":"obj"}' | python -m json
{
"json": "obj"
}
- $ echo '{ 1.2:3.4}' | python -m json.tool
+ $ echo '{ 1.2:3.4}' | python -m json
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
"""
__version__ = '2.0.9'
diff --git a/Lib/json/__main__.py b/Lib/json/__main__.py
new file mode 100644
index 0000000..1808ead
--- /dev/null
+++ b/Lib/json/__main__.py
@@ -0,0 +1,20 @@
+"""Command-line tool to validate and pretty-print JSON
+
+Usage::
+
+ $ echo '{"json":"obj"}' | python -m json
+ {
+ "json": "obj"
+ }
+ $ echo '{ 1.2:3.4}' | python -m json
+ Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
+
+"""
+import json.tool
+
+
+if __name__ == '__main__':
+ try:
+ json.tool.main()
+ except BrokenPipeError as exc:
+ raise SystemExit(exc.errno)
diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index fdfc337..9028e51 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -1,14 +1,7 @@
-r"""Command-line tool to validate and pretty-print JSON
-
-Usage::
-
- $ echo '{"json":"obj"}' | python -m json.tool
- {
- "json": "obj"
- }
- $ echo '{ 1.2:3.4}' | python -m json.tool
- Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
+"""Command-line tool to validate and pretty-print JSON
+See `json.__main__` for a usage example (invocation as
+`python -m json.tool` is supported for backwards compatibility).
"""
import argparse
import json
@@ -16,7 +9,7 @@ import sys
def main():
- prog = 'python -m json.tool'
+ prog = 'python -m json'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description)
@@ -86,4 +79,4 @@ if __name__ == '__main__':
try:
main()
except BrokenPipeError as exc:
- sys.exit(exc.errno)
+ raise SystemExit(exc.errno)