File: test_interp_posix.py

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; awk: 4
file content (83 lines) | stat: -rw-r--r-- 2,709 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
import sys

import py
import pytest

from rpython.tool.udir import udir
from pypy import pypydir
from pypy.interpreter.error import OperationError
from pypy.interpreter.gateway import unwrap_spec, interp2app_temp, WrappedDefault
from pypy.module.posix.interp_posix import convert_seconds, path_or_fd, Path


class TestPexpect(object):
    # XXX replace with AppExpectTest class as soon as possible
    def setup_class(cls):
        try:
            import pexpect
        except ImportError:
            pytest.skip("pexpect not found")

    def _spawn(self, *args, **kwds):
        import pexpect
        kwds.setdefault('timeout', 600)
        print 'SPAWN:', args, kwds
        child = pexpect.spawn(*args, maxread=5000, **kwds)
        child.logfile = sys.stdout
        return child

    def spawn(self, argv):
        py_py = py.path.local(pypydir).join('bin', 'pyinteractive.py')
        return self._spawn(sys.executable, [str(py_py), '-S'] + argv)

    def test_ttyname(self):
        source = py.code.Source("""
        import os, sys
        assert os.ttyname(sys.stdin.fileno())
        print('ok!')
        """)
        f = udir.join("test_ttyname.py")
        f.write(source)
        child = self.spawn([str(f)])
        child.expect('ok!')


def test_convert_seconds_simple(space):
    w_time = space.wrap(123.456)
    assert convert_seconds(space, w_time) == (123, 456000000)

def test_convert_seconds_full(space):
    try:
        from hypothesis import given
        from hypothesis.strategies import integers
    except ImportError:
        pytest.skip("hypothesis not found")

    @given(s=integers(min_value=-2**30, max_value=2**30),
           ns=integers(min_value=0, max_value=10**9))
    def _test_convert_seconds_full(space, s, ns):
        w_time = space.wrap(s + ns * 1e-9)
        sec, nsec = convert_seconds(space, w_time)
        assert 0 <= nsec < 1e9
        MAX_ERR = 1e9 / 2**23 + 1  # nsec has 53 - 30 = 23 bits of precisin
        err = (sec * 10**9 + nsec) - (s * 10**9 + ns)
        assert -MAX_ERR < err < MAX_ERR
    _test_convert_seconds_full(space)

def test_path_or_fd_nullable(space):
    @unwrap_spec(p=path_or_fd(allow_fd=False, nullable=True))
    def f(space, p=None):
        return p.w_path
    w_f = space.wrap(interp2app_temp(f))
    res = space.call_function(w_f, space.w_None)
    assert res is space.w_None
    res = space.call_function(w_f)
    assert res is space.w_None

    @unwrap_spec(p=path_or_fd(allow_fd=False))
    def g(space, p):
        return p
    w_g = space.wrap(interp2app_temp(g))
    with pytest.raises(OperationError) as exc:
        res = space.call_function(w_g, space.w_None)
    assert exc.value.match(space, space.w_TypeError)