فهرست منبع

Refs #35515 -- Refactored internal `get_and_report_namespace` in the shell command.

Natalia 1 ماه پیش
والد
کامیت
44ccd20375
2فایلهای تغییر یافته به همراه16 افزوده شده و 20 حذف شده
  1. 6 14
      django/core/management/commands/shell.py
  2. 10 6
      tests/shell/tests.py

+ 6 - 14
django/core/management/commands/shell.py

@@ -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
 

+ 10 - 6
tests/shell/tests.py

@@ -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