home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / psyco / support.py < prev   
Encoding:
Python Source  |  2005-10-30  |  6.0 KB  |  197 lines

  1. ###########################################################################
  2. #  Psyco general support module.
  3. #   Copyright (C) 2001-2002  Armin Rigo et.al.
  4.  
  5. """Psyco general support module.
  6.  
  7. For internal use.
  8. """
  9. ###########################################################################
  10.  
  11. import sys, _psyco, __builtin__
  12.  
  13. error = _psyco.error
  14. class warning(Warning):
  15.     pass
  16.  
  17. _psyco.NoLocalsWarning = warning
  18.  
  19. def warn(msg):
  20.     from warnings import warn
  21.     warn(msg, warning, stacklevel=2)
  22.  
  23. #
  24. # Version checks
  25. #
  26. __version__ = 0x010500f0
  27. if _psyco.PSYVER != __version__:
  28.     raise error, "version mismatch between Psyco parts, reinstall it"
  29.  
  30. version_info = (__version__ >> 24,
  31.                 (__version__ >> 16) & 0xff,
  32.                 (__version__ >> 8) & 0xff,
  33.                 {0xa0: 'alpha',
  34.                  0xb0: 'beta',
  35.                  0xc0: 'candidate',
  36.                  0xf0: 'final'}[__version__ & 0xf0],
  37.                 __version__ & 0xf)
  38.  
  39.  
  40. VERSION_LIMITS = [0x02010000,   # 2.1
  41.                   0x02020000,   # 2.2
  42.                   0x02020200,   # 2.2.2
  43.                   0x02030000,   # 2.3
  44.                   0x02040000]   # 2.4
  45.  
  46. if ([v for v in VERSION_LIMITS if v <= sys.hexversion] !=
  47.     [v for v in VERSION_LIMITS if v <= _psyco.PYVER  ]):
  48.     if sys.hexversion < VERSION_LIMITS[0]:
  49.         warn("Psyco requires Python version 2.1 or later")
  50.     else:
  51.         warn("Psyco version does not match Python version. "
  52.              "Psyco must be updated or recompiled")
  53.  
  54. PYTHON_SUPPORT = hasattr(_psyco, 'turbo_code')
  55.  
  56.  
  57. if hasattr(_psyco, 'ALL_CHECKS') and hasattr(_psyco, 'VERBOSE_LEVEL'):
  58.     print >> sys.stderr, ('psyco: running in debugging mode on %s' %
  59.                           _psyco.PROCESSOR)
  60.  
  61.  
  62. ###########################################################################
  63. # sys._getframe() gives strange results on a mixed Psyco- and Python-style
  64. # stack frame. Psyco provides a replacement that partially emulates Python
  65. # frames from Psyco frames. The new sys._getframe() may return objects of
  66. # a custom "Psyco frame" type, which with Python >=2.2 is a subtype of the
  67. # normal frame type.
  68. #
  69. # The same problems require some other built-in functions to be replaced
  70. # as well. Note that the local variables are not available in any
  71. # dictionary with Psyco.
  72.  
  73.  
  74. class Frame:
  75.     pass
  76.  
  77.  
  78. class PythonFrame(Frame):
  79.  
  80.     def __init__(self, frame):
  81.         self.__dict__.update({
  82.             '_frame': frame,
  83.             })
  84.  
  85.     def __getattr__(self, attr):
  86.         if attr == 'f_back':
  87.             try:
  88.                 result = embedframe(_psyco.getframe(self._frame))
  89.             except ValueError:
  90.                 result = None
  91.             except error:
  92.                 warn("f_back is skipping dead Psyco frames")
  93.                 result = self._frame.f_back
  94.             self.__dict__['f_back'] = result
  95.             return result
  96.         else:
  97.             return getattr(self._frame, attr)
  98.  
  99.     def __setattr__(self, attr, value):
  100.         setattr(self._frame, attr, value)
  101.  
  102.     def __delattr__(self, attr):
  103.         delattr(self._frame, attr)
  104.  
  105.  
  106. class PsycoFrame(Frame):
  107.  
  108.     def __init__(self, tag):
  109.         self.__dict__.update({
  110.             '_tag'     : tag,
  111.             'f_code'   : tag[0],
  112.             'f_globals': tag[1],
  113.             })
  114.  
  115.     def __getattr__(self, attr):
  116.         if attr == 'f_back':
  117.             try:
  118.                 result = embedframe(_psyco.getframe(self._tag))
  119.             except ValueError:
  120.                 result = None
  121.         elif attr == 'f_lineno':
  122.             result = self.f_code.co_firstlineno  # better than nothing
  123.         elif attr == 'f_builtins':
  124.             result = self.f_globals['__builtins__']
  125.         elif attr == 'f_restricted':
  126.             result = self.f_builtins is not __builtins__
  127.         elif attr == 'f_locals':
  128.             raise AttributeError, ("local variables of functions run by Psyco "
  129.                                    "cannot be accessed in any way, sorry")
  130.         else:
  131.             raise AttributeError, ("emulated Psyco frames have "
  132.                                    "no '%s' attribute" % attr)
  133.         self.__dict__[attr] = result
  134.         return result
  135.  
  136.     def __setattr__(self, attr, value):
  137.         raise AttributeError, "Psyco frame objects are read-only"
  138.  
  139.     def __delattr__(self, attr):
  140.         if attr == 'f_trace':
  141.             # for bdb which relies on CPython frames exhibiting a slightly
  142.             # buggy behavior: you can 'del f.f_trace' as often as you like
  143.             # even without having set it previously.
  144.             return
  145.         raise AttributeError, "Psyco frame objects are read-only"
  146.  
  147.  
  148. def embedframe(result):
  149.     if type(result) is type(()):
  150.         return PsycoFrame(result)
  151.     else:
  152.         return PythonFrame(result)
  153.  
  154. def _getframe(depth=0):
  155.     """Return a frame object from the call stack. This is a replacement for
  156. sys._getframe() which is aware of Psyco frames.
  157.  
  158. The returned objects are instances of either PythonFrame or PsycoFrame
  159. instead of being real Python-level frame object, so that they can emulate
  160. the common attributes of frame objects.
  161.  
  162. The original sys._getframe() ignoring Psyco frames altogether is stored in
  163. psyco._getrealframe(). See also psyco._getemulframe()."""
  164.     # 'depth+1' to account for this _getframe() Python function
  165.     return embedframe(_psyco.getframe(depth+1))
  166.  
  167. def _getemulframe(depth=0):
  168.     """As _getframe(), but the returned objects are real Python frame objects
  169. emulating Psyco frames. Some of their attributes can be wrong or missing,
  170. however."""
  171.     # 'depth+1' to account for this _getemulframe() Python function
  172.     return _psyco.getframe(depth+1, 1)
  173.  
  174. def patch(name, module=__builtin__):
  175.     f = getattr(_psyco, name)
  176.     org = getattr(module, name)
  177.     if org is not f:
  178.         setattr(module, name, f)
  179.         setattr(_psyco, 'original_' + name, org)
  180.  
  181. _getrealframe = sys._getframe
  182. sys._getframe = _getframe
  183. patch('globals')
  184. patch('eval')
  185. patch('execfile')
  186. patch('locals')
  187. patch('vars')
  188. patch('dir')
  189. patch('input')
  190. _psyco.original_raw_input = raw_input
  191. __builtin__.__in_psyco__ = 0==1   # False
  192.  
  193. if hasattr(_psyco, 'compact'):
  194.     import kdictproxy
  195.     _psyco.compactdictproxy = kdictproxy.compactdictproxy
  196.