]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-121016: Add test for `PYTHON_BASIC_REPL` envioronment variable (#121017)
authordevdanzin <74280297+devdanzin@users.noreply.github.com>
Wed, 26 Jun 2024 10:39:07 +0000 (07:39 -0300)
committerGitHub <noreply@github.com>
Wed, 26 Jun 2024 10:39:07 +0000 (10:39 +0000)
Lib/test/support/__init__.py
Lib/test/test_cmd_line.py
Lib/test/test_pyrepl/test_pyrepl.py

index 4b430f85e7175cd744e45d998ad43cb29b2c1757..dbea070929be9b57a0600b96df25265741adfffc 100644 (file)
@@ -2626,3 +2626,9 @@ def force_not_colorized(func):
                 if value is not None:
                     os.environ[key] = value
     return wrapper
+
+
+def initialized_with_pyrepl():
+    """Detect whether PyREPL was used during Python initialization."""
+    # If the main module has a __file__ attribute it's a Python module, which means PyREPL.
+    return hasattr(sys.modules["__main__"], "__file__")
index a9963bf89d2914cb1271cccd51f69f5bf413680a..ac99dc4f915f7dfa3a59c2956d0c9851f40b8e6f 100644 (file)
@@ -969,7 +969,7 @@ class CmdLineTest(unittest.TestCase):
         self.assertIn(expected.encode(), out)
 
     def test_python_basic_repl(self):
-        # Currently this only tests that the env var is set
+        # Currently this only tests that the env var is set. See test_pyrepl.test_python_basic_repl.
         code = "import os; print('PYTHON_BASIC_REPL' in os.environ)"
         expected = "True"
         rc, out, err = assert_python_ok('-c', code, PYTHON_BASIC_REPL='1')
index adc55f28f08a1e4e912b98a2aef5c38acf1a90e1..21a570d271dc847525dbd0f8bb015305174cc979 100644 (file)
@@ -862,6 +862,31 @@ class TestMain(TestCase):
         self.assertNotIn("Exception", output)
         self.assertNotIn("Traceback", output)
 
+    @force_not_colorized
+    def test_python_basic_repl(self):
+        env = os.environ.copy()
+        commands = ("from test.support import initialized_with_pyrepl\n"
+                    "initialized_with_pyrepl()\n"
+                    "exit()\n")
+
+        env.pop("PYTHON_BASIC_REPL", None)
+        output, exit_code = self.run_repl(commands, env=env)
+        if "can\'t use pyrepl" in output:
+            self.skipTest("pyrepl not available")
+        self.assertEqual(exit_code, 0)
+        self.assertIn("True", output)
+        self.assertNotIn("False", output)
+        self.assertNotIn("Exception", output)
+        self.assertNotIn("Traceback", output)
+
+        env["PYTHON_BASIC_REPL"] = "1"
+        output, exit_code = self.run_repl(commands, env=env)
+        self.assertEqual(exit_code, 0)
+        self.assertIn("False", output)
+        self.assertNotIn("True", output)
+        self.assertNotIn("Exception", output)
+        self.assertNotIn("Traceback", output)
+
     def run_repl(self, repl_input: str | list[str], env: dict | None = None) -> tuple[str, int]:
         master_fd, slave_fd = pty.openpty()
         process = subprocess.Popen(