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

/pypy/module/_multiprocessing/interp_win32.py

https://bitbucket.org/pypy/pypy/
Python | 217 lines | 215 code | 2 blank | 0 comment | 0 complexity | 4c8c77ef4eb22cb045a882d01d47439b MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rlib import rwin32
  2. from rpython.rlib.rarithmetic import r_uint
  3. from rpython.rtyper.lltypesystem import lltype, rffi
  4. from rpython.rtyper.tool import rffi_platform
  5. from rpython.translator.tool.cbuild import ExternalCompilationInfo
  6. from pypy.interpreter.error import oefmt, wrap_windowserror
  7. from pypy.interpreter.function import StaticMethod
  8. from pypy.interpreter.gateway import interp2app, unwrap_spec
  9. from pypy.module._multiprocessing.interp_connection import w_handle
  10. CONSTANTS = """
  11. PIPE_ACCESS_INBOUND PIPE_ACCESS_DUPLEX
  12. GENERIC_READ GENERIC_WRITE OPEN_EXISTING
  13. PIPE_TYPE_MESSAGE PIPE_READMODE_MESSAGE PIPE_WAIT
  14. PIPE_UNLIMITED_INSTANCES
  15. NMPWAIT_WAIT_FOREVER
  16. ERROR_PIPE_CONNECTED ERROR_SEM_TIMEOUT ERROR_PIPE_BUSY
  17. ERROR_NO_SYSTEM_RESOURCES ERROR_BROKEN_PIPE ERROR_MORE_DATA
  18. ERROR_ALREADY_EXISTS ERROR_NO_DATA
  19. """.split()
  20. class CConfig:
  21. _compilation_info_ = ExternalCompilationInfo(
  22. includes = ['windows.h'],
  23. libraries = ['kernel32'],
  24. )
  25. for name in CONSTANTS:
  26. locals()[name] = rffi_platform.ConstantInteger(name)
  27. config = rffi_platform.configure(CConfig)
  28. globals().update(config)
  29. def handle_w(space, w_handle):
  30. return rffi.cast(rwin32.HANDLE, space.int_w(w_handle))
  31. _CreateNamedPipe = rwin32.winexternal(
  32. 'CreateNamedPipeA', [
  33. rwin32.LPCSTR,
  34. rwin32.DWORD, rwin32.DWORD, rwin32.DWORD,
  35. rwin32.DWORD, rwin32.DWORD, rwin32.DWORD,
  36. rffi.VOIDP],
  37. rwin32.HANDLE,
  38. save_err=rffi.RFFI_SAVE_LASTERROR)
  39. _ConnectNamedPipe = rwin32.winexternal(
  40. 'ConnectNamedPipe', [rwin32.HANDLE, rffi.VOIDP], rwin32.BOOL,
  41. save_err=rffi.RFFI_SAVE_LASTERROR)
  42. _SetNamedPipeHandleState = rwin32.winexternal(
  43. 'SetNamedPipeHandleState', [
  44. rwin32.HANDLE,
  45. rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD],
  46. rwin32.BOOL,
  47. save_err=rffi.RFFI_SAVE_LASTERROR)
  48. _WaitNamedPipe = rwin32.winexternal(
  49. 'WaitNamedPipeA', [rwin32.LPCSTR, rwin32.DWORD],
  50. rwin32.BOOL,
  51. save_err=rffi.RFFI_SAVE_LASTERROR)
  52. _PeekNamedPipe = rwin32.winexternal(
  53. 'PeekNamedPipe', [
  54. rwin32.HANDLE,
  55. rffi.VOIDP,
  56. rwin32.DWORD,
  57. rwin32.LPDWORD, rwin32.LPDWORD, rwin32.LPDWORD],
  58. rwin32.BOOL,
  59. save_err=rffi.RFFI_SAVE_LASTERROR)
  60. _CreateFile = rwin32.winexternal(
  61. 'CreateFileA', [
  62. rwin32.LPCSTR,
  63. rwin32.DWORD, rwin32.DWORD, rffi.VOIDP,
  64. rwin32.DWORD, rwin32.DWORD, rwin32.HANDLE],
  65. rwin32.HANDLE,
  66. save_err=rffi.RFFI_SAVE_LASTERROR)
  67. _WriteFile = rwin32.winexternal(
  68. 'WriteFile', [
  69. rwin32.HANDLE,
  70. rffi.VOIDP, rwin32.DWORD,
  71. rwin32.LPDWORD, rffi.VOIDP],
  72. rwin32.BOOL,
  73. save_err=rffi.RFFI_SAVE_LASTERROR)
  74. _ReadFile = rwin32.winexternal(
  75. 'ReadFile', [
  76. rwin32.HANDLE,
  77. rffi.VOIDP, rwin32.DWORD,
  78. rwin32.LPDWORD, rffi.VOIDP],
  79. rwin32.BOOL,
  80. save_err=rffi.RFFI_SAVE_LASTERROR)
  81. _ExitProcess = rwin32.winexternal(
  82. 'ExitProcess', [rffi.UINT], lltype.Void,
  83. save_err=rffi.RFFI_SAVE_LASTERROR)
  84. _GetTickCount = rwin32.winexternal(
  85. 'GetTickCount', [], rwin32.DWORD)
  86. _Sleep = rwin32.winexternal(
  87. 'Sleep', [rwin32.DWORD], lltype.Void)
  88. def CloseHandle(space, w_handle):
  89. handle = handle_w(space, w_handle)
  90. if not rwin32.CloseHandle(handle):
  91. raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
  92. def GetLastError(space):
  93. return space.wrap(rwin32.GetLastError_saved())
  94. # __________________________________________________________
  95. # functions for the "win32" namespace
  96. @unwrap_spec(name=str, openmode=r_uint, pipemode=r_uint, maxinstances=r_uint,
  97. outputsize=r_uint, inputsize=r_uint, timeout=r_uint)
  98. def CreateNamedPipe(space, name, openmode, pipemode, maxinstances,
  99. outputsize, inputsize, timeout, w_security):
  100. security = space.int_w(w_security)
  101. if security:
  102. raise oefmt(space.w_NotImplementedError, "expected a NULL pointer")
  103. handle = _CreateNamedPipe(
  104. name, openmode, pipemode, maxinstances,
  105. outputsize, inputsize, timeout, rffi.NULL)
  106. if handle == rwin32.INVALID_HANDLE_VALUE:
  107. raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
  108. return w_handle(space, handle)
  109. def ConnectNamedPipe(space, w_handle, w_overlapped):
  110. handle = handle_w(space, w_handle)
  111. overlapped = space.int_w(w_overlapped)
  112. if overlapped:
  113. raise oefmt(space.w_NotImplementedError, "expected a NULL pointer")
  114. if not _ConnectNamedPipe(handle, rffi.NULL):
  115. raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
  116. def SetNamedPipeHandleState(space, w_handle, w_pipemode, w_maxinstances,
  117. w_timeout):
  118. handle = handle_w(space, w_handle)
  119. state = lltype.malloc(rffi.CArrayPtr(rffi.UINT).TO, 3, flavor='raw')
  120. statep = lltype.malloc(rffi.CArrayPtr(rffi.UINTP).TO, 3, flavor='raw',
  121. zero=True)
  122. try:
  123. if not space.is_w(w_pipemode, space.w_None):
  124. state[0] = space.uint_w(w_pipemode)
  125. statep[0] = rffi.ptradd(state, 0)
  126. if not space.is_w(w_maxinstances, space.w_None):
  127. state[1] = space.uint_w(w_maxinstances)
  128. statep[1] = rffi.ptradd(state, 1)
  129. if not space.is_w(w_timeout, space.w_None):
  130. state[2] = space.uint_w(w_timeout)
  131. statep[2] = rffi.ptradd(state, 2)
  132. if not _SetNamedPipeHandleState(handle, statep[0], statep[1],
  133. statep[2]):
  134. raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
  135. finally:
  136. lltype.free(state, flavor='raw')
  137. lltype.free(statep, flavor='raw')
  138. @unwrap_spec(name=str, timeout=r_uint)
  139. def WaitNamedPipe(space, name, timeout):
  140. # Careful: zero means "default value specified by CreateNamedPipe()"
  141. if not _WaitNamedPipe(name, timeout):
  142. raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
  143. @unwrap_spec(filename=str, access=r_uint, share=r_uint,
  144. disposition=r_uint, flags=r_uint)
  145. def CreateFile(space, filename, access, share, w_security,
  146. disposition, flags, w_templatefile):
  147. security = space.int_w(w_security)
  148. templatefile = space.int_w(w_templatefile)
  149. if security or templatefile:
  150. raise oefmt(space.w_NotImplementedError, "expected a NULL pointer")
  151. handle = _CreateFile(filename, access, share, rffi.NULL,
  152. disposition, flags, rwin32.NULL_HANDLE)
  153. if handle == rwin32.INVALID_HANDLE_VALUE:
  154. raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
  155. return w_handle(space, handle)
  156. @unwrap_spec(code=r_uint)
  157. def ExitProcess(space, code):
  158. _ExitProcess(code)
  159. def win32_namespace(space):
  160. "NOT_RPYTHON"
  161. w_win32 = space.call_function(space.w_type,
  162. space.wrap("win32"),
  163. space.newtuple([]),
  164. space.newdict())
  165. # constants
  166. for name in CONSTANTS:
  167. space.setattr(w_win32,
  168. space.wrap(name),
  169. space.wrap(config[name]))
  170. space.setattr(w_win32,
  171. space.wrap('NULL'),
  172. space.newint(0))
  173. # functions
  174. for name in ['CloseHandle', 'GetLastError', 'CreateFile',
  175. 'CreateNamedPipe', 'ConnectNamedPipe',
  176. 'SetNamedPipeHandleState', 'WaitNamedPipe',
  177. 'ExitProcess',
  178. ]:
  179. function = globals()[name]
  180. w_function = space.wrap(interp2app(function))
  181. w_method = space.wrap(StaticMethod(w_function))
  182. space.setattr(w_win32, space.wrap(name), w_method)
  183. return w_win32