PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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