home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / warnings.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  8.4 KB  |  297 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import sys
  5. import re
  6. import types
  7. __all__ = [
  8.     'warn',
  9.     'showwarning',
  10.     'formatwarning',
  11.     'filterwarnings',
  12.     'resetwarnings']
  13. defaultaction = 'default'
  14. filters = []
  15. onceregistry = { }
  16.  
  17. def warn(message, category = None, stacklevel = 1):
  18.     if category is None:
  19.         category = UserWarning
  20.     
  21.     
  22.     try:
  23.         caller = sys._getframe(stacklevel)
  24.     except ValueError:
  25.         globals = sys.__dict__
  26.         lineno = 1
  27.  
  28.     globals = caller.f_globals
  29.     lineno = caller.f_lineno
  30.     if globals.has_key('__name__'):
  31.         module = globals['__name__']
  32.     else:
  33.         module = '<string>'
  34.     filename = globals.get('__file__')
  35.     if filename:
  36.         fnl = filename.lower()
  37.         if fnl.endswith('.pyc') or fnl.endswith('.pyo'):
  38.             filename = filename[:-1]
  39.         
  40.     elif module == '__main__':
  41.         filename = sys.argv[0]
  42.     
  43.     if not filename:
  44.         filename = module
  45.     
  46.     registry = globals.setdefault('__warningregistry__', { })
  47.     warn_explicit(message, category, filename, lineno, module, registry)
  48.  
  49.  
  50. def warn_explicit(message, category, filename, lineno, module = None, registry = None):
  51.     if module is None:
  52.         module = filename
  53.         if module[-3:].lower() == '.py':
  54.             module = module[:-3]
  55.         
  56.     
  57.     if registry is None:
  58.         registry = { }
  59.     
  60.     key = (message, category, lineno)
  61.     if registry.get(key):
  62.         return None
  63.     
  64.     for item in filters:
  65.         (action, msg, cat, mod, ln) = item
  66.         if msg.match(message) and issubclass(category, cat) and mod.match(module) and ln == 0 or lineno == ln:
  67.             break
  68.         
  69.     else:
  70.         action = defaultaction
  71.     if action == 'ignore':
  72.         registry[key] = 1
  73.         return None
  74.     
  75.     if action == 'error':
  76.         raise category(message)
  77.     
  78.     if action == 'once':
  79.         registry[key] = 1
  80.         oncekey = (message, category)
  81.         if onceregistry.get(oncekey):
  82.             return None
  83.         
  84.         onceregistry[oncekey] = 1
  85.     elif action == 'always':
  86.         pass
  87.     elif action == 'module':
  88.         registry[key] = 1
  89.         altkey = (message, category, 0)
  90.         if registry.get(altkey):
  91.             return None
  92.         
  93.         registry[altkey] = 1
  94.     elif action == 'default':
  95.         registry[key] = 1
  96.     else:
  97.         raise RuntimeError('Unrecognized action (%s) in warnings.filters:\n %s' % (`action`, str(item)))
  98.     showwarning(message, category, filename, lineno)
  99.  
  100.  
  101. def showwarning(message, category, filename, lineno, file = None):
  102.     if file is None:
  103.         file = sys.stderr
  104.     
  105.     
  106.     try:
  107.         file.write(formatwarning(message, category, filename, lineno))
  108.     except IOError:
  109.         pass
  110.  
  111.  
  112.  
  113. def formatwarning(message, category, filename, lineno):
  114.     import linecache
  115.     s = '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)
  116.     line = linecache.getline(filename, lineno).strip()
  117.     if line:
  118.         s = s + '  ' + line + '\n'
  119.     
  120.     return s
  121.  
  122.  
  123. def filterwarnings(action, message = '', category = Warning, module = '', lineno = 0, append = 0):
  124.     item = (action, re.compile(message, re.I), category, re.compile(module), lineno)
  125.     if append:
  126.         filters.append(item)
  127.     else:
  128.         filters.insert(0, item)
  129.  
  130.  
  131. def resetwarnings():
  132.     filters[:] = []
  133.  
  134.  
  135. class _OptionError(Exception):
  136.     pass
  137.  
  138.  
  139. def _processoptions(args):
  140.     for arg in args:
  141.         
  142.         try:
  143.             _setoption(arg)
  144.         except _OptionError:
  145.             msg = None
  146.             print >>sys.stderr, 'Invalid -W option ignored:', msg
  147.  
  148.     
  149.  
  150.  
  151. def _setoption(arg):
  152.     parts = arg.split(':')
  153.     if len(parts) > 5:
  154.         raise _OptionError('too many fields (max 5): %s' % `arg`)
  155.     
  156.     while len(parts) < 5:
  157.         parts.append('')
  158.     (action, message, category, module, lineno) = [ s.strip() for s in parts ]
  159.     action = _getaction(action)
  160.     message = re.escape(message)
  161.     category = _getcategory(category)
  162.     module = re.escape(module)
  163.     if lineno:
  164.         
  165.         try:
  166.             lineno = int(lineno)
  167.             if lineno < 0:
  168.                 raise ValueError
  169.         except (ValueError, OverflowError):
  170.             None if module else []
  171.             None if module else []
  172.             raise _OptionError('invalid lineno %s' % `lineno`)
  173.         except:
  174.             None if module else []
  175.  
  176.     else:
  177.         lineno = 0
  178.     filterwarnings(action, message, category, module, lineno)
  179.  
  180.  
  181. def _getaction(action):
  182.     if not action:
  183.         return 'default'
  184.     
  185.     if action == 'all':
  186.         return 'always'
  187.     
  188.     for a in [
  189.         'default',
  190.         'always',
  191.         'ignore',
  192.         'module',
  193.         'once',
  194.         'error']:
  195.         if a.startswith(action):
  196.             return a
  197.         
  198.     
  199.     raise _OptionError('invalid action: %s' % `action`)
  200.  
  201.  
  202. def _getcategory(category):
  203.     if not category:
  204.         return Warning
  205.     
  206.     if re.match('^[a-zA-Z0-9_]+$', category):
  207.         
  208.         try:
  209.             cat = eval(category)
  210.         except NameError:
  211.             raise _OptionError('unknown warning category: %s' % `category`)
  212.  
  213.     else:
  214.         i = category.rfind('.')
  215.         module = category[:i]
  216.         klass = category[i + 1:]
  217.         
  218.         try:
  219.             m = __import__(module, None, None, [
  220.                 klass])
  221.         except ImportError:
  222.             raise _OptionError('invalid module name: %s' % `module`)
  223.  
  224.         
  225.         try:
  226.             cat = getattr(m, klass)
  227.         except AttributeError:
  228.             raise _OptionError('unknown warning category: %s' % `category`)
  229.  
  230.     if not isinstance(cat, types.ClassType) or not issubclass(cat, Warning):
  231.         raise _OptionError('invalid warning category: %s' % `category`)
  232.     
  233.     return cat
  234.  
  235.  
  236. def _test():
  237.     import getopt
  238.     testoptions = []
  239.     
  240.     try:
  241.         (opts, args) = getopt.getopt(sys.argv[1:], 'W:')
  242.     except getopt.error:
  243.         msg = None
  244.         print >>sys.stderr, msg
  245.         return None
  246.  
  247.     for o, a in opts:
  248.         testoptions.append(a)
  249.     
  250.     
  251.     try:
  252.         _processoptions(testoptions)
  253.     except _OptionError:
  254.         msg = None
  255.         print >>sys.stderr, msg
  256.         return None
  257.  
  258.     for item in filters:
  259.         print item
  260.     
  261.     hello = 'hello world'
  262.     warn(hello)
  263.     warn(hello)
  264.     warn(hello)
  265.     warn(hello)
  266.     warn(hello, UserWarning)
  267.     warn(hello, DeprecationWarning)
  268.     for i in range(3):
  269.         warn(hello)
  270.     
  271.     filterwarnings('error', '', Warning, '', 0)
  272.     
  273.     try:
  274.         warn(hello)
  275.     except Exception:
  276.         msg = None
  277.         print 'Caught', msg.__class__.__name__ + ':', msg
  278.  
  279.     print 'No exception'
  280.     resetwarnings()
  281.     
  282.     try:
  283.         filterwarnings('booh', '', Warning, '', 0)
  284.     except Exception:
  285.         msg = None
  286.         print 'Caught', msg.__class__.__name__ + ':', msg
  287.  
  288.     print 'No exception'
  289.  
  290. if __name__ == '__main__':
  291.     import __main__
  292.     sys.modules['warnings'] = __main__
  293.     _test()
  294. else:
  295.     _processoptions(sys.warnoptions)
  296.     filterwarnings('ignore', category = OverflowWarning, append = 1)
  297.