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

/pypy/rlib/rposix.py

http://github.com/pypy/pypy
Python | 243 lines | 240 code | 1 blank | 2 comment | 3 complexity | 0afe40b798190ce942cdcfec4ebcb453 MD5 | raw file
  1. import os
  2. from pypy.rpython.lltypesystem.rffi import (CConstant, CExternVariable,
  3. INT, CCHARPP)
  4. from pypy.rpython.lltypesystem import lltype, ll2ctypes, rffi
  5. from pypy.translator.tool.cbuild import ExternalCompilationInfo
  6. from pypy.rlib.rarithmetic import intmask
  7. from pypy.rlib.objectmodel import specialize
  8. from pypy.rlib import jit
  9. class CConstantErrno(CConstant):
  10. # these accessors are used when calling get_errno() or set_errno()
  11. # on top of CPython
  12. def __getitem__(self, index):
  13. assert index == 0
  14. try:
  15. return ll2ctypes.TLS.errno
  16. except AttributeError:
  17. raise ValueError("no C function call occurred so far, "
  18. "errno is undefined")
  19. def __setitem__(self, index, value):
  20. assert index == 0
  21. ll2ctypes.TLS.errno = value
  22. if os.name == 'nt':
  23. separate_module_sources =['''
  24. /* Lifted completely from CPython 3.3 Modules/posix_module.c */
  25. #include <malloc.h> /* for _msize */
  26. typedef struct {
  27. intptr_t osfhnd;
  28. char osfile;
  29. } my_ioinfo;
  30. extern __declspec(dllimport) char * __pioinfo[];
  31. #define IOINFO_L2E 5
  32. #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
  33. #define IOINFO_ARRAYS 64
  34. #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
  35. #define FOPEN 0x01
  36. #define _NO_CONSOLE_FILENO (intptr_t)-2
  37. /* This function emulates what the windows CRT
  38. does to validate file handles */
  39. int
  40. _PyVerify_fd(int fd)
  41. {
  42. const int i1 = fd >> IOINFO_L2E;
  43. const int i2 = fd & ((1 << IOINFO_L2E) - 1);
  44. static size_t sizeof_ioinfo = 0;
  45. /* Determine the actual size of the ioinfo structure,
  46. * as used by the CRT loaded in memory
  47. */
  48. if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
  49. sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
  50. }
  51. if (sizeof_ioinfo == 0) {
  52. /* This should not happen... */
  53. goto fail;
  54. }
  55. /* See that it isn't a special CLEAR fileno */
  56. if (fd != _NO_CONSOLE_FILENO) {
  57. /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
  58. * we check pointer validity and other info
  59. */
  60. if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
  61. /* finally, check that the file is open */
  62. my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
  63. if (info->osfile & FOPEN) {
  64. return 1;
  65. }
  66. }
  67. }
  68. fail:
  69. errno = EBADF;
  70. return 0;
  71. }
  72. ''',]
  73. export_symbols = ['_PyVerify_fd']
  74. else:
  75. separate_module_sources = []
  76. export_symbols = []
  77. errno_eci = ExternalCompilationInfo(
  78. includes=['errno.h','stdio.h'],
  79. separate_module_sources = separate_module_sources,
  80. export_symbols = export_symbols,
  81. )
  82. _get_errno, _set_errno = CExternVariable(INT, 'errno', errno_eci,
  83. CConstantErrno, sandboxsafe=True,
  84. _nowrapper=True, c_type='int')
  85. # the default wrapper for set_errno is not suitable for use in critical places
  86. # like around GIL handling logic, so we provide our own wrappers.
  87. def get_errno():
  88. return intmask(_get_errno())
  89. def set_errno(errno):
  90. _set_errno(rffi.cast(INT, errno))
  91. if os.name == 'nt':
  92. is_valid_fd = rffi.llexternal(
  93. "_PyVerify_fd", [rffi.INT], rffi.INT,
  94. compilation_info=errno_eci,
  95. )
  96. @jit.dont_look_inside
  97. def validate_fd(fd):
  98. if not is_valid_fd(fd):
  99. raise OSError(get_errno(), 'Bad file descriptor')
  100. else:
  101. def is_valid_fd(fd):
  102. return 1
  103. def validate_fd(fd):
  104. return 1
  105. def closerange(fd_low, fd_high):
  106. # this behaves like os.closerange() from Python 2.6.
  107. for fd in xrange(fd_low, fd_high):
  108. try:
  109. if is_valid_fd(fd):
  110. os.close(fd)
  111. except OSError:
  112. pass
  113. #___________________________________________________________________
  114. # Wrappers around posix functions, that accept either strings, or
  115. # instances with a "as_bytes()" method.
  116. # - pypy.modules.posix.interp_posix passes an object containing a unicode path
  117. # which can encode itself with sys.filesystemencoding.
  118. # - but pypy.rpython.module.ll_os.py on Windows will replace these functions
  119. # with other wrappers that directly handle unicode strings.
  120. @specialize.argtype(0)
  121. def open(path, flags, mode):
  122. if isinstance(path, str):
  123. return os.open(path, flags, mode)
  124. else:
  125. return os.open(path.as_bytes(), flags, mode)
  126. @specialize.argtype(0)
  127. def stat(path):
  128. if isinstance(path, str):
  129. return os.stat(path)
  130. else:
  131. return os.stat(path.as_bytes())
  132. @specialize.argtype(0)
  133. def lstat(path):
  134. if isinstance(path, str):
  135. return os.lstat(path)
  136. else:
  137. return os.lstat(path.as_bytes())
  138. @specialize.argtype(0)
  139. def unlink(path):
  140. if isinstance(path, str):
  141. return os.unlink(path)
  142. else:
  143. return os.unlink(path.as_bytes())
  144. @specialize.argtype(0, 1)
  145. def rename(path1, path2):
  146. if isinstance(path1, str):
  147. return os.rename(path1, path2)
  148. else:
  149. return os.rename(path1.as_bytes(), path2.as_bytes())
  150. @specialize.argtype(0)
  151. def listdir(dirname):
  152. if isinstance(dirname, str):
  153. return os.listdir(dirname)
  154. else:
  155. return os.listdir(dirname.as_bytes())
  156. @specialize.argtype(0)
  157. def access(path, mode):
  158. if isinstance(path, str):
  159. return os.access(path, mode)
  160. else:
  161. return os.access(path.as_bytes(), mode)
  162. @specialize.argtype(0)
  163. def chmod(path, mode):
  164. if isinstance(path, str):
  165. return os.chmod(path, mode)
  166. else:
  167. return os.chmod(path.as_bytes(), mode)
  168. @specialize.argtype(0, 1)
  169. def utime(path, times):
  170. if isinstance(path, str):
  171. return os.utime(path, times)
  172. else:
  173. return os.utime(path.as_bytes(), times)
  174. @specialize.argtype(0)
  175. def chdir(path):
  176. if isinstance(path, str):
  177. return os.chdir(path)
  178. else:
  179. return os.chdir(path.as_bytes())
  180. @specialize.argtype(0)
  181. def mkdir(path, mode=0777):
  182. if isinstance(path, str):
  183. return os.mkdir(path, mode)
  184. else:
  185. return os.mkdir(path.as_bytes(), mode)
  186. @specialize.argtype(0)
  187. def rmdir(path):
  188. if isinstance(path, str):
  189. return os.rmdir(path)
  190. else:
  191. return os.rmdir(path.as_bytes())
  192. @specialize.argtype(0)
  193. def mkfifo(path, mode):
  194. if isinstance(path, str):
  195. os.mkfifo(path, mode)
  196. else:
  197. os.mkfifo(path.as_bytes(), mode)
  198. @specialize.argtype(0)
  199. def mknod(path, mode, device):
  200. if isinstance(path, str):
  201. os.mknod(path, mode, device)
  202. else:
  203. os.mknod(path.as_bytes(), mode, device)
  204. @specialize.argtype(0, 1)
  205. def symlink(src, dest):
  206. if isinstance(src, str):
  207. os.symlink(src, dest)
  208. else:
  209. os.symlink(src.as_bytes(), dest.as_bytes())
  210. if os.name == 'nt':
  211. import nt
  212. def _getfullpathname(path):
  213. if isinstance(path, str):
  214. return nt._getfullpathname(path)
  215. else:
  216. return nt._getfullpathname(path.as_bytes())