Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-122873: Allow "python3 -m json" to work #122884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
27e449d
Rename json.tool to json.__main__
treyhunner Aug 1, 2024
423c971
Make json.tool delegate to json.__main__
treyhunner Aug 1, 2024
1c1a8fa
Note json.tool deprecation
treyhunner Aug 1, 2024
f8f6931
Move main function from json.__main__ to json.tool
treyhunner Aug 2, 2024
834fbd7
Remove deprecation warning
treyhunner Aug 9, 2024
62afcf1
Fix module name as shown at command-line
treyhunner Aug 10, 2024
f58e60a
Update docs and what's new for json.tool to json
treyhunner Aug 10, 2024
168082a
Remove duplicate json module description from docs
treyhunner Aug 10, 2024
52039ee
Give up on marking `-m` as an option
treyhunner Aug 10, 2024
2e37475
Remove unnecessary newline in changed note
treyhunner Aug 10, 2024
efd0503
Remove now-invalid json.tool module reference
treyhunner Aug 10, 2024
c6bb211
Add news entry with blurb
treyhunner Aug 10, 2024
e768e39
Remove older raw string usage in docstring
treyhunner Aug 10, 2024
a6b5c0d
Use sentence case in section title
treyhunner Aug 10, 2024
3105bc2
Add CLI invocation for easier copy-pasting
treyhunner Aug 10, 2024
238fc57
Update json.__init__ docstring for python -m json
treyhunner Aug 10, 2024
7f704bf
Note `python -m json` in What's New
treyhunner Aug 10, 2024
fba68ac
Use SystemExit consistently in json package
treyhunner Aug 10, 2024
8662c81
Improve grammar in news
treyhunner Aug 12, 2024
52129e6
Add json.tool submodule documentation reference back in.
treyhunner Aug 12, 2024
016206d
Note that json.tool still works for compatibility reasons
treyhunner Aug 12, 2024
9b22285
Note that json.tool implements the CLI
treyhunner Aug 12, 2024
9fd0c1c
Reference json.__main__ in json.tool docstring
treyhunner Aug 12, 2024
734fbf9
Switch json.tool to json on listing of Python CLIs
treyhunner Aug 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove deprecation warning
  • Loading branch information
treyhunner committed Aug 10, 2024
commit 834fbd7b37fdcf398da14c452e0673dcf11907ef
2 changes: 0 additions & 2 deletions 2 Lib/json/tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import argparse
treyhunner marked this conversation as resolved.
Show resolved Hide resolved
import json
import sys
import warnings


def main():
Expand Down Expand Up @@ -72,7 +71,6 @@ def main():


if __name__ == '__main__':
warnings.warn('The json.tool module is deprecated', DeprecationWarning)
try:
main()
except BrokenPipeError as exc:
Expand Down
48 changes: 14 additions & 34 deletions 48 Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class TestMain(unittest.TestCase):
:"yes"} ]
"""
module = 'json'
env_vars = {}
warnings_expected = False

expect_without_sort_keys = textwrap.dedent("""\
[
Expand Down Expand Up @@ -93,8 +91,7 @@ def test_stdin_stdout(self):
args = sys.executable, '-m', self.module
process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, self.expect)
if not self.warnings_expected:
self.assertEqual(process.stderr, '')
self.assertEqual(process.stderr, '')

def _create_infile(self, data=None):
infile = os_helper.TESTFN
Expand All @@ -105,7 +102,7 @@ def _create_infile(self, data=None):

