mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 19:20:22 +01:00
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:
@@ -140,9 +140,12 @@ static mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, i
|
||||
// The rationale MicroPython follows is that close() just releases
|
||||
// file descriptor. If you're interested to catch I/O errors before
|
||||
// closing fd, fsync() it.
|
||||
MP_THREAD_GIL_EXIT();
|
||||
close(self->fd);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
if (self->fd >= 0) {
|
||||
MP_THREAD_GIL_EXIT();
|
||||
close(self->fd);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
}
|
||||
self->fd = -1;
|
||||
return 0;
|
||||
|
||||
case MP_STREAM_GET_FILENO:
|
||||
|
||||
17
tests/extmod/socket_fileno.py
Normal file
17
tests/extmod/socket_fileno.py
Normal 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
|
||||
Reference in New Issue
Block a user