Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38142 > unrolled thread

error in except

Started byRodrick Brown <rodrick.brown@gmail.com>
First post2013-02-04 16:49 -0500
Last post2013-02-04 17:37 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  error in except Rodrick Brown <rodrick.brown@gmail.com> - 2013-02-04 16:49 -0500
    Re: error in except Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 09:21 +1100
      Re: error in except John Evans <john.g.evans.ne@gmail.com> - 2013-02-04 17:37 -0500

#38142 — error in except

FromRodrick Brown <rodrick.brown@gmail.com>
Date2013-02-04 16:49 -0500
Subjecterror in except
Message-ID<mailman.1335.1360014616.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

For the life of me I cant figure out why this exception is being thrown.
How could I use pdb to debug this?

$ python udp_local2.py server
  File "udp_local2.py", line 36
    except:
         ^
SyntaxError: invalid syntax


#!/usr/bin/env python

import random, socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

MAX = 65535
PORT = 1060

if 2 <= len(sys.argv) <= 3 and sys.argv[1] == 'server':
    interface = sys.argv[2] if len(sys.argv) > 2 else ''
    s.bind((interface, PORT))
    print 'Listening at', s.getsockname()
    while True:
        data, address = s.recvfrom(MAX)
        if random.randint(0, 1):
            print 'The client at', address, 'says:', repr(data)
            s.sendto('Your data was %d bytes' % len(data), address)
        else:
            print 'Pretending to drop packet from', address

elif len(sys.argv) == 3 and sys.argv[1] == 'client':
    hostname = sys.argv[2]
    s.connect((hostname, PORT))
    print 'Client socket name is', s.getsockname()
    delay = 0.1
    while True:
        s.send('This is another message')
        print 'Waiting up to', delay, 'seconds for a reply'
        s.settimeout(delay)
        try:
            data = s.recv(MAX)
        except socket.timeout:
            delay *= 2
            if delay > 2.0:
                raise RuntimeError('I think the server is down')
            except:
                raise
            else:
                break
        print 'The server says', repr(data)
    else:
        print >> sys.stderr, 'usage: %d server [<interfae>]' % sys.argv[0]
        print >> sys.stderr, '   or: %d client <host>' % sys.argv[0]
        sys.exit(2)

[toc] | [next] | [standalone]


#38145

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-02-05 09:21 +1100
Message-ID<51103461$0$29981$c3e8da3$5496439d@news.astraweb.com>
In reply to#38142
Rodrick Brown wrote:

> For the life of me I cant figure out why this exception is being thrown.
> How could I use pdb to debug this?
> 
> $ python udp_local2.py server
>   File "udp_local2.py", line 36
>     except:
>          ^
> SyntaxError: invalid syntax

You can't use pdb to debug it, because you can't run the code until you fix
the syntax error. You use your text editor to debug it.

Sometimes if you have a missing bracket (round, square or curly), Python
reports the syntax error on the line *after* where it expected the closing
bracket.

I've also seen unexpected syntax errors if the source code contains binary
characters such as DOS end-of-file ^Z. Try opening the file in a hex editor
and looking for anything that shouldn't be there.

But the most likely problem is that you are mixing tabs and spaces, and
consequently have inadvertently become confused about the indent level. You
think that the "except" clause is indented level with a try, but it
actually is indented level with something else. Using spaces for indents is
good; using tabs for indents is also good; using both at the same time is a
nightmare. (Python 3 prohibits this.)

I recommend running TabNanny over the file:

python -m tabnanny <file-or-directory>


A couple of comments on your code:

> try:
>     data = s.recv(MAX)
> except socket.timeout:
>     delay *= 2
>     if delay > 2.0:
>         raise RuntimeError('I think the server is down')

Five attempts and a total of 3.1 seconds (0.1 + 0.2 + 0.4 + 0.8 + 1.6) is
rather short to conclude that a server is down, although it depends on what
sort of server and where it is. I would have thought 30 seconds is more
appropriate. (By default, wget doesn't time out for 3 minutes, which is
possibly overkill.)

But either way, I don't think RuntimeError is the right exception to use. I
expect that a socket error would be more relevant.

> except:
>     raise

What this does is:

"Unconditionally catch anything. Then raise it again."

Don't do this. The right thing to do here is, just delete it and don't catch
it at all.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#38146

FromJohn Evans <john.g.evans.ne@gmail.com>
Date2013-02-04 17:37 -0500
Message-ID<mailman.1337.1360017438.2939.python-list@python.org>
In reply to#38145

[Multipart message — attachments visible in raw view] — view raw

Should it not be "try-except-else' instead of 'if-except-else'?

            try:
                if delay > 2.0:
                    raise RuntimeError('I think the server is down')
            except:
                raise
            else:
                break


On Mon, Feb 4, 2013 at 5:21 PM, Steven D'Aprano <
steve+comp.lang.python@pearwood.info> wrote:

> Rodrick Brown wrote:
>
> > For the life of me I cant figure out why this exception is being thrown.
> > How could I use pdb to debug this?
> >
> > $ python udp_local2.py server
> >   File "udp_local2.py", line 36
> >     except:
> >          ^
> > SyntaxError: invalid syntax
>
> You can't use pdb to debug it, because you can't run the code until you fix
> the syntax error. You use your text editor to debug it.
>
> Sometimes if you have a missing bracket (round, square or curly), Python
> reports the syntax error on the line *after* where it expected the closing
> bracket.
>
> I've also seen unexpected syntax errors if the source code contains binary
> characters such as DOS end-of-file ^Z. Try opening the file in a hex editor
> and looking for anything that shouldn't be there.
>
> But the most likely problem is that you are mixing tabs and spaces, and
> consequently have inadvertently become confused about the indent level. You
> think that the "except" clause is indented level with a try, but it
> actually is indented level with something else. Using spaces for indents is
> good; using tabs for indents is also good; using both at the same time is a
> nightmare. (Python 3 prohibits this.)
>
> I recommend running TabNanny over the file:
>
> python -m tabnanny <file-or-directory>
>
>
> A couple of comments on your code:
>
> > try:
> >     data = s.recv(MAX)
> > except socket.timeout:
> >     delay *= 2
> >     if delay > 2.0:
> >         raise RuntimeError('I think the server is down')
>
> Five attempts and a total of 3.1 seconds (0.1 + 0.2 + 0.4 + 0.8 + 1.6) is
> rather short to conclude that a server is down, although it depends on what
> sort of server and where it is. I would have thought 30 seconds is more
> appropriate. (By default, wget doesn't time out for 3 minutes, which is
> possibly overkill.)
>
> But either way, I don't think RuntimeError is the right exception to use. I
> expect that a socket error would be more relevant.
>
> > except:
> >     raise
>
> What this does is:
>
> "Unconditionally catch anything. Then raise it again."
>
> Don't do this. The right thing to do here is, just delete it and don't
> catch
> it at all.
>
>
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
John Evans

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web