1
0
mirror of https://github.com/django/django.git synced 2025-03-06 07:22:32 +00:00

[5.2.x] Refs #35515 -- Refactored internal get_and_report_namespace in the shell command.

Backport of 44ccd20375ba0d4da869ef994bc10a2311e9dc88 from main.
This commit is contained in:
Natalia 2025-02-07 14:18:54 -03:00
parent 0125bc15d4
commit 7b0c587a68
2 changed files with 16 additions and 20 deletions

View File

@ -54,27 +54,18 @@ class Command(BaseCommand):
def ipython(self, options):
from IPython import start_ipython
start_ipython(
argv=[],
user_ns=self.get_and_report_namespace(
options["verbosity"], options["no_imports"]
),
)
start_ipython(argv=[], user_ns=self.get_and_report_namespace(**options))
def bpython(self, options):
import bpython
bpython.embed(
self.get_and_report_namespace(options["verbosity"], options["no_imports"])
)
bpython.embed(self.get_and_report_namespace(**options))
def python(self, options):
import code
# Set up a dictionary to serve as the environment for the shell.
imported_objects = self.get_and_report_namespace(
options["verbosity"], options["no_imports"]
)
imported_objects = self.get_and_report_namespace(**options)
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
@ -127,12 +118,13 @@ class Command(BaseCommand):
# Start the interactive interpreter.
code.interact(local=imported_objects)
def get_and_report_namespace(self, verbosity, no_imports=False):
if no_imports:
def get_and_report_namespace(self, **options):
if options and options.get("no_imports"):
return {}
namespace = self.get_namespace()
verbosity = options["verbosity"] if options else 0
if verbosity < 1:
return namespace

View File

@ -85,13 +85,14 @@ class ShellCommandTestCase(SimpleTestCase):
def test_ipython(self):
cmd = shell.Command()
mock_ipython = mock.Mock(start_ipython=mock.MagicMock())
options = {"verbosity": 0, "no_imports": False}
with mock.patch.dict(sys.modules, {"IPython": mock_ipython}):
cmd.ipython({"verbosity": 0, "no_imports": False})
cmd.ipython(options)
self.assertEqual(
mock_ipython.start_ipython.mock_calls,
[mock.call(argv=[], user_ns=cmd.get_and_report_namespace(0))],
[mock.call(argv=[], user_ns=cmd.get_and_report_namespace(**options))],
)
@mock.patch("django.core.management.commands.shell.select.select") # [1]
@ -106,12 +107,14 @@ class ShellCommandTestCase(SimpleTestCase):
def test_bpython(self):
cmd = shell.Command()
mock_bpython = mock.Mock(embed=mock.MagicMock())
options = {"verbosity": 0, "no_imports": False}
with mock.patch.dict(sys.modules, {"bpython": mock_bpython}):
cmd.bpython({"verbosity": 0, "no_imports": False})
cmd.bpython(options)
self.assertEqual(
mock_bpython.embed.mock_calls, [mock.call(cmd.get_and_report_namespace(0))]
mock_bpython.embed.mock_calls,
[mock.call(cmd.get_and_report_namespace(**options))],
)
@mock.patch("django.core.management.commands.shell.select.select") # [1]
@ -126,13 +129,14 @@ class ShellCommandTestCase(SimpleTestCase):
def test_python(self):
cmd = shell.Command()
mock_code = mock.Mock(interact=mock.MagicMock())
options = {"verbosity": 0, "no_startup": True, "no_imports": False}
with mock.patch.dict(sys.modules, {"code": mock_code}):
cmd.python({"verbosity": 0, "no_startup": True, "no_imports": False})
cmd.python(options)
self.assertEqual(
mock_code.interact.mock_calls,
[mock.call(local=cmd.get_and_report_namespace(0))],
[mock.call(local=cmd.get_and_report_namespace(**options))],
)
# [1] Patch select to prevent tests failing when the test suite is run