PageRenderTime 95ms CodeModel.GetById 28ms app.highlight 20ms RepoModel.GetById 44ms app.codeStats 0ms

/pypy/module/cpyext/test/test_pyfile.py

https://bitbucket.org/pypy/pypy/
Python | 161 lines | 131 code | 23 blank | 7 comment | 4 complexity | afd86e5fbef77ed7f88e199bea6f5823 MD5 | raw file
  1from pypy.conftest import option
  2from pypy.module.cpyext.test.test_api import BaseApiTest
  3from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
  4from pypy.module.cpyext.object import Py_PRINT_RAW
  5from rpython.rtyper.lltypesystem import rffi, lltype
  6from rpython.tool.udir import udir
  7import pytest
  8
  9class TestFile(BaseApiTest):
 10
 11    def test_file_fromstring(self, space, api):
 12        filename = rffi.str2charp(str(udir / "_test_file"))
 13        mode = rffi.str2charp("wb")
 14        w_file = api.PyFile_FromString(filename, mode)
 15        rffi.free_charp(filename)
 16        rffi.free_charp(mode)
 17
 18        assert api.PyFile_Check(w_file)
 19        assert api.PyFile_CheckExact(w_file)
 20        assert not api.PyFile_Check(space.wrap("text"))
 21
 22        space.call_method(w_file, "write", space.newbytes("text"))
 23        space.call_method(w_file, "close")
 24        assert (udir / "_test_file").read() == "text"
 25
 26    def test_file_getline(self, space, api):
 27        filename = rffi.str2charp(str(udir / "_test_file"))
 28
 29        mode = rffi.str2charp("w")
 30        w_file = api.PyFile_FromString(filename, mode)
 31        space.call_method(w_file, "write",
 32                          space.wrap("line1\nline2\nline3\nline4"))
 33        space.call_method(w_file, "close")
 34
 35        rffi.free_charp(mode)
 36        mode = rffi.str2charp("r")
 37        w_file = api.PyFile_FromString(filename, mode)
 38        rffi.free_charp(filename)
 39        rffi.free_charp(mode)
 40
 41        w_line = api.PyFile_GetLine(w_file, 0)
 42        assert space.str_w(w_line) == "line1\n"
 43
 44        w_line = api.PyFile_GetLine(w_file, 4)
 45        assert space.str_w(w_line) == "line"
 46
 47        w_line = api.PyFile_GetLine(w_file, 0)
 48        assert space.str_w(w_line) == "2\n"
 49
 50        # XXX We ought to raise an EOFError here, but don't
 51        w_line = api.PyFile_GetLine(w_file, -1)
 52        # assert api.PyErr_Occurred() is space.w_EOFError
 53        assert space.str_w(w_line) == "line3\n"
 54
 55        space.call_method(w_file, "close")
 56
 57    def test_file_name(self, space, api):
 58        name = str(udir / "_test_file")
 59        with rffi.scoped_str2charp(name) as filename:
 60            with rffi.scoped_str2charp("wb") as mode:
 61                w_file = api.PyFile_FromString(filename, mode)
 62        assert space.str_w(api.PyFile_Name(w_file)) == name
 63
 64    def test_file_fromfile(self, space, api):
 65        name = str(udir / "_test_file")
 66        with rffi.scoped_str2charp(name) as filename:
 67            with rffi.scoped_str2charp("wb") as mode:
 68                w_file = api.PyFile_FromString(filename, mode)
 69                fp = api.PyFile_AsFile(w_file)
 70                assert fp is not None
 71                w_file2 = api.PyFile_FromFile(fp, filename, mode, None)
 72        assert w_file2 is not None
 73        assert api.PyFile_Check(w_file2)
 74        assert space.str_w(api.PyFile_Name(w_file2)) == name
 75
 76    @pytest.mark.xfail
 77    def test_file_setbufsize(self, space, api):
 78        api.PyFile_SetBufSize()
 79
 80    def test_file_writestring(self, space, api, capfd):
 81        s = rffi.str2charp("test\n")
 82        try:
 83            api.PyFile_WriteString(s, space.sys.get("stdout"))
 84        finally:
 85            rffi.free_charp(s)
 86        out, err = capfd.readouterr()
 87        out = out.replace('\r\n', '\n')
 88        assert out == "test\n"
 89
 90    def test_file_writeobject(self, space, api, capfd):
 91        w_obj = space.wrap("test\n")
 92        w_stdout = space.sys.get("stdout")
 93        api.PyFile_WriteObject(w_obj, w_stdout, Py_PRINT_RAW)
 94        api.PyFile_WriteObject(w_obj, w_stdout, 0)
 95        space.call_method(w_stdout, "flush")
 96        out, err = capfd.readouterr()
 97        out = out.replace('\r\n', '\n')
 98        assert out == "test\n'test\\n'"
 99
100    def test_file_softspace(self, space, api, capfd):
101        w_stdout = space.sys.get("stdout")
102        assert api.PyFile_SoftSpace(w_stdout, 1) == 0
103        assert api.PyFile_SoftSpace(w_stdout, 0) == 1
104        
105        api.PyFile_SoftSpace(w_stdout, 1)
106        w_ns = space.newdict()
107        space.exec_("print 1,", w_ns, w_ns)
108        space.exec_("print 2,", w_ns, w_ns)
109        api.PyFile_SoftSpace(w_stdout, 0)
110        space.exec_("print 3", w_ns, w_ns)
111        space.call_method(w_stdout, "flush")
112        out, err = capfd.readouterr()
113        out = out.replace('\r\n', '\n')
114        assert out == " 1 23\n"
115
116
117class AppTestPyFile(AppTestCpythonExtensionBase):
118
119    def setup_class(cls):
120        from rpython.tool.udir import udir
121        if option.runappdirect:
122            cls.w_udir = str(udir)
123        else:
124            cls.w_udir = cls.space.wrap(str(udir))
125
126    def test_file_tell(self):
127        module = self.import_extension('foo', [
128            ("get_c_tell", "METH_O",
129             """
130                FILE * fp = PyFile_AsFile(args);
131                if (fp == NULL)
132                    return PyLong_FromLong(0);
133                return PyLong_FromLong(ftell(fp));
134             """),
135            ("read_10", "METH_O",
136             """
137                char s[10];
138                FILE * fp = PyFile_AsFile(args);
139                if (fp == NULL)
140                    return PyLong_FromLong(0);
141                fread(s, 1, 10, fp);
142                return PyLong_FromLong(ftell(fp));
143             """),
144            ])
145        filename = self.udir + "/_test_file"
146        with open(filename, 'w') as fid:
147            fid.write('3' * 122)
148        with open(filename, 'r') as fid:
149            s = fid.read(80)
150            t_py = fid.tell()
151            assert t_py == 80
152            t_c = module.get_c_tell(fid)
153            assert t_c == t_py
154            print '-------- tell ',t_c
155            t_c = module.read_10(fid)
156            assert t_c == t_py + 10
157            print '-------- tell ',t_c
158            t_py = fid.tell()
159            assert t_c == t_py, 'after a fread, c level ftell(fp) %d but PyFile.tell() %d' % (t_c, t_py)
160
161