vm.py 12.1 KB
Newer Older
1
2
3
"""
Implementation of interpreter-level 'sys' routines.
"""
4

Alexander Hesse's avatar
Alexander Hesse committed
5
from rpython.rlib import jit
6
from rpython.rlib.rutf8 import MAXUNICODE
7

8
from pypy.interpreter import gateway
Philip Jenvey's avatar
Philip Jenvey committed
9
from pypy.interpreter.error import oefmt
10
11
12
from pypy.interpreter.gateway import unwrap_spec


13
14
# ____________________________________________________________

15

16
17
@unwrap_spec(depth=int)
def _getframe(space, depth=0):
Armin Rigo's avatar
Armin Rigo committed
18
19
20
21
22
23
24
    """Return a frame object from the call stack.  If optional integer depth is
given, return the frame object that many calls below the top of the stack.
If that is deeper than the call stack, ValueError is raised.  The default
for depth is zero, returning the frame at the top of the call stack.

This function should be used for internal and specialized
purposes only."""
25
    if depth < 0:
Philip Jenvey's avatar
Philip Jenvey committed
26
        raise oefmt(space.w_ValueError, "frame index must not be negative")
27
    return getframe(space, depth)
28
29
30


@jit.look_inside_iff(lambda space, depth: jit.isconstant(depth))
31
def getframe(space, depth):
32
33
34
35
    ec = space.getexecutioncontext()
    f = ec.gettopframe_nohidden()
    while True:
        if f is None:
Philip Jenvey's avatar
Philip Jenvey committed
36
            raise oefmt(space.w_ValueError, "call stack is not deep enough")
37
        if depth == 0:
38
            f.mark_as_escaped()
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
39
            return f
40
41
        depth -= 1
        f = ec.getnextframe_nohidden(f)
42

43

44
45
@unwrap_spec(new_limit="c_int")
def setrecursionlimit(space, new_limit):
46
47
48
49
50
51
    """setrecursionlimit() sets the maximum number of nested calls that
can occur before a RuntimeError is raised.  On PyPy the limit is
approximative and checked at a lower level.  The default 1000
reserves 768KB of stack space, which should suffice (on Linux,
depending on the compiler settings) for ~1400 calls.  Setting the
value to N reserves N/1000 times 768KB of stack space.
52
53
54
55
56

Note that there are other factors that also limit the stack size.
The operating system typically sets a maximum which can be changed
manually (e.g. with "ulimit" on Linux) for the main thread.  For other
threads you can configure the limit by calling "threading.stack_size()".
57
"""
Alexander Hesse's avatar
Alexander Hesse committed
58
    from rpython.rlib.rstack import _stack_set_length_fraction
59
    from rpython.rlib.rgc import increase_root_stack_depth
60
    if new_limit <= 0:
Philip Jenvey's avatar
Philip Jenvey committed
61
        raise oefmt(space.w_ValueError, "recursion limit must be positive")
Armin Rigo's avatar
Armin Rigo committed
62
63
64
65
66
67
    # Some programs use very large values to mean "don't check, I want to
    # use as much as possible and then segfault".  Add a silent upper bound
    # of 10**6 here, because huge values cause huge shadowstacks to be
    # allocated (or MemoryErrors).
    if new_limit > 1000000:
        new_limit = 1000000
68
    space.sys.recursionlimit = new_limit
69
    _stack_set_length_fraction(new_limit * 0.001)
70
    increase_root_stack_depth(int(new_limit * 0.001 * 163840))
71
72

def getrecursionlimit(space):
73
    """Return the last value set by setrecursionlimit().
74
    """
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
75
    return space.newint(space.sys.recursionlimit)
76

77
@unwrap_spec(flag=bool)
78
79
def set_track_resources(space, flag):
    space.sys.track_resources = flag
80

81
def get_track_resources(space):
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
82
    return space.newbool(space.sys.track_resources)
83

84
@unwrap_spec(interval=int)
85
def setcheckinterval(space, interval):
Armin Rigo's avatar
Armin Rigo committed
86
    """Tell the Python interpreter to check for asynchronous events every
87
    n instructions.  This also affects how often thread switches occur."""
Amaury Forgeot d'Arc's avatar
Amaury Forgeot d'Arc committed
88
    space.actionflag.setcheckinterval(interval)
89
90

def getcheckinterval(space):
Armin Rigo's avatar
Armin Rigo committed
91
    """Return the current check interval; see setcheckinterval()."""
92
93
94
95
96
    # xxx to make tests and possibly some obscure apps happy, if the
    # checkinterval is set to the minimum possible value (which is 1) we
    # return 0.  The idea is that according to the CPython docs, <= 0
    # means "check every virtual instruction, maximizing responsiveness
    # as well as overhead".
Amaury Forgeot d'Arc's avatar
Amaury Forgeot d'Arc committed
97
    result = space.actionflag.getcheckinterval()
