gc.garbage from paramiko
Martin Pool
mbp at sourcefrog.net
Tue Feb 7 21:17:41 GMT 2006
On 7 Feb 2006, Robey Pointer <robey at lag.net> wrote:
> I guess that was kind of vague, sorry. :) Python borrows a concept
> from c++, and causes files & sockets to auto-close themselves as soon
> as the last reference goes away. Since paramiko supplies both socket-
> like things (Channels) and file-like things (SFTPFiles), I've found
> that python coders expect them to have the same del behavior as
> builtins, allowing things like this:
>
> open('email.txt', 'w').write('jdoe at example.com')
Of course this statement is not really safe, and shouldn't be used in
serious code. There's no guarantee that the file will be closed or its
buffers flushed.
I do write this myself all the time in throwaway scripts, and have
never seen it fail in cPython 2.4.
The only really safe way to do it is with a finally block that closes
the file, and this is what we should do with sftp too.
> >Cycles are really, really hard to avoid. If a traceback occurs,
> >that can create
> >cycles. There's lots of ways to unexpectedly make a cycle.
>
> How could a traceback create a cycle?
The traceback contains an object representing the stack, which contains
a copy of all the local variables. This will cause additional
references to objects, and might cause them to be retained for longer
than you would expect. I'm not sure how it would cause a cycle passing
through the file-like object, unless for some reason it saved the
exception info within itself?
--
Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060208/99252bb3/attachment.pgp
More information about the bazaar
mailing list