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