| 1 |
import sys |
| 2 |
|
| 3 |
import py |
| 4 |
import pytest |
| 5 |
|
| 6 |
from rpython.tool.udir import udir |
| 7 |
from pypy import pypydir |
| 8 |
from pypy.interpreter.error import OperationError |
| 9 |
from pypy.interpreter.gateway import unwrap_spec, interp2app_temp, WrappedDefault |
| 10 |
from pypy.module.posix.interp_posix import convert_seconds, path_or_fd, Path |
| 11 |
|
| 12 |
|
| 13 |
class TestPexpect(object): |
| 14 |
|
| 15 |
def setup_class(cls): |
| 16 |
try: |
| 17 |
import pexpect |
| 18 |
import sys |
| 19 |
except ImportError: |
| 20 |
pytest.skip("pexpect not found") |
| 21 |
if sys.platform == "win32": |
| 22 |
pytest.skip("no pexpect.spawn on windows") |
| 23 |
|
| 24 |
def _spawn(self, *args, **kwds): |
| 25 |
import pexpect |
| 26 |
kwds.setdefault('timeout', 600) |
| 27 |
print 'SPAWN:', args, kwds |
| 28 |
child = pexpect.spawn(*args, maxread=5000, **kwds) |
| 29 |
child.logfile = sys.stdout |
| 30 |
return child |
| 31 |
|
| 32 |
def spawn(self, argv): |
| 33 |
py_py = py.path.local(pypydir).join('bin', 'pyinteractive.py') |
| 34 |
return self._spawn(sys.executable, [str(py_py), '-S'] + argv) |
| 35 |
|
| 36 |
def test_ttyname(self): |
| 37 |
source = py.code.Source(""" |
| 38 |
import os, sys |
| 39 |
assert os.ttyname(sys.stdin.fileno()) |
| 40 |
print('ok!') |
| 41 |
""") |
| 42 |
f = udir.join("test_ttyname.py") |
| 43 |
f.write(source) |
| 44 |
child = self.spawn([str(f)]) |
| 45 |
child.expect('ok!') |
| 46 |
|
| 47 |
|
| 48 |
def test_convert_seconds_simple(space): |
| 49 |
w_time = space.wrap(123.456) |
| 50 |
assert convert_seconds(space, w_time) == (123, 456000000) |
| 51 |
|
| 52 |
def test_convert_seconds_full(space): |
| 53 |
try: |
| 54 |
from hypothesis import given |
| 55 |
from hypothesis.strategies import integers |
| 56 |
except ImportError: |
| 57 |
pytest.skip("hypothesis not found") |
| 58 |
|
| 59 |
@given(s=integers(min_value=-2**30, max_value=2**30), |
| 60 |
ns=integers(min_value=0, max_value=10**9)) |
| 61 |
def _test_convert_seconds_full(space, s, ns): |
| 62 |
w_time = space.wrap(s + ns * 1e-9) |
| 63 |
sec, nsec = convert_seconds(space, w_time) |
| 64 |
assert 0 <= nsec < 1e9 |
| 65 |
MAX_ERR = 1e9 / 2**23 + 1 |
| 66 |
err = (sec * 10**9 + nsec) - (s * 10**9 + ns) |
| 67 |
assert -MAX_ERR < err < MAX_ERR |
| 68 |
_test_convert_seconds_full(space) |
| 69 |
|
| 70 |
def test_path_or_fd_nullable(space): |
| 71 |
@unwrap_spec(p=path_or_fd(allow_fd=False, nullable=True)) |
| 72 |
def f(space, p=None): |
| 73 |
return p.w_path |
| 74 |
w_f = space.wrap(interp2app_temp(f)) |
| 75 |
res = space.call_function(w_f, space.w_None) |
| 76 |
assert res is space.w_None |
| 77 |
res = space.call_function(w_f) |
| 78 |
assert res is space.w_None |
| 79 |
|
| 80 |
@unwrap_spec(p=path_or_fd(allow_fd=False)) |
| 81 |
def g(space, p): |
| 82 |
return p |
| 83 |
w_g = space.wrap(interp2app_temp(g)) |
| 84 |
with pytest.raises(OperationError) as exc: |
| 85 |
res = space.call_function(w_g, space.w_None) |
| 86 |
assert exc.value.match(space, space.w_TypeError) |