PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/translator/oosupport/test_template/builtin.py

https://github.com/lalitjsraks/pypy
Python | 238 lines | 230 code | 3 blank | 5 comment | 1 complexity | 015c56537c8a06d87ae60b7231fc97b8 MD5 | raw file
  1. import os, math
  2. import errno
  3. import stat
  4. from py.builtin import sorted
  5. from pypy.tool import udir
  6. from pypy.rpython.test.test_rbuiltin import BaseTestRbuiltin
  7. from pypy.rpython.module.test.test_ll_time import BaseTestTime as llBaseTestTime
  8. class BaseTestBuiltin(BaseTestRbuiltin):
  9. def test_os_flags(self):
  10. from pypy.translator.oosupport.support import NT_OS
  11. def fn():
  12. return os.O_CREAT
  13. assert self.interpret(fn, []) == NT_OS['O_CREAT']
  14. def test_os_read_hibytes(self):
  15. """
  16. Test that we read in characters with the high bit set correctly
  17. This can be a problem on JVM or CLI, where we use unicode strings to
  18. encode byte arrays!
  19. """
  20. tmpfile = str(udir.udir.join("os_read_hibytes.txt"))
  21. def chrs2int(b):
  22. assert len(b) == 4
  23. first = ord(b[0]) # big endian
  24. if first & 0x80 != 0:
  25. first = first - 0x100
  26. return first << 24 | ord(b[1]) << 16 | ord(b[2]) << 8 | ord(b[3])
  27. def fn():
  28. fd = os.open(tmpfile, os.O_RDONLY|os.O_BINARY, 0666)
  29. res = os.read(fd, 4)
  30. os.close(fd)
  31. return chrs2int(res)
  32. f = file(tmpfile, 'w')
  33. f.write("".join([chr(x) for x in [0x06, 0x64, 0x90, 0x00]]))
  34. f.close()
  35. assert self.interpret(fn, []) == fn()
  36. def test_os_read_binary_crlf(self):
  37. tmpfile = str(udir.udir.join("os_read_test"))
  38. def fn(flag):
  39. if flag:
  40. fd = os.open(tmpfile, os.O_RDONLY|os.O_BINARY, 0666)
  41. else:
  42. fd = os.open(tmpfile, os.O_RDONLY, 0666)
  43. res = os.read(fd, 4096)
  44. os.close(fd)
  45. return res
  46. f = file(tmpfile, 'w')
  47. f.write('Hello\nWorld')
  48. f.close()
  49. res = self.ll_to_string(self.interpret(fn, [True]))
  50. assert res == file(tmpfile, 'rb').read()
  51. res = self.ll_to_string(self.interpret(fn, [False]))
  52. assert res == file(tmpfile, 'r').read()
  53. def test_os_dup_oo(self):
  54. tmpdir = str(udir.udir.join("os_dup_oo"))
  55. def fn():
  56. fd = os.open(tmpdir, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0777)
  57. os.write(fd, "hello world")
  58. fd2 = os.dup(fd)
  59. os.write(fd2, " (dupped)")
  60. os.close(fd)
  61. try:
  62. os.write(fd2, " (uh oh)")
  63. except OSError, e:
  64. return e.errno
  65. return -1
  66. assert self.interpret(fn, []) == 5 # EIO
  67. assert file(tmpdir).read() == 'hello world (dupped)'
  68. # the following tests can't be executed with gencli because they
  69. # returns file descriptors, and cli code is executed in another
  70. # process. Instead of those, there are new tests that opens and
  71. # write to a file all in the same process.
  72. def test_os_dup(self):
  73. pass
  74. def test_os_write(self):
  75. pass
  76. def test_os_write_single_char(self):
  77. pass
  78. def test_os_open(self):
  79. pass
  80. def test_os_open_write(self):
  81. tmpdir = str(udir.udir.join("os_write_test"))
  82. def fn():
  83. fd = os.open(tmpdir, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0777)
  84. os.write(fd, "hello world")
  85. os.close(fd)
  86. self.interpret(fn, [])
  87. assert file(tmpdir).read() == 'hello world'
  88. def test_os_write_magic(self):
  89. MAGIC = 62061 | (ord('\r')<<16) | (ord('\n')<<24)
  90. tmpfile = str(udir.udir.join("os_write_test"))
  91. def long2str(x):
  92. a = x & 0xff
  93. x >>= 8
  94. b = x & 0xff
  95. x >>= 8
  96. c = x & 0xff
  97. x >>= 8
  98. d = x & 0xff
  99. return chr(a) + chr(b) + chr(c) + chr(d)
  100. def fn(magic):
  101. fd = os.open(tmpfile, os.O_BINARY|os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0777)
  102. os.write(fd, long2str(magic))
  103. os.close(fd)
  104. self.interpret(fn, [MAGIC])
  105. contents = file(tmpfile, 'rb').read()
  106. assert contents == long2str(MAGIC)
  107. def test_os_stat(self):
  108. def fn(flag):
  109. if flag:
  110. return os.stat('.')[0]
  111. else:
  112. return os.stat('.').st_mode
  113. mode = self.interpret(fn, [0])
  114. assert stat.S_ISDIR(mode)
  115. mode = self.interpret(fn, [1])
  116. assert stat.S_ISDIR(mode)
  117. ACCESS_FLAGS = [os.F_OK, os.R_OK, os.W_OK, os.X_OK]
  118. def test_os_access(self):
  119. def create_fn(filenm):
  120. return lambda flag: os.access(filenm, flag)
  121. def try_file(filenm):
  122. for flag in self.ACCESS_FLAGS:
  123. print filenm, flag
  124. fn = create_fn(filenm)
  125. act = self.interpret(fn, [flag])
  126. assert act == fn(flag)
  127. assert not os.access('some_file_that_does_not_exist', os.F_OK) # shouldn't exist
  128. try_file('some_file_that_does_not_exist')
  129. try_file('.')
  130. open('some_file_that_DID_not_exist', 'w').close()
  131. os.chmod('some_file_that_DID_not_exist', 0)
  132. assert os.access('some_file_that_DID_not_exist', os.F_OK) # should exist now
  133. assert not os.access('some_file_that_DID_not_exist', os.W_OK) # should not be writable
  134. try_file('some_file_that_DID_not_exist')
  135. os.remove('some_file_that_DID_not_exist')
  136. #def test_os_access_allowed(self):
  137. # def fn(flag):
  138. # return os.access('.', flag)
  139. # for flag in self.ACCESS_FLAGS:
  140. # print flag
  141. # act = self.interpret(fn, [flag])
  142. # assert act == fn(flag)
  143. #
  144. #def test_os_access_denied(self):
  145. #
  146. # def fn(flag):
  147. # return os.access('/', flag)
  148. # for flag in self.ACCESS_FLAGS:
  149. # act = self.interpret(fn, [flag])
  150. # assert act == fn(flag)
  151. def test_os_stat_oserror(self):
  152. def fn():
  153. return os.stat('/directory/unlikely/to/exists')[0]
  154. self.interpret_raises(OSError, fn, [])
  155. def test_os_strerror(self):
  156. def fn():
  157. return os.strerror(errno.ENOTDIR)
  158. res = self.ll_to_string(self.interpret(fn, []))
  159. # XXX assert something about res
  160. def test_environ(self):
  161. def fn():
  162. os.environ['PYPY_TEST_ENVIRON'] = '42'
  163. return os.environ['PYPY_TEST_ENVIRON']
  164. assert self.interpret(fn, []) == '42'
  165. def test_environ_items(self):
  166. def fn():
  167. env = os.environ.items()
  168. env2 = []
  169. for key in os.environ.keys():
  170. env2.append((key, os.environ[key]))
  171. assert env == env2
  172. self.interpret(fn, [])
  173. def test_os_listdir(self):
  174. def fn():
  175. return os.listdir('.')
  176. res = self.ll_to_list(self.interpret(fn, []))
  177. res = [self.ll_to_string(s) for s in res]
  178. res.sort()
  179. assert res == sorted(os.listdir('.'))
  180. # XXX: remember to test ll_os_readlink and ll_os_pipe as soon as
  181. # they are implemented
  182. def test_math_modf(self):
  183. def fn(x):
  184. return math.modf(x)
  185. for x in (.5, 1, 1.5):
  186. for y in (1, -1):
  187. act_res = self.interpret(fn, [x*y])
  188. exp_res = math.modf(x*y)
  189. assert act_res.item0 == exp_res[0]
  190. assert act_res.item1 == exp_res[1]
  191. def test_rffi_primitive(self):
  192. from pypy.rpython.lltypesystem import rffi, lltype
  193. from pypy.translator.tool.cbuild import ExternalCompilationInfo
  194. eci = ExternalCompilationInfo(
  195. includes = ['ctype.h']
  196. )
  197. tolower = rffi.llexternal('tolower', [lltype.Signed], lltype.Signed,
  198. compilation_info=eci,
  199. oo_primitive='tolower')
  200. assert tolower._ptr._obj.oo_primitive == 'tolower'
  201. def fn(n):
  202. return tolower(n)
  203. res = self.interpret(fn, [ord('A')])
  204. assert res == ord('a')
  205. class BaseTestTime(llBaseTestTime):
  206. def test_time_clock(self):
  207. import time
  208. def f():
  209. return time.clock(), time.clock(), time.clock()
  210. res = self.interpret(f, [])
  211. t1, t2, t3 = self.ll_to_tuple(res)
  212. assert 0 <= t1 <= t2 <= t3