98
99
    if result <= 1:
        result = 0
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
100
    return space.newint(result)
101
102

def exc_info(space):
Armin Rigo's avatar
Armin Rigo committed
103
104
105
    """Return the (type, value, traceback) of the most recent exception
caught by an except clause in the current stack frame or in an older stack
frame."""
106
107
108
    return exc_info_with_tb(space)    # indirection for the tests

def exc_info_with_tb(space):
109
110
    operror = space.getexecutioncontext().sys_exc_info()
    if operror is None:
111
        return space.newtuple([space.w_None, space.w_None, space.w_None])
112
    else:
Armin Rigo's avatar
Armin Rigo committed
113
        return space.newtuple([operror.w_type, operror.get_w_value(space),
114
                               operror.get_w_traceback(space)])
115

116
117
118
119
120
121
def exc_info_without_tb(space, frame):
    operror = frame.last_exception
    return space.newtuple([operror.w_type, operror.get_w_value(space),
                           space.w_None])

def exc_info_direct(space, frame):
Alexander Hesse's avatar
Alexander Hesse committed
122
    from pypy.tool import stdlib_opcode
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
    # In order to make the JIT happy, we try to return (exc, val, None)
    # instead of (exc, val, tb).  We can do that only if we recognize
    # the following pattern in the bytecode:
    #       CALL_FUNCTION/CALL_METHOD         <-- invoking me
    #       LOAD_CONST 0, 1, -2 or -3
    #       BINARY_SUBSCR
    # or:
    #       CALL_FUNCTION/CALL_METHOD
    #       LOAD_CONST <=2
    #       SLICE_2
    # or:
    #       CALL_FUNCTION/CALL_METHOD
    #       LOAD_CONST any integer
    #       LOAD_CONST <=2
    #       SLICE_3
    need_all_three_args = True
    co = frame.getcode().co_code
    p = frame.last_instr
    if (ord(co[p]) == stdlib_opcode.CALL_FUNCTION or
        ord(co[p]) == stdlib_opcode.CALL_METHOD):
        if ord(co[p+3]) == stdlib_opcode.LOAD_CONST:
            lo = ord(co[p+4])
            hi = ord(co[p+5])
            w_constant = frame.getconstant_w((hi * 256) | lo)
            if space.isinstance_w(w_constant, space.w_int):
                constant = space.int_w(w_constant)
                if ord(co[p+6]) == stdlib_opcode.BINARY_SUBSCR:
                    if -3 <= constant <= 1 and constant != -1:
                        need_all_three_args = False
                elif ord(co[p+6]) == stdlib_opcode.SLICE+2:
                    if constant <= 2:
                        need_all_three_args = False
                elif (ord(co[p+6]) == stdlib_opcode.LOAD_CONST and
                      ord(co[p+9]) == stdlib_opcode.SLICE+3):
                    lo = ord(co[p+7])
                    hi = ord(co[p+8])
                    w_constant = frame.getconstant_w((hi * 256) | lo)
                    if space.isinstance_w(w_constant, space.w_int):
                        if space.int_w(w_constant) <= 2:
                            need_all_three_args = False
    #
    if need_all_three_args or frame.last_exception is None or frame.hide():
        return exc_info_with_tb(space)
    else:
        return exc_info_without_tb(space, frame)

169
def exc_clear(space):
Armin Rigo's avatar
Armin Rigo committed
170
171
172
173
    """Clear global information on the current exception.  Subsequent calls
to exc_info() will return (None,None,None) until another exception is
raised and caught in the current thread or the execution stack returns to a
frame where another exception is being handled."""
174
    space.getexecutioncontext().clear_sys_exc_info()
175

176
def settrace(space, w_func):
Armin Rigo's avatar
Armin Rigo committed
177
    """Set the global debug tracing function.  It will be called on each
178
function call.  See the debugger chapter in the library manual."""
179
    space.getexecutioncontext().settrace(w_func)
180

181
182
183
184
185
def gettrace(space):
    """Return the global debug tracing function set with sys.settrace.
See the debugger chapter in the library manual."""
    return space.getexecutioncontext().gettrace()

186
def setprofile(space, w_func):
Armin Rigo's avatar
Armin Rigo committed
187
    """Set the profiling function.  It will be called on each function call
188
and return.  See the profiler chapter in the library manual."""
189
    space.getexecutioncontext().setprofile(w_func)
190

191
def getprofile(space):
192
193
    """Return the profiling function set with sys.setprofile.
See the profiler chapter in the library manual."""
194
195
196
197
198
199
    w_func = space.getexecutioncontext().getprofile()
    if w_func is not None:
        return w_func
    else:
        return space.w_None

200
def call_tracing(space, w_func, w_args):
Armin Rigo's avatar
Armin Rigo committed
201
    """Call func(*args), while tracing is enabled.  The tracing state is
202
203
204
saved, and restored afterwards.  This is intended to be called from
a debugger from a checkpoint, to recursively debug some other code."""
    return space.getexecutioncontext().call_tracing(w_func, w_args)
