diff Python/_warnings.c @ 46637:a859f835ef53 legacy-trunk

Fix the C implementation of 'warnings' to infer the filename of the module that raised an exception properly when __file__ is not set, __name__ == '__main__', and sys.argv[0] is a false value. Closes issue2743.
author Brett Cannon <bcannon@gmail.com>
date Sat, 03 May 2008 03:19:39 +0000
parents cc66029d2efe
children d9071fa8244a
line wrap: on
line diff
--- a/Python/_warnings.c	Sat May 03 01:02:41 2008 +0000
+++ b/Python/_warnings.c	Sat May 03 03:19:39 2008 +0000
@@ -487,8 +487,21 @@
         if (module_str && strcmp(module_str, "__main__") == 0) {
             PyObject *argv = PySys_GetObject("argv");
             if (argv != NULL && PyList_Size(argv) > 0) {
+                int is_true;
                 *filename = PyList_GetItem(argv, 0);
                 Py_INCREF(*filename);
+                /* If sys.argv[0] is false, then use '__main__'. */
+                is_true = PyObject_IsTrue(*filename);
+                if (is_true < 0) {
+                    Py_DECREF(*filename);
+                    goto handle_error;
+                }
+                else if (!is_true) {
+                    Py_DECREF(*filename);
+                    *filename = PyString_FromString("__main__");
+                    if (*filename == NULL)
+                        goto handle_error;
+                }
             }
             else {
                 /* embedded interpreters don't have sys.argv, see bug #839151 */