Coming from C, I think that it's generally a good programming practice
to make sure everything you create, closes; whether it's about a socket
or a file. This may not be the case with Python though. To be honest,
leaving this task to the garbage collector doesn't sound like a good
idea to me (since
Fredrik Lundh wrote:
"Bryan" wrote
the above is not the same. make the a = ... raise an exception and you'll see
the difference.
s = ... #
a = 1/0
s.close()
as you can see, s.close() will never be called. also, in this example, i intentionally didn't put
the extra try/except around the try/fina
"Bryan" wrote
> the above is not the same. make the a = ... raise an exception and you'll see
> the difference.
>
> s = ... #
> a = 1/0
> s.close()
>
> as you can see, s.close() will never be called. also, in this example, i
> intentionally didn't put
> the extra try/except around the try/fina
Dan Perl wrote:
"Bryan" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
IMO, that is not the reason for the try/finally statement and it is not
redundant. the try/finally statement guarantees the resource is closed
and the try/finally statement only gets executed if and only if the
"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Good point, but with your way, if "s = ... # socket opens" fails, then
> nothing will catch it. What I usually do is what I wrote above (place
> it below the 2nd try), and when attempting to close it, first use a
"Bryan" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> IMO, that is not the reason for the try/finally statement and it is not
> redundant. the try/finally statement guarantees the resource is closed
> and the try/finally statement only gets executed if and only if the
> openin
Good point, but with your way, if "s = ... # socket opens" fails, then
nothing will catch it. What I usually do is what I wrote above (place
it below the 2nd try), and when attempting to close it, first use an if
like: "if locals().has_key('s'):".
--
http://mail.python.org/mailman/listinfo/python
I need it because the "various code" may raise other exceptions (not
related to sockets). In such case, the "except socket.error, x:" won't
catch it, but thanks to the "finally:", it is sure that the socket will
close.
--
http://mail.python.org/mailman/listinfo/python-list
Dan Perl wrote:
"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
Thanks. This should now be OK:
#try:
#try:
#s = ... # socket opens
#
## various code ...
#except socket.error, x:
## exception handling
#finally:
#s.close() # soc
"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Thanks. This should now be OK:
>
> #try:
> #try:
> #s = ... # socket opens
> #
> ## various code ...
> #except socket.error, x:
> ## exception handling
> #finally:
> #s.close()
Thanks. This should now be OK:
#try:
#try:
#s = ... # socket opens
#
## various code ...
#except socket.error, x:
## exception handling
#finally:
#s.close() # socket closes
--
http://mail.python.org/mailman/listinfo/python-list
Aggelos I. Orfanakos wrote:
> (I don't know why, but indentation was not preserved once I posted.)
This problem and its possible solutions was discussed here in the
thread "OT: spacing of code in Google Groups".
--
http://mail.python.org/mailman/listinfo/python-list
(I don't know why, but indentation was not preserved once I posted.)
--
http://mail.python.org/mailman/listinfo/python-list
Hello.
In a program, I want to ensure that a socket closes (so I use try ...
finally), but I also want to catch/handle a socket exception. This is
what I have done:
try:
try:
s = ... # socket opens
# various code ...
except socket.error, x:
# exception handling
finally:
s.close() # socket closes
14 matches
Mail list logo