PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rpython/module/test/test_posix.py

https://github.com/yasirs/pypy
Python | 223 lines | 187 code | 34 blank | 2 comment | 23 complexity | 5801f5a8fa421d7486f9a2ec6726b7bb MD5 | raw file
  1. import py
  2. from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
  3. from pypy.tool.udir import udir
  4. import os
  5. exec 'import %s as posix' % os.name
  6. def setup_module(module):
  7. testf = udir.join('test.txt')
  8. module.path = testf.strpath
  9. class BaseTestPosix(BaseRtypingTest):
  10. def setup_method(self, meth):
  11. # prepare/restore the file before each test
  12. testfile = open(path, 'wb')
  13. testfile.write('This is a test')
  14. testfile.close()
  15. def test_open(self):
  16. def f():
  17. ff = posix.open(path,posix.O_RDONLY,0777)
  18. return ff
  19. func = self.interpret(f,[])
  20. assert type(func) == int
  21. def test_fstat(self):
  22. def fo(fi):
  23. g = posix.fstat(fi)
  24. return g
  25. fi = os.open(path,os.O_RDONLY,0777)
  26. func = self.interpret(fo,[fi])
  27. stat = os.fstat(fi)
  28. for i in range(len(stat)):
  29. assert long(getattr(func, 'item%d' % i)) == stat[i]
  30. def test_stat(self):
  31. def fo():
  32. g = posix.stat(path)
  33. return g
  34. func = self.interpret(fo,[])
  35. stat = os.stat(path)
  36. for i in range(len(stat)):
  37. assert long(getattr(func, 'item%d' % i)) == stat[i]
  38. def test_stat_exception(self):
  39. def fo():
  40. try:
  41. posix.stat('I/do/not/exist')
  42. except OSError:
  43. return True
  44. else:
  45. return False
  46. res = self.interpret(fo,[])
  47. assert res
  48. def test_times(self):
  49. import py; py.test.skip("llinterp does not like tuple returns")
  50. from pypy.rpython.test.test_llinterp import interpret
  51. times = interpret(lambda: posix.times(), ())
  52. assert isinstance(times, tuple)
  53. assert len(times) == 5
  54. for value in times:
  55. assert isinstance(value, int)
  56. def test_lseek(self):
  57. def f(fi,pos):
  58. posix.lseek(fi,pos,0)
  59. fi = os.open(path,os.O_RDONLY,0777)
  60. func = self.interpret(f,[fi,5])
  61. res = os.read(fi,2)
  62. assert res =='is'
  63. def test_isatty(self):
  64. def f(fi):
  65. posix.isatty(fi)
  66. fi = os.open(path,os.O_RDONLY,0777)
  67. func = self.interpret(f,[fi])
  68. assert not func
  69. os.close(fi)
  70. func = self.interpret(f,[fi])
  71. assert not func
  72. def test_getcwd(self):
  73. def f():
  74. return posix.getcwd()
  75. res = self.interpret(f,[])
  76. cwd = os.getcwd()
  77. #print res.chars,cwd
  78. assert self.ll_to_string(res) == cwd
  79. def test_write(self):
  80. def f(fi):
  81. if fi > 0:
  82. text = 'This is a test'
  83. else:
  84. text = '333'
  85. return posix.write(fi,text)
  86. fi = os.open(path,os.O_WRONLY,0777)
  87. text = 'This is a test'
  88. func = self.interpret(f,[fi])
  89. os.close(fi)
  90. fi = os.open(path,os.O_RDONLY,0777)
  91. res = os.read(fi,20)
  92. assert res == text
  93. def test_read(self):
  94. def f(fi,len):
  95. return posix.read(fi,len)
  96. fi = os.open(path,os.O_WRONLY,0777)
  97. text = 'This is a test'
  98. os.write(fi,text)
  99. os.close(fi)
  100. fi = os.open(path,os.O_RDONLY,0777)
  101. res = self.interpret(f,[fi,20])
  102. assert self.ll_to_string(res) == text
  103. if hasattr(os, 'chown'):
  104. def test_chown(self):
  105. f = open(path, "w")
  106. f.write("xyz")
  107. f.close()
  108. def f():
  109. try:
  110. posix.chown(path, os.getuid(), os.getgid())
  111. return 1
  112. except OSError:
  113. return 2
  114. assert self.interpret(f, []) == 1
  115. os.unlink(path)
  116. assert self.interpret(f, []) == 2
  117. def test_close(self):
  118. def f(fi):
  119. return posix.close(fi)
  120. fi = os.open(path,os.O_WRONLY,0777)
  121. text = 'This is a test'
  122. os.write(fi,text)
  123. res = self.interpret(f,[fi])
  124. raises( OSError, os.fstat, fi)
  125. if hasattr(os, 'ftruncate'):
  126. def test_ftruncate(self):
  127. def f(fi,len):
  128. os.ftruncate(fi,len)
  129. fi = os.open(path,os.O_RDWR,0777)
  130. func = self.interpret(f,[fi,6])
  131. assert os.fstat(fi).st_size == 6
  132. if hasattr(os, 'getuid'):
  133. def test_getuid(self):
  134. def f():
  135. return os.getuid()
  136. assert self.interpret(f, []) == f()
  137. if hasattr(os, 'getgid'):
  138. def test_getgid(self):
  139. def f():
  140. return os.getgid()
  141. assert self.interpret(f, []) == f()
  142. if hasattr(os, 'setuid'):
  143. def test_os_setuid(self):
  144. def f():
  145. os.setuid(os.getuid())
  146. return os.getuid()
  147. assert self.interpret(f, []) == f()
  148. if hasattr(os, 'sysconf'):
  149. def test_os_sysconf(self):
  150. def f(i):
  151. return os.sysconf(i)
  152. assert self.interpret(f, [13]) == f(13)
  153. if hasattr(os, 'chroot'):
  154. def test_os_chroot(self):
  155. def f():
  156. try:
  157. os.chroot('!@$#!#%$#^#@!#!$$#^')
  158. except OSError:
  159. return 1
  160. return 0
  161. assert self.interpret(f, []) == 1
  162. def test_os_wstar(self):
  163. from pypy.rpython.module.ll_os import RegisterOs
  164. for name in RegisterOs.w_star:
  165. if not hasattr(os, name):
  166. continue
  167. def fun(s):
  168. return getattr(os, name)(s)
  169. for value in [0, 1, 127, 128, 255]:
  170. res = self.interpret(fun, [value])
  171. assert res == fun(value)
  172. class TestLLtype(BaseTestPosix, LLRtypeMixin):
  173. if hasattr(os, 'getgroups'):
  174. def test_getgroups(self):
  175. def f():
  176. return os.getgroups()
  177. ll_a = self.interpret(f, [])
  178. assert self.ll_to_list(ll_a) == f()
  179. class TestOOtype(BaseTestPosix, OORtypeMixin):
  180. def test_fstat(self):
  181. py.test.skip("ootypesystem does not support os.fstat")
  182. def test_os_chroot(self):
  183. py.test.skip("ootypesystem does not support os.chroot")
  184. def test_stat(self):
  185. py.test.skip("ootypesystem does not support os.stat")
  186. def test_stat_exception(self):
  187. py.test.skip("ootypesystem does not support os.stat")
  188. def test_chown(self):
  189. py.test.skip("ootypesystem does not support os.chown")