205

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

app = gateway.applevel('''
"NOT_RPYTHON"
from _structseq import structseqtype, structseqfield

class windows_version_info:
    __metaclass__ = structseqtype

    name = "sys.getwindowsversion"

    major = structseqfield(0, "Major version number")
    minor = structseqfield(1, "Minor version number")
    build = structseqfield(2, "Build number")
    platform = structseqfield(3, "Operating system platform")
    service_pack = structseqfield(4, "Latest Service Pack installed on the system")
221
222
223
224
225
226
227

    # Because the indices aren't consecutive, they aren't included when
    # unpacking and other such operations.
    service_pack_major = structseqfield(10, "Service Pack major version number")
    service_pack_minor = structseqfield(11, "Service Pack minor version number")
    suite_mask = structseqfield(12, "Bit mask identifying available product suites")
    product_type = structseqfield(13, "System product type")
228
229
''')

230

231
def getwindowsversion(space):
Alexander Hesse's avatar
Alexander Hesse committed
232
    from rpython.rlib import rwin32
233
    info = rwin32.GetVersionEx()
234
235
    w_windows_version_info = app.wget(space, "windows_version_info")
    raw_version = space.newtuple([
236
237
238
239
240
241
242
243
244
        space.newint(info[0]),
        space.newint(info[1]),
        space.newint(info[2]),
        space.newint(info[3]),
        space.newtext(info[4]),
        space.newint(info[5]),
        space.newint(info[6]),
        space.newint(info[7]),
        space.newint(info[8]),
245
246
    ])
    return space.call_function(w_windows_version_info, raw_version)
247

Matti Picus's avatar
Matti Picus committed
248
249
250
@jit.dont_look_inside
def get_dllhandle(space):
    if not space.config.objspace.usemodules.cpyext:
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
251
        return space.newint(0)
Matti Picus's avatar
Matti Picus committed
252
253
254
255
256
257
258
259

    return _get_dllhandle(space)

def _get_dllhandle(space):
    # Retrieve cpyext api handle
    from pypy.module.cpyext.api import State
    handle = space.fromcache(State).get_pythonapi_handle()

Matti Picus's avatar
Matti Picus committed
260
261
262
263
    # It used to be a CDLL
    # from pypy.module._rawffi.interp_rawffi import W_CDLL
    # from rpython.rlib.clibffi import RawCDLL
    # cdll = RawCDLL(handle)
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
264
    # return W_CDLL(space, "python api", cdll)
Matti Picus's avatar
Matti Picus committed
265
    # Provide a cpython-compatible int
Armin Rigo's avatar
Fix    
Armin Rigo committed
266
    from rpython.rtyper.lltypesystem import lltype, rffi
Carl Friedrich Bolz-Tereick's avatar
Carl Friedrich Bolz-Tereick committed
267
    return space.newint(rffi.cast(lltype.Signed, handle))
Matti Picus's avatar
Matti Picus committed
268

269
270
271
272
273
274
275
getsizeof_missing = """getsizeof(...)
    getsizeof(object, default) -> int
    
    Return the size of object in bytes.

sys.getsizeof(object, default) will always return default on PyPy, and
raise a TypeError if default is not provided.
276

Armin Rigo's avatar
Armin Rigo committed
277
278
279
280
281
282
283
284
First note that the CPython documentation says that this function may
raise a TypeError, so if you are seeing it, it means that the program
you are using is not correctly handling this case.

On PyPy, though, it always raises TypeError.  Before looking for
alternatives, please take a moment to read the following explanation as
to why it is the case.  What you are looking for may not be possible.

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
A memory profiler using this function is most likely to give results
inconsistent with reality on PyPy.  It would be possible to have
sys.getsizeof() return a number (with enough work), but that may or
may not represent how much memory the object uses.  It doesn't even
make really sense to ask how much *one* object uses, in isolation
with the rest of the system.  For example, instances have maps,
which are often shared across many instances; in this case the maps
would probably be ignored by an implementation of sys.getsizeof(),
but their overhead is important in some cases if they are many
instances with unique maps.  Conversely, equal strings may share
their internal string data even if they are different objects---or
empty containers may share parts of their internals as long as they
are empty.  Even stranger, some lists create objects as you read
them; if you try to estimate the size in memory of range(10**6) as
the sum of all items' size, that operation will by itself create one
million integer objects that never existed in the first place.
"""

Armin Rigo's avatar
Armin Rigo committed
303
def getsizeof(space, w_object, w_default=None):
Armin Rigo's avatar
Armin Rigo committed
304
    if w_default is None:
305
        raise oefmt(space.w_TypeError, getsizeof_missing)
Armin Rigo's avatar
Armin Rigo committed
306
    return w_default
307
getsizeof.__doc__ = getsizeof_missing