Message241794
| Author |
mpaolini |
| Recipients |
docs@python, martin.panter, mpaolini, r.david.murray, sjdrake |
| Date |
2015年04月22日.10:17:11 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1429697832.04.0.661077443795.issue23227@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
I think there is an issue in the way you designed your cleanup logic. So I think this issue is invalid.
Usually, the code (funcion, class, ...) that *opens* the file should also be resposible of closing it.
option 1) the caller opens and closes the file and wrapping the logged lines in a try/finally
def logged_lines(f):
try:
for line in f:
logging.warning(line.strip())
yield line
finally:
logging.warning('closing')
f = open('yyy', 'r')
try:
for l in logged_lines(f):
print(l)
finally:
f.close()
option 2) the funcion opens and closes the file
def logged_lines(fname):
f = open('yyy', 'r')
try:
for line in f:
logging.warning(line.strip())
yield line
finally:
logging.warning('closing')
f.close()
for l in logged_lines('yyy'):
print(l) |
|