1
0
mirror of https://github.com/django/django.git synced 2025-03-06 15:32:33 +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): def ipython(self, options):
from IPython import start_ipython from IPython import start_ipython
start_ipython( start_ipython(argv=[], user_ns=self.get_and_report_namespace(**options))
argv=[],
user_ns=self.get_and_report_namespace(
options["verbosity"], options["no_imports"]
),
)
def bpython(self, options): def bpython(self, options):
import bpython import bpython
bpython.embed( bpython.embed(self.get_and_report_namespace(**options))
self.get_and_report_namespace(options["verbosity"], options["no_imports"])
)
def python(self, options): def python(self, options):
import code import code
# Set up a dictionary to serve as the environment for the shell. # Set up a dictionary to serve as the environment for the shell.
imported_objects = self.get_and_report_namespace( imported_objects = self.get_and_report_namespace(**options)
options["verbosity"], options["no_imports"]
)
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py. # conventions and get $PYTHONSTARTUP first then .pythonrc.py.
@ -127,12 +118,13 @@ class Command(BaseCommand):
# Start the interactive interpreter. # Start the interactive interpreter.
code.interact(local=imported_objects) code.interact(local=imported_objects)
def get_and_report_namespace(self, verbosity, no_imports=False): def get_and_report_namespace(self, **options):
if no_imports: if options and options.get("no_imports"):
return {} return {}
namespace = self.get_namespace() namespace = self.get_namespace()
verbosity = options["verbosity"] if options else 0
if verbosity < 1: if verbosity < 1:
return namespace return namespace

View File

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