-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Description
Feature or enhancement
When an object like socket.socket is used in with statement, NotImplementedError should be risen.
Pitch
The standard library has some classes that implement thin low-level wrappers but look and feel like manageable resources. For example, socket.socket has the same semantics as a file stream so a library user can conclude that an ability to use a file in with means that every openable I/O stream may be used in such a way.
However, it leads to hard to track bugs. A default behavior of __exit__ is to do nothing so we get untraceable resource leaks or stream deadlocks. For example, a server thread needs to close all of its sockets if an exception occurs (otherwise there will be a lonely server socket with a hanged client and a leaked owner thread).
According to documentation, the issue is solved by wrapping such an object into contextlib.closing but it comes in a hard postfactum way.
Anectotally, I was backlashed by this mismatch of a mental model trying to hunt down unstable tests in gh-92475 for some fruitless weeks.