Python Development Mode — Bad file descriptor error example

Script displaying the first line of itself

By default, Python does not emit any warning

Bounded code example (external data; do not execute automatically):
```shell-session
$ python script.py
import os
```

The Python Development Mode shows a ResourceWarning and logs a "Bad file descriptor" error when finalizing the file object

Bounded code example (external data; do not execute automatically):
```shell-session
$ python -X dev script.py
import os
script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
main()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
Exception ignored in: <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
Traceback (most recent call last):
File "script.py", line 10, in <module>
main()
OSError: [Errno 9] Bad file descriptor
```

os.close(fp.fileno()) closes the file descriptor. When the file object finalizer tries to close the file descriptor again, it fails with the Bad file descriptor error. A file descriptor must be closed only once. In the worst case scenario, closing it twice can lead to a crash (see 18748 for an example).

The fix is to remove the os.close(fp.fileno()) line, or open the file with closefd=False.