def test_infile_stdout(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', self.module, infile, **self.env_vars)
rc, out, err = assert_python_ok('-m', self.module, infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
self.assertEqual(err, b'')
Expand All @@ -119,7 +116,7 @@ def test_non_ascii_infile(self):
''').encode()

infile = self._create_infile(data)
rc, out, err = assert_python_ok('-m', self.module, infile, **self.env_vars)
rc, out, err = assert_python_ok('-m', self.module, infile)

self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), expect.splitlines())
Expand All @@ -128,7 +125,7 @@ def test_non_ascii_infile(self):
def test_infile_outfile(self):
infile = self._create_infile()
outfile = os_helper.TESTFN + '.out'
rc, out, err = assert_python_ok('-m', self.module, infile, outfile, **self.env_vars)
rc, out, err = assert_python_ok('-m', self.module, infile, outfile)
self.addCleanup(os.remove, outfile)
with open(outfile, "r", encoding="utf-8") as fp:
self.assertEqual(fp.read(), self.expect)
Expand All @@ -138,7 +135,7 @@ def test_infile_outfile(self):

def test_writing_in_place(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', self.module, infile, infile, **self.env_vars)
rc, out, err = assert_python_ok('-m', self.module, infile, infile)
with open(infile, "r", encoding="utf-8") as fp:
self.assertEqual(fp.read(), self.expect)
self.assertEqual(rc, 0)
Expand All @@ -149,18 +146,17 @@ def test_jsonlines(self):
args = sys.executable, '-m', self.module, '--json-lines'
process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, self.jsonlines_expect)
if not self.warnings_expected:
self.assertEqual(process.stderr, '')
self.assertEqual(process.stderr, '')

def test_help_flag(self):
rc, out, err = assert_python_ok('-m', self.module, '-h', **self.env_vars)
rc, out, err = assert_python_ok('-m', self.module, '-h')
self.assertEqual(rc, 0)
self.assertTrue(out.startswith(b'usage: '))
self.assertEqual(err, b'')

def test_sort_keys_flag(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', self.module, '--sort-keys', infile, **self.env_vars)
rc, out, err = assert_python_ok('-m', self.module, '--sort-keys', infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(),
self.expect_without_sort_keys.encode().splitlines())
Expand All @@ -177,41 +173,37 @@ def test_indent(self):
args = sys.executable, '-m', self.module, '--indent', '2'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
if not self.warnings_expected:
self.assertEqual(process.stderr, '')
self.assertEqual(process.stderr, '')

def test_no_indent(self):
input_ = '[1,\n2]'
expect = '[1, 2]\n'
args = sys.executable, '-m', self.module, '--no-indent'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
if not self.warnings_expected:
self.assertEqual(process.stderr, '')
self.assertEqual(process.stderr, '')

def test_tab(self):
input_ = '[1, 2]'
expect = '[\n\t1,\n\t2\n]\n'
args = sys.executable, '-m', self.module, '--tab'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
if not self.warnings_expected:
self.assertEqual(process.stderr, '')
self.assertEqual(process.stderr, '')

def test_compact(self):
input_ = '[ 1 ,\n 2]'
expect = '[1,2]\n'
args = sys.executable, '-m', self.module, '--compact'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
if not self.warnings_expected:
self.assertEqual(process.stderr, '')
self.assertEqual(process.stderr, '')

def test_no_ensure_ascii_flag(self):
infile = self._create_infile('{"key":"馃挬"}')
outfile = os_helper.TESTFN + '.out'
self.addCleanup(os.remove, outfile)
assert_python_ok('-m', self.module, '--no-ensure-ascii', infile, outfile, **self.env_vars)
assert_python_ok('-m', self.module, '--no-ensure-ascii', infile, outfile)
with open(outfile, "rb") as f:
lines = f.read().splitlines()
# asserting utf-8 encoded output file
Expand All @@ -222,7 +214,7 @@ def test_ensure_ascii_default(self):
infile = self._create_infile('{"key":"馃挬"}')
outfile = os_helper.TESTFN + '.out'
self.addCleanup(os.remove, outfile)
assert_python_ok('-m', self.module, infile, outfile, **self.env_vars)
assert_python_ok('-m', self.module, infile, outfile)
with open(outfile, "rb") as f:
lines = f.read().splitlines()
# asserting an ascii encoded output file
Expand All @@ -244,15 +236,3 @@ def test_broken_pipe_error(self):
@support.requires_subprocess()
class TestTool(TestMain):
module = 'json.tool'
env_vars = {'PYTHONWARNINGS': 'ignore'}
warnings_expected = True

def test_with_warnings(self):
import json.tool
filename = json.tool.__file__
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', self.module, infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
self.assertRegex(err.decode(),
r'^.+/json/tool\.py:\d+: DeprecationWarning: The json.tool module is deprecated\n.+\n$')
Morty Proxy This is a proxified and sanitized view of the page, visit original site.