Ask Your Question
1

symbolic_expression produces invalid traceback (TypeError)

asked 2011-10-23 14:56:01 +0100

Hello all !

In the following code I consider

f=RealDistribution('chisquared',10).distribution_function

then I provoke and catch an AttributeError on f. No problems. Then I provoke and catch a TypeError with symbolic_expression(f). Once again no problem.

The fun part is that when I provoke the AttributeError and catch AttributeError and TypeError, the next TypeError will not be handled.

f=RealDistribution('chisquared',10).distribution_function

try:
    f.blah()
except AttributeError:
    pass

try:
    f=symbolic_expression(f)
except TypeError:
    print "ok"

print "Well, let's do that again"

try:
    f.blah()
except AttributeError,TypeError:
    pass

try:
    f=symbolic_expression(f)
except TypeError:
    print "ok"

ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (994, 0))

--------------------------------------------------------------------------- TypeError Traceback (most recent call last)

/home/moky/Documents_sources/Unif/Enseignement/pey/phystricks/<ipython console=""> in <module>()

/home/moky/Sage/local/lib/python2.6/site-packages/sage/misc/session.so in sage.misc.session.attach (sage/misc/session.c:1988)()

/home/moky/Sage/local/lib/python2.6/site-packages/sage/misc/preparser.pyc in load(filename, globals, attach)

1592 1593 if fpath.endswith('.py'): -> 1594 execfile(fpath, globals) 1595 elif fpath.endswith('.sage'): 1596 exec(preparse_file(open(fpath).read()) + "\n", globals)

/home/moky/Documents_sources/Unif/Enseignement/pey/phystricks/phystricksChiSquared.py in <module>() 19 20 try: ---> 21 f=symbolic_expression(f) 22 except TypeError: 23 print "ok"

/home/moky/Sage/local/lib/python2.6/site-packages/sage/calculus/all.pyc in symbolic_expression(x) 93 return vector(SR,x) 94 else: ---> 95 return SR(x) 96 97 import desolvers

/home/moky/Sage/local/lib/python2.6/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:7102)()

/home/moky/Sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (sage/structure/coerce_maps.c:3254)()

/home/moky/Sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (sage/structure/coerce_maps.c:3157)()

/home/moky/Sage/local/lib/python2.6/site-packages/sage/symbolic/ring.so in sage.symbolic.ring.SymbolicRing._element_constructor_ (sage/symbolic/ring.cpp:4377)()

TypeError:

Any idea to understand or fix this funny behaviour ?

Have a good night Laurent

edit retag flag offensive close merge delete

Comments

I got some more tries. This is in fact not sage related since I can reproduce it in plain python. Catching TypeError together with an AttributeError make the next TypeError not caught. a="text" and then print a/2 ...

Laurent Claessens gravatar imageLaurent Claessens ( 2011-10-23 15:00:24 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2011-10-24 11:42:29 +0100

kcrisman gravatar image

Well, you figured it out. Here is a reference - the Python tutorial section on this.

An except clause may name multiple exceptions as a parenthesized tuple, for example:

I think your syntax may have wanted to assign the error message to the variable TypeError... or something like that.

edit flag offensive delete link more

Comments

Right: using "except AttributeError, s" is the same as using "except AttributeError as s": "s" is used to determine the 'value' of the exception: it can contain the error code, the string with the actual error message, etc. So with "except AttributeError,TypeError", it tries to extract information like this from "TypeError", and it can't so it raises a new error to reflect that.

John Palmieri gravatar imageJohn Palmieri ( 2011-10-24 13:52:27 +0100 )edit
1

answered 2011-10-23 15:11:59 +0100

Changing

except AttributeError,TypeError:

to

except (AttributeError,TypeError):

solves the problem. And by the way it seems to works with all kind of exceptions.

Well, my problem is solved, sorry for the noise. (still I'm interested in the rational behind)

See you Laurent

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-10-23 14:56:01 +0100

Seen: 727 times

Last updated: Oct 24 '11