I have a question about this on testing the following code:
1,
def file_close_test():
f = open('/tmp/test', 'w+')
if __name__ == '__main__':
file_close_test()
# wait to see whether file closed.
import time
time.sleep(30)
2,
def file_close_on_exc_test():
f = open('/tmp/test', 'w+')
raise Exception()
def exception_wrapper():
try:
file_close_on_exc_test()
except:
pass
# wait to see whether file closed.
import time
time.sleep(10)
if __name__ == '__main__':
exception_wrapper()
import time
time.sleep(30)
- The file object closed when the file_close_test exits because no reference to it.
- After the exception raised,the file object not closed.so i think the related stack data not released.
- After exception_wrapper exit,the file closed automatically.
can you explain this for me? thanks.