]> granicus.if.org Git - python/commitdiff
Close #18957: The PYTHONFAULTHANDLER environment variable now only enables the
authorVictor Stinner <victor.stinner@gmail.com>
Sun, 8 Sep 2013 09:36:23 +0000 (11:36 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sun, 8 Sep 2013 09:36:23 +0000 (11:36 +0200)
faulthandler module if the variable is non-empty. Same behaviour than other
variables like PYTHONDONTWRITEBYTECODE.

Doc/using/cmdline.rst
Lib/test/test_faulthandler.py
Misc/NEWS
Modules/faulthandler.c

index c14f6c76e002dec743dd3c9639e040e54732e08a..908a17c5aac3b8edd535bdeb04c143eeb493e0de 100644 (file)
@@ -511,9 +511,9 @@ conflict.
 
 .. envvar:: PYTHONDONTWRITEBYTECODE
 
-   If this is set, Python won't try to write ``.pyc`` or ``.pyo`` files on the
-   import of source modules.  This is equivalent to specifying the :option:`-B`
-   option.
+   If this is set to a non-empty string, Python won't try to write ``.pyc`` or
+   ``.pyo`` files on the import of source modules.  This is equivalent to
+   specifying the :option:`-B` option.
 
 
 .. envvar:: PYTHONHASHSEED
@@ -582,11 +582,11 @@ conflict.
 
 .. envvar:: PYTHONFAULTHANDLER
 
-   If this environment variable is set, :func:`faulthandler.enable` is called
-   at startup: install a handler for :const:`SIGSEGV`, :const:`SIGFPE`,
-   :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL` signals to dump the
-   Python traceback.  This is equivalent to :option:`-X` ``faulthandler``
-   option.
+   If this environment variable is set to a non-empty string,
+   :func:`faulthandler.enable` is called at startup: install a handler for
+   :const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and
+   :const:`SIGILL` signals to dump the Python traceback.  This is equivalent to
+   :option:`-X` ``faulthandler`` option.
 
    .. versionadded:: 3.3
 
index 4a8becfe38fb77bc2754d7e91b5007e9f9cde26c..d78bcb0a95f8a973f6d649f2fc3ab0b97990a132 100644 (file)
@@ -265,17 +265,33 @@ faulthandler._sigsegv()
         # By default, the module should be disabled
         code = "import faulthandler; print(faulthandler.is_enabled())"
         args = (sys.executable, '-E', '-c', code)
-        # use subprocess module directly because test.script_helper adds
-        # "-X faulthandler" to the command line
-        stdout = subprocess.check_output(args)
-        self.assertEqual(stdout.rstrip(), b"False")
+        # don't use assert_python_ok() because it always enable faulthandler
+        output = subprocess.check_output(args)
+        self.assertEqual(output.rstrip(), b"False")
 
     def test_sys_xoptions(self):
         # Test python -X faulthandler
         code = "import faulthandler; print(faulthandler.is_enabled())"
-        rc, stdout, stderr = assert_python_ok("-X", "faulthandler", "-c", code)
-        stdout = (stdout + stderr).strip()
-        self.assertEqual(stdout, b"True")
+        args = (sys.executable, "-E", "-X", "faulthandler", "-c", code)
+        # don't use assert_python_ok() because it always enable faulthandler
+        output = subprocess.check_output(args)
+        self.assertEqual(output.rstrip(), b"True")
+
+    def test_env_var(self):
+        # empty env var
+        code = "import faulthandler; print(faulthandler.is_enabled())"
+        args = (sys.executable, "-c", code)
+        env = os.environ.copy()
+        env['PYTHONFAULTHANDLER'] = ''
+        # don't use assert_python_ok() because it always enable faulthandler
+        output = subprocess.check_output(args, env=env)
+        self.assertEqual(output.rstrip(), b"False")
+
+        # non-empty env var
+        env = os.environ.copy()
+        env['PYTHONFAULTHANDLER'] = '1'
+        output = subprocess.check_output(args, env=env)
+        self.assertEqual(output.rstrip(), b"True")
 
     def check_dump_traceback(self, filename):
         """
index f97b504e7f21662ad64da5ccdfa996d496cd468c..128e740b746636efa2f278265c34c8d356e7f300 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -7,6 +7,13 @@ What's New in Python 3.4.0 Alpha 3?
 
 Projected Release date: 2013-10-XX
 
+Library
+-------
+
+- The :envvar:`PYTHONFAULTHANDLER` environment variable now only enables the
+  faulthandler module if the variable is non-empty. Same behaviour than other
+  variables like :envvar:`PYTHONDONTWRITEBYTECODE`.
+
 Tests
 -----
 
index 172945d1a40746b4ebfff37b99e81a1d3b254e59..47bc9e8d3b3ba9458c2ccc5716e243899558cb81 100644 (file)
@@ -1048,8 +1048,11 @@ faulthandler_env_options(void)
 {
     PyObject *xoptions, *key, *module, *res;
     _Py_IDENTIFIER(enable);
+    char *p;
 
-    if (!Py_GETENV("PYTHONFAULTHANDLER")) {
+    if (!((p = Py_GETENV("PYTHONFAULTHANDLER")) && *p != '\0')) {
+        /* PYTHONFAULTHANDLER environment variable is missing
+           or an empty string */
         int has_key;
 
         xoptions = PySys_GetXOptions();