Can this be stopped from leaking memory?

Rob Hooft rob at hooft.net
Wed Jun 14 01:58:26 EDT 2000


John Grayson writes:

 JG> However, this leaves the file descriptor dangling (further reading of
 JG> the documentation reveals:

 JG> Important: the caller is responsible for closing the file argument,
 JG> if it was not None, even when an exception is raised. ...

 JG> So, this is probably better:

 JG> import imp

 JG> fn = "mod.pyc"

 JG> for i in range(0, 20000):
 JG>     fd = open(fn, "rb")
 JG>     m = imp.load_module('Mod', fd, fn, (".pyc", "rb", imp.PY_COMPILED))
 JG>     fd.close()
 JG>     m.main()

Or, if you re-read the docs:

for i in range(0, 20000):
    fd = open(fn, "rb")
    try:
        m = imp.load_module('Mod', fd, fn, (".pyc", "rb", imp.PY_COMPILED))
    finally:
        fd.close()
    m.main()

Regards,

Rob Hooft
-- 
=====   rob at hooft.net          http://www.hooft.net/people/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========



More information about the Python-list mailing list