about semantic web, software architecture and life in general

Post details: Python Exception Weirdness ?

2005-08-12

Permalink 18:35:47, Categories: Semantic Web, Software Development   English (EU)

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.
PermalinkPermalink 2005-08-12 @ 21:57
Comment from: captsolo [Member] Email
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.
PermalinkPermalink 2005-08-12 @ 23:21
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.
PermalinkPermalink 2005-08-13 @ 15:01
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?
PermalinkPermalink 2005-08-22 @ 00:11
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:


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.)
PermalinkPermalink 2005-08-22 @ 17:29
Comment from: Nick. [Visitor] · http://bashregion.com/blog/
Thank for the input!
PermalinkPermalink 2006-10-11 @ 15:22

This post has 6 feedbacks awaiting moderation...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, a, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

captsolo weblog

See also:

May 2012
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      
Last comments

Search

Gallery

www.flickr.com
captsolo's items Go to captsolo's photostream

Misc

Syndicate this blog XML

powered by
b2evolution
Page served in 0.461 seconds

Valid XHTML 1.0! Valid CSS! Valid RSS! Valid Atom!