home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-python-addon-1.4.9-installer.exe / warnings.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-10-01  |  8.9 KB  |  283 lines

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