home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / warnings.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  7.0 KB  |  289 lines

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