New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
subprocess should include filename in FileNotFoundError exception #66726
Comments
FileNotFoundError should contain a 'filename' information, as per its specification. It's 'None' after a failure to execute a subprocess. |
The attached patch includes the first element in args in _execute_child to the OSError exception subclass. This correctly populates the 'filename' field on the resulting exception. A test is also included that fails without the patch. |
I can confirm that without the patch the filename attribute is None Travis, you should use subprocess.call("exit 0", shell=True, executable='/nonexistent bash') case. And use It seems appropriate to set filename even if errno is not ENOENT |
If the_oserror.filename is not None then str(the_oserror) appends the [Errno 2] No such file or directory: 'nonexistent': 'nonexistent' You could remove |
You should only add the filename if the error if a FileNotFound exception, not for any OSError, and only if exec() was called. It looks like the variable child_exec_never_called indicates if exec() was called. |
It would be inconsitent to provide filename only if exec is called e.g.: >>> import subprocess
subprocess.call("not used", cwd="nonexistent")
FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent' The error message shows the filename (cwd) despite exec not being If we ignore the backward compatibility issue I've mentioned in msg231297 if errno_num == errno.ENOENT:
if child_exec_never_called:
# The error must be from chdir(cwd).
err_msg += ': ' + repr(cwd)
else:
err_msg += ': ' + repr(orig_executable)
raise child_exception_type(errno_num, err_msg) could be replaced with:
[1] https://hg.python.org/cpython/file/23ab1197df0b/Lib/subprocess.py#l1443 |
I wnated to say that args[0] is not the right filename if exec() was not |
Thank you all for the helpful comments. A revised attempt is attached as -2.patch, with improved behavior around using cwd if the child is never called and orig_executable if it is. I opted not to fix the issue with the redundancy in the error message as I agree that should be handled as a separate issue. |
I was also bitten by this bug, and would like to see it merged. The patch 22536-subprocess-exception-filename-2.patch looks fine to me. |
Your change makes test_subprocess f1iling on Windows. Example: |
right, the test needs to be excluded there. fixing... (bummer windows wasn't in the CI) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: