home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / spyceException.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  3.8 KB  |  117 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: spyceException.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. __doc__ = '''Various Spyce-related exceptions'''
  10.  
  11. import sys, string
  12. import spyceCompile, spyceUtil
  13.  
  14. ##################################################
  15. # Syntax errors
  16. #
  17.  
  18. class pythonSyntaxError:
  19.   "Generate string out of current pythonSyntaxError exception"
  20.   def __repr__(self):
  21.     return self.str
  22.   def __init__(self, spycewrap):
  23.     self.str = ''
  24.     type, error, _ = sys.exc_info()
  25.     if type is type(SyntaxError):
  26.       raise 'instantiate pythonSyntaxError only when SyntaxError raised: %s' % `type`
  27.     if spycewrap.getCodeRefs().has_key(error.lineno):
  28.       begin, end, text, filename = spycewrap.getCodeRefs()[error.lineno]
  29.       if begin[0]==end[0]:
  30.         linestr = str(begin[0])
  31.       else:
  32.         linestr = '%d-%d' % (begin[0], end[0])
  33.       self.str = 'Python syntax error at %s:%s - %s\n  %s\n' % (filename, linestr, error.msg, text)
  34.     else:
  35.       self.str = spyceUtil.exceptionString()
  36.  
  37. class spyceSyntaxError:
  38.   "Generate string out of current spyceSyntaxError exception"
  39.   def __init__(self, msg, info=None):
  40.     self.msg = msg
  41.     self.info = info
  42.   def __repr__(self):
  43.     s = 'Spyce syntax error'
  44.     if self.info:
  45.       (begin, _), (end, _), text, filename = self.info
  46.       if begin==end:
  47.         linestr = str(begin)
  48.       else:
  49.         linestr = '%d-%d' % (begin, end)
  50.       s = s + ' at %s:%s - %s\n  %s\n' % (filename, linestr, self.msg, text)
  51.     else:
  52.       s = s + ': '+self.msg
  53.     return s
  54.  
  55. ##################################################
  56. # Runtime errors
  57. #
  58.  
  59. class spyceRuntimeException:
  60.   "Generate string out of current SpyceException exception."
  61.   # useful fields: str, type, value, traceback, msg
  62.   def __repr__(self):
  63.     return self.str
  64.   def __init__(self, spycewrap=None):
  65.     import traceback, string
  66.     e1, e2, tb = sys.exc_info()
  67.     tb = traceback.extract_tb(tb)
  68.     self.str = ''
  69.     self.type, self.value, self.traceback = e1, e2, tb
  70.     if e1 == spyceRuntimeException:
  71.       self.msg = str(e2)
  72.     else:
  73.       self.msg = string.join(traceback.format_exception_only(e1, e2))
  74.     for i in range(len(tb)):
  75.       filename, lineno, funcname, text = tb[i]
  76.       if filename == '<string>' and spycewrap and spycewrap.getCodeRefs().has_key(lineno):
  77.         if funcname == spyceCompile.SPYCE_PROCESS_FUNC:
  78.           funcname = '(main)'
  79.         begin, end, text, filename = spycewrap.getCodeRefs()[lineno]
  80.         if begin[0]==end[0]:
  81.           lineno = str(begin[0])
  82.         else:
  83.           lineno = '%d-%d' % (begin[0], end[0])
  84.       lineno=str(lineno)
  85.       tb[i] = filename, lineno, funcname, text
  86.     for i in range(len(tb)):
  87.       self.str = self.str + '  %s:%s, in %s: \n    %s\n' % tb[i]
  88.     self.str = self.str + self.msg
  89.  
  90. class spyceNotFound:
  91.   "Exception class to signal that Spyce file does not exist."
  92.   def __init__(self, file):
  93.     self.file = file
  94.   def __repr__(self):
  95.     return 'spyceNotFound exception: could not find "%s"' % self.file
  96.  
  97. class spyceForbidden:
  98.   "Exception class to signal that Spyce file has access problems."
  99.   def __init__(self, file):
  100.     self.file = file
  101.   def __repr__(self):
  102.     return 'spyceForbidden exception: could not read "%s"' % self.file
  103.  
  104. ##################################################
  105. # Special control-flow exceptions
  106. #
  107.  
  108. class spyceRedirect:
  109.   "Exception class to signal an internal redirect."
  110.   def __init__(self, filename):
  111.     self.filename = filename
  112.  
  113. class spyceDone:
  114.   "Exception class to immediately jump to the end of the spyceProcess method"
  115.   pass
  116.  
  117.