[issue36812] posix_spawnp returns error when used with file_actions

Toshio Kuratomi report at bugs.python.org
Wed May 8 08:52:52 EDT 2019


Toshio Kuratomi <a.badger at gmail.com> added the comment:

Yeah, I've verified what Victor said about the OS not giving us enough information to tell what file is causing the issue.  However, I wonder if we should change the error message to be less confusing?  I'm a godawful C programmer but maybe something like this:

-        PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
+        if (file_actionsp != NULL) {
+            /* OSErrors can be triggered by the program being invoked or by a
+             * problem with the files in file_actions.  Change the default
+             * error message so as not to confuse the programmer
+             */
+            if (path->narrow != NULL) {
+                char *err_msg_fmt = "While spawning %s\0";
+                unsigned int err_msg_size = strlen(path->narrow) + strlen(err_msg_fmt) + 1;
+                char* err_msg = malloc(err_msg_size);
+
+                PyOS_snprintf(err_msg, err_msg_size, err_msg_fmt, path->narrow);
+                /* Slight abuse, we're sending an error message rather than
+                 * a filename
+                 */
+                PyErr_SetFromErrnoWithFilename(PyExc_OSError, err_msg);
+            }
+        }
+        else
+        {
+            PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
+        }


Which leads to output like this:

>>> import os
>>> file_actions = [(os.POSIX_SPAWN_OPEN, 1, '.tmp/temp_file', os.O_CREAT | os.O_RDWR, 777)]
>>> os.posix_spawnp('whoami', ['whoami'], os.environ, file_actions=file_actions)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'While spawning whoami'


I can submit a PR for that and people can teach me how to fix my C if it's considered useful.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36812>
_______________________________________


More information about the Python-bugs-list mailing list