3 ms·
well, the nested try: ... finally: gave you something to think about: why do you have a "sock" object that you need to explicitly close "manually"? I didn't lo
by _ZeD_ 5y ago
well, the nested try: ... finally: gave you something to think about:
why do you have a "sock" object that you need to explicitly close "manually"?
I didn't look at the source, it can be for a number of reasons. But I suspect some long lived sock object shared between functions.
Something that could be wrapped in a context manager.
like
with MyCtxMgr() as my_ctx_mgr: # open the socket, init, etc...
my_ctx_mgr.do_stuff() # internally it has access
my_ctx_mgr.do_another_stuff() # to the opened socket
external_function(my_ctx_mgr.get_sock()) # and can pass it
# in the __exit__ actually close the socket, dealing with the OSError
calling a barebone "obj.close()" is a code smell IMHO