| 1 |
import pytest |
| 2 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 3 |
from pypy.module.cpyext.object import Py_PRINT_RAW |
| 4 |
from pypy.interpreter.error import OperationError |
| 5 |
from rpython.rtyper.lltypesystem import rffi |
| 6 |
from rpython.tool.udir import udir |
| 7 |
|
| 8 |
class TestFile(BaseApiTest): |
| 9 |
|
| 10 |
def test_file_fromstring(self, space, api): |
| 11 |
filename = rffi.str2charp(str(udir / "_test_file")) |
| 12 |
mode = rffi.str2charp("wb") |
| 13 |
w_file = api.PyFile_FromString(filename, mode) |
| 14 |
rffi.free_charp(filename) |
| 15 |
rffi.free_charp(mode) |
| 16 |
|
| 17 |
space.call_method(w_file, "write", space.newbytes("text")) |
| 18 |
space.call_method(w_file, "close") |
| 19 |
assert (udir / "_test_file").read() == "text" |
| 20 |
|
| 21 |
def test_file_getline(self, space, api): |
| 22 |
filename = rffi.str2charp(str(udir / "_test_file")) |
| 23 |
|
| 24 |
mode = rffi.str2charp("w") |
| 25 |
w_file = api.PyFile_FromString(filename, mode) |
| 26 |
space.call_method(w_file, "write", |
| 27 |
space.wrap("line1\nline2\nline3\nline4")) |
| 28 |
space.call_method(w_file, "close") |
| 29 |
|
| 30 |
rffi.free_charp(mode) |
| 31 |
mode = rffi.str2charp("r") |
| 32 |
w_file = api.PyFile_FromString(filename, mode) |
| 33 |
rffi.free_charp(filename) |
| 34 |
rffi.free_charp(mode) |
| 35 |
|
| 36 |
w_line = api.PyFile_GetLine(w_file, 0) |
| 37 |
assert space.text_w(w_line) == "line1\n" |
| 38 |
|
| 39 |
w_line = api.PyFile_GetLine(w_file, 4) |
| 40 |
assert space.text_w(w_line) == "line" |
| 41 |
|
| 42 |
w_line = api.PyFile_GetLine(w_file, 0) |
| 43 |
assert space.text_w(w_line) == "2\n" |
| 44 |
|
| 45 |
|
| 46 |
w_line = api.PyFile_GetLine(w_file, -1) |
| 47 |
|
| 48 |
assert space.text_w(w_line) == "line3\n" |
| 49 |
|
| 50 |
space.call_method(w_file, "close") |
| 51 |
|
| 52 |
def test_file_fromfd(self, space, api): |
| 53 |
name = str(udir / "_test_file") |
| 54 |
with rffi.scoped_str2charp(name) as filename: |
| 55 |
with rffi.scoped_str2charp("wb") as mode: |
| 56 |
w_file = api.PyFile_FromString(filename, mode) |
| 57 |
fp = space.int_w(w_file.fileno_w(space)) |
| 58 |
assert fp is not None |
| 59 |
w_file2 = api.PyFile_FromFd(fp, filename, mode, -1, None, None, None, 1) |
| 60 |
assert w_file2 is not None |
| 61 |
|
| 62 |
@pytest.mark.xfail |
| 63 |
def test_file_setbufsize(self, space, api): |
| 64 |
api.PyFile_SetBufSize() |
| 65 |
|
| 66 |
def test_file_writestring(self, space, api, capfd): |
| 67 |
w_stdout = space.sys.get("stdout") |
| 68 |
with rffi.scoped_str2charp("test\n") as s: |
| 69 |
api.PyFile_WriteString(s, w_stdout) |
| 70 |
space.call_method(w_stdout, "flush") |
| 71 |
out, err = capfd.readouterr() |
| 72 |
out = out.replace('\r\n', '\n') |
| 73 |
assert out == "test\n" |
| 74 |
|
| 75 |
def test_file_writeobject(self, space, api, capfd): |
| 76 |
w_obj = space.wrap("test\n") |
| 77 |
w_stdout = space.sys.get("stdout") |
| 78 |
api.PyFile_WriteObject(w_obj, w_stdout, Py_PRINT_RAW) |
| 79 |
api.PyFile_WriteObject(w_obj, w_stdout, 0) |
| 80 |
space.call_method(w_stdout, "flush") |
| 81 |
out, err = capfd.readouterr() |
| 82 |
out = out.replace('\r\n', '\n') |
| 83 |
assert out == "test\n'test\\n'" |
| 84 |
|
| 85 |
def test_fspath(self, space, api): |
| 86 |
w_obj = space.newtext("test") |
| 87 |
w_ret = api.PyOS_FSPath(w_obj) |
| 88 |
assert space.eq_w(w_ret, w_obj) |
| 89 |
|
| 90 |
w_obj = space.newint(3) |
| 91 |
with pytest.raises(OperationError): |
| 92 |
w_ret = api.PyOS_FSPath(w_obj) |
| 93 |
|
| 94 |
|
| 95 |
w_p1 = space.appexec([], '''(): |
| 96 |
class Pathlike(): |
| 97 |
def __fspath__(self): |
| 98 |
return 'test' |
| 99 |
return Pathlike()''') |
| 100 |
|
| 101 |
w_p2 = space.appexec([], '''(): |
| 102 |
class UnPathlike(): |
| 103 |
def __fspath__(self): |
| 104 |
return 42 |
| 105 |
return UnPathlike()''') |
| 106 |
|
| 107 |
w_ret = api.PyOS_FSPath(w_p1) |
| 108 |
assert space.eq_w(w_ret, space.newtext('test')) |
| 109 |
|
| 110 |
with pytest.raises(OperationError): |
| 111 |
w_ret = api.PyOS_FSPath(w_p2) |