File: objspace.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (168 lines) | stat: -rw-r--r-- 5,324 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import py
import sys
from rpython.config.config import ConflictConfigError
from pypy.tool.option import make_config, make_objspace
from pypy.tool.pytest import appsupport
from pypy.conftest import option

_SPACECACHE={}
def gettestobjspace(**kwds):
    """ helper for instantiating and caching spaces for testing.
    """
    try:
        config = make_config(option, **kwds)
    except ConflictConfigError as e:
        # this exception is typically only raised if a module is not available.
        # in this case the test should be skipped
        py.test.skip(str(e))
    if getattr(option, 'runappdirect', None):
        return TinyObjSpace()
    key = config.getkey()
    try:
        return _SPACECACHE[key]
    except KeyError:
        space = maketestobjspace(config)
        _SPACECACHE[key] = space
        return space

def maketestobjspace(config=None):
    if config is None:
        config = make_config(option)
    if config.objspace.usemodules.thread:
        config.translation.thread = True
    config.objspace.extmodules = 'pypy.tool.pytest.fake_pytest'
    space = make_objspace(config)
    space.startup() # Initialize all builtin modules
    space.setitem(space.builtin.w_dict, space.wrap('raises'),
                  space.wrap(appsupport.app_raises))
    space.setitem(space.builtin.w_dict, space.wrap('skip'),
                  space.wrap(appsupport.app_skip))
    space.setitem(space.builtin.w_dict, space.wrap('py3k_skip'),
                  space.wrap(appsupport.app_py3k_skip))
    space.raises_w = appsupport.raises_w.__get__(space)
    return space

def skip_on_incompatible_interpreter(space_options):
    info = getattr(sys, 'pypy_translation_info', None)
    for key, value in space_options.iteritems():
        if key == 'usemodules':
            if info is not None:
                for modname in value:
                    ok = info.get('objspace.usemodules.%s' % modname,
                                  False)
                    if not ok:
                        py.test.skip("cannot runappdirect test: "
                                     "module %r required" % (modname,))
            else:
                if '__pypy__' in value:
                    py.test.skip("no module __pypy__ on top of CPython")
            continue
        if info is None:
            continue
        if ('translation.' + key) in info:
            key = 'translation.' + key
        has = info.get(key, None)
        if has != value:
            #print sys.pypy_translation_info
            py.test.skip("cannot runappdirect test: space needs %s = %s, "\
                "while pypy-c was built with %s" % (key, value, has))


class TinyObjSpace(object):
    """An object space that delegates everything to the hosting Python."""
    def __init__(self):
        for name in ('int', 'long', 'str', 'unicode', 'list', 'None', 'ValueError',
                'OverflowError'):
            setattr(self, 'w_' + name, eval(name))
        self.w_bytes = bytes
        import __builtin__ as __builtin__
        self.builtin = __builtin__

    def appexec(self, args, body):
        body = body.lstrip()
        assert body.startswith('(')
        src = py.code.Source("def anonymous" + body)
        return (src, args)

    def wrap(self, obj):
        if isinstance(obj, str):
            return obj.decode('utf-8')
        if isinstance(obj, dict):
            return dict((self.wrap(k), self.wrap(v))
                        for k, v in obj.iteritems())
        if isinstance(obj, tuple):
            return tuple(self.wrap(item) for item in obj)
        if isinstance(obj, list):
            return list(self.wrap(item) for item in obj)
        return obj

    def newtext(self, obj):
        assert isinstance(obj, str)
        return obj.decode('utf-8')

    def newbytes(self, obj):
        assert isinstance(obj, str)
        return obj

    def unpackiterable(self, itr):
        return list(itr)

    def is_true(self, obj):
        if isinstance(obj, tuple) and isinstance(obj[0], py.code.Source):
            raise ValueError('bool(appexec object) unknown')
        return bool(obj)

    def is_none(self, obj):
        return obj is None

    def str_w(self, w_str):
        return w_str

    def utf8_w(self, w_utf8):
        return w_utf8

    def bytes_w(self, w_bytes):
        return w_bytes

    def newdict(self, module=None):
        return {}

    def newtuple(self, iterable):
        return tuple(iterable)

    def newlist(self, iterable):
        return list(iterable)

    def newbytes(self, obj):
        return bytes(obj)

    def newutf8(self, obj, lgth):
        return obj

    def newtext(self, obj):
        assert isinstance(obj, str)
        return obj.decode('utf-8')

    def call_function(self, func, *args, **kwds):
        return func(*args, **kwds)

    def call_method(self, obj, name, *args, **kwds):
        return getattr(obj, name)(*args, **kwds)

    def getattr(self, obj, name):
        return getattr(obj, name)

    def setattr(self, obj, name, value):
        setattr(obj, name, value)

    def getbuiltinmodule(self, name):
        return __import__(name)

    def delslice(self, obj, *args):
        obj.__delslice__(*args)

    def is_w(self, obj1, obj2):
        return obj1 is obj2

    def setitem(self, obj, key, value):
        obj[key] = value