This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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)
History
Date User Action Args
2015-04-22 10:17:12mpaolinisetrecipients: + mpaolini, r.david.murray, docs@python, martin.panter, sjdrake
2015-04-22 10:17:12mpaolinisetmessageid: <1429697832.04.0.661077443795.issue23227@psf.upfronthosting.co.za>
2015-04-22 10:17:12mpaolinilinkissue23227 messages
2015-04-22 10:17:11mpaolinicreate