unix/modsocket: Set file descriptor to -1 on close.

After s.close(), s.fileno() now returns -1, matching CPython behavior.
Some code relies on this compatibility, as it allows checking whether
a socket is closed by testing its fileno() value. This change ensures
better interoperability with existing Python code and libraries.

Signed-off-by: Mike Wang <mikewang000000@gmail.com>
This commit is contained in:
Mike Wang
2025-09-21 01:44:59 +08:00
committed by Damien George
parent a277fe4a36
commit cf097932a2
2 changed files with 23 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
# Test socket.fileno() functionality
try:
import socket
except ImportError:
print("SKIP")
raise SystemExit
if not hasattr(socket.socket, "fileno"):
print("SKIP")
raise SystemExit
s = socket.socket()
print(s.fileno() >= 0)
s.close()
print(s.fileno()) # should print -1