fdesc.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- test-case-name: twisted.test.test_fdesc -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Utility functions for dealing with POSIX file descriptors.
  6. """
  7. import errno
  8. import os
  9. try:
  10. import fcntl as _fcntl
  11. except ImportError:
  12. fcntl = None
  13. else:
  14. fcntl = _fcntl
  15. # twisted imports
  16. from twisted.internet.main import CONNECTION_DONE, CONNECTION_LOST
  17. def setNonBlocking(fd):
  18. """
  19. Set the file description of the given file descriptor to non-blocking.
  20. """
  21. flags = fcntl.fcntl(fd, fcntl.F_GETFL)
  22. flags = flags | os.O_NONBLOCK
  23. fcntl.fcntl(fd, fcntl.F_SETFL, flags)
  24. def setBlocking(fd):
  25. """
  26. Set the file description of the given file descriptor to blocking.
  27. """
  28. flags = fcntl.fcntl(fd, fcntl.F_GETFL)
  29. flags = flags & ~os.O_NONBLOCK
  30. fcntl.fcntl(fd, fcntl.F_SETFL, flags)
  31. if fcntl is None:
  32. # fcntl isn't available on Windows. By default, handles aren't
  33. # inherited on Windows, so we can do nothing here.
  34. _setCloseOnExec = _unsetCloseOnExec = lambda fd: None
  35. else:
  36. def _setCloseOnExec(fd):
  37. """
  38. Make a file descriptor close-on-exec.
  39. """
  40. flags = fcntl.fcntl(fd, fcntl.F_GETFD)
  41. flags = flags | fcntl.FD_CLOEXEC
  42. fcntl.fcntl(fd, fcntl.F_SETFD, flags)
  43. def _unsetCloseOnExec(fd):
  44. """
  45. Make a file descriptor close-on-exec.
  46. """
  47. flags = fcntl.fcntl(fd, fcntl.F_GETFD)
  48. flags = flags & ~fcntl.FD_CLOEXEC
  49. fcntl.fcntl(fd, fcntl.F_SETFD, flags)
  50. def readFromFD(fd, callback):
  51. """
  52. Read from file descriptor, calling callback with resulting data.
  53. If successful, call 'callback' with a single argument: the
  54. resulting data.
  55. Returns same thing FileDescriptor.doRead would: CONNECTION_LOST,
  56. CONNECTION_DONE, or None.
  57. @type fd: C{int}
  58. @param fd: non-blocking file descriptor to be read from.
  59. @param callback: a callable which accepts a single argument. If
  60. data is read from the file descriptor it will be called with this
  61. data. Handling exceptions from calling the callback is up to the
  62. caller.
  63. Note that if the descriptor is still connected but no data is read,
  64. None will be returned but callback will not be called.
  65. @return: CONNECTION_LOST on error, CONNECTION_DONE when fd is
  66. closed, otherwise None.
  67. """
  68. try:
  69. output = os.read(fd, 8192)
  70. except OSError as ioe:
  71. if ioe.args[0] in (errno.EAGAIN, errno.EINTR):
  72. return
  73. else:
  74. return CONNECTION_LOST
  75. if not output:
  76. return CONNECTION_DONE
  77. callback(output)
  78. def writeToFD(fd, data):
  79. """
  80. Write data to file descriptor.
  81. Returns same thing FileDescriptor.writeSomeData would.
  82. @type fd: C{int}
  83. @param fd: non-blocking file descriptor to be written to.
  84. @type data: C{str} or C{buffer}
  85. @param data: bytes to write to fd.
  86. @return: number of bytes written, or CONNECTION_LOST.
  87. """
  88. try:
  89. return os.write(fd, data)
  90. except OSError as io:
  91. if io.errno in (errno.EAGAIN, errno.EINTR):
  92. return 0
  93. return CONNECTION_LOST
  94. __all__ = ["setNonBlocking", "setBlocking", "readFromFD", "writeToFD"]