I don't think there is a silver bullet for your problem, but you can somehow solve it by:
explicitly calling method to dispose resources (your
close
method). The only drawback is, well, the explicitness.creating a thin wrapper for interactive interpreter, that registers instance's
close
method at exit usingatexit
module. Drawback is that all of your resources will be released usually later than you would like.creating helper functions (it's not hard to create them dynamically), that wraps usage of resources. It's not feasible if you need to call more functions with one resource. For example:
def do_something() with tempdir() as tmp: return original.do_something(tmp)
creating a solution that would hide resource handling. For example I don't care about TCP sockets, ssl, 301/302 redirection, opening certificate file etc., I just need to send a GET request over https using one specific certificate. Naturally it depends on problem you would like to solve.