This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: os.stat(fd) crashes on Windows if fd does not exist
Type: crash Stage: resolved
Components: Versions: Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, larry, python-dev, sbt
Priority: high Keywords:

Created on 2012-07-06 00:03 by sbt, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg164705 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-07-06 00:03
In Python 3.3 (but not earlier) os.stat() is documented to work with file descriptors.  (os.path.exists() also works with fds since it is implemented in terms of os.stat(), although that is *not* documented.)

However, on Windows if fd is not open then os.stat(fd) triggers an assertion error or crash:

  File: f:\dd\vctools\crt_bld\self_x86\crt\src\osfinfo.c
  Line: 316
  Expression: (_osfile(fh) & FOPEN)

Note that os.fstat() fails on Windows with OSError(EBADF, ...).
msg164706 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-07-06 00:10
This can probably be fixed by using _PyVerify_fd().
msg164717 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-07-06 07:49
Many os functions started to accept file descriptors. I don't know how many are available on Windows, but IMO _PyVerify_fd() could be used for all of them; it's a no-op macro on Unix anyway.
msg164719 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-07-06 11:23
New changeset 62b9bfbc3356 by Richard Oudkerk in branch 'default':
Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
msg164720 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-07-06 11:36
> Many os functions started to accept file descriptors.  
> I don't know how many are available on Windows...

On Windows os.stat() seems to be the only one:

  >>> os.supports_fd
  {<built-in function stat>}
msg164723 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-06 14:16
The 62b9bfbc3356 changeset does more than add the guard against invalid file handles; it also adds documentation to os.path.exists documenting that it now accepts path-as-int-fd.  While this modification is fine in principle I would have preferred it was submitted separately.
msg164732 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-07-06 15:55
Sorry about that...
Date User Action Args
2022-04-11 14:57:32adminsetgithub: 59466
2012-07-06 15:55:38sbtsetmessages: + msg164732
2012-07-06 14:16:55larrysetmessages: + msg164723
2012-07-06 12:45:27sbtsetstatus: open -> closed
resolution: fixed
stage: needs patch -> resolved
2012-07-06 11:36:25sbtsetmessages: + msg164720
2012-07-06 11:23:42python-devsetnosy: + python-dev
messages: + msg164719
2012-07-06 07:49:39amaury.forgeotdarcsetnosy: + amaury.forgeotdarc, larry
messages: + msg164717
2012-07-06 00:10:01sbtsetmessages: + msg164706
2012-07-06 00:03:25sbtcreate