Post details: Python Exception Weirdness ?
2005-08-12
Python Exception Weirdness ?
Why Python does not recognize RDF.RedlandError exception in the following code:
try:
parser = RDF.Parser('raptor')
for s in parser.parse_string_as_stream(data, "http://something"):
if (s.predicate == seeAlso and s.object.is_resource()):
result.append(str(s.object.uri))
except (KeyboardInterrupt, MemoryError):
raise
except RDF.RedlandError:
print " : Caught Redland Exception : " , sys.exc_type
except Exception:
print " : Caught Exception :" , sys.exc_type
What is weird - the exception gets caught in "except Exception" clause and is shown as being RDF.RedlandError in the output. Why does Python behave that strange?
: Caught Exception : RDF.RedlandError
Comments, Pingbacks:
Comment from: Christopher Schmidt [Visitor]
Huh. I seem to remember trying to catch RedlandErrors in the past, but I don't know if I ever managed to succeed or what. I do agree that the code sample you have here doesn't work though.
Hi, Chris!
Yes, it does not. Thanks for confirmation. Had I not had XML errors in test data, I'd have thought it catches RedlandErrors all right.
It is disturbing that there is nothing in the source code [that I can think of] that could be causing this error. Which means I'll leave it as is, unless someone can suggest what's causing this.
I have seen other weirdness in Python before - e.g. - I was using it as a simple calculator until I found out that even in simple calculations like 1+1 (don't remember what I was calculating then, but something simple) it will give a wrong result like 2.0001.
Yes, it does not. Thanks for confirmation. Had I not had XML errors in test data, I'd have thought it catches RedlandErrors all right.
It is disturbing that there is nothing in the source code [that I can think of] that could be causing this error. Which means I'll leave it as is, unless someone can suggest what's causing this.
I have seen other weirdness in Python before - e.g. - I was using it as a simple calculator until I found out that even in simple calculations like 1+1 (don't remember what I was calculating then, but something simple) it will give a wrong result like 2.0001.
Comment from: amk [Visitor] · http://www.amk.ca
When I've seen this kind of thing in the past, it's because the underlying module was imported twice through different paths, and there are therefore *two* copies of the class. To check this, print RDF.RedlandError and sys.exc_type; do the 'at 0x...' portions of the output match? I'll wager they don't.
A common cause of this is importing a module once in absolute form (from Redland import RDF) and once in relative form (import RDF).
The 1+1=2.001 (a good example is 2/5.0, which is 0.4000002) problem is just the nature of floating point numbers; there's not much to be done about it. You could use the Decimal type, but that's annoying when using it as a calculator.
A common cause of this is importing a module once in absolute form (from Redland import RDF) and once in relative form (import RDF).
The 1+1=2.001 (a good example is 2/5.0, which is 0.4000002) problem is just the nature of floating point numbers; there's not much to be done about it. You could use the Decimal type, but that's annoying when using it as a calculator.
Comment from: Christopher Schmidt [Visitor]
Printing out repr(RDF.RedlandError) and sys.exc_type do return different things - however, I can't find anything in a small (10 line code sample) that would seem to cause this.
import sys
import RDF
try:
parser = RDF.Parser('raptor')
for s in parser.parse_as_stream(RDF.Uri("http://crschmidt.net"),
RDF.Uri("http://something")):
if (s.predicate == seeAlso and s.object.is_resource()):
result.append(str(s.object.uri))
except RDF.RedlandError:
print " : Caught Redland Exception : " , sys.exc_type
except Exception, E:
print repr(RDF.RedlandError)
print repr(sys.exc_type)
print " : Caught Exception : %s, %s, %s" % (sys.exc_type, type(E), E)
Thoughts?
Comment from: amk [Visitor] · http://www.amk.ca
What version of Redland? For the version I have installed (0.9.14, I think), the script prints:
Redland may have rearranged its internal imports and caused this problem. (If my theory of relative vs. absolute imports is correct, then this *must* be a Redland bug.)
Use of deprecated SAXv1 function internalSubset
/usr/lib/python2.3/site-packages/RDF.py:1445: RuntimeWarning: URI http://something:3 - Raptor error -Document element rdf:RDF missing.
uri._reduri, base_uri._reduri)
URI http://crschmidt.net raptor www error - Failed writing body
Redland may have rearranged its internal imports and caused this problem. (If my theory of relative vs. absolute imports is correct, then this *must* be a Redland bug.)
Comment from: Nick. [Visitor] · http://bashregion.com/blog/
Thank for the input!
This post has 6 feedbacks awaiting moderation...
Leave a comment:
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | |||
Search
Gallery
www.flickr.com
|
Categories
Archives
- February 2011 (1)
- September 2010 (1)
- October 2009 (1)
- March 2009 (2)
- February 2009 (4)
- January 2009 (2)
- December 2008 (2)
- November 2008 (5)
- October 2008 (10)
- August 2008 (1)
- July 2008 (4)
- June 2008 (1)
- More...
- more...

