~certify-web-dev/twisted/certify-staging

« back to all changes in this revision

Viewing changes to twisted/internet/fdesc.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-01-02 19:38:17 UTC
  • mfrom: (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100102193817-jphp464ppwh7dulg
Tags: 9.0.0-1
* python-twisted: Depend on the python-twisted-* 9.0 packages.
* python-twisted: Depend on python-zope.interface only. Closes: #557781.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- test-case-name: twisted.test.test_fdesc -*-
2
 
 
3
 
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
 
2
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
4
3
# See LICENSE for details.
5
4
 
6
5
 
8
7
Utility functions for dealing with POSIX file descriptors.
9
8
"""
10
9
 
11
 
import sys
12
10
import os
13
11
import errno
14
 
import fcntl
15
 
if (sys.hexversion >> 16) >= 0x202:
16
 
    FCNTL = fcntl
17
 
else:
18
 
    import FCNTL
 
12
try:
 
13
    import fcntl
 
14
except ImportError:
 
15
    fcntl = None
19
16
 
20
17
# twisted imports
21
18
from twisted.internet.main import CONNECTION_LOST, CONNECTION_DONE
22
 
 
 
19
from twisted.python.runtime import platformType
23
20
 
24
21
def setNonBlocking(fd):
25
22
    """
26
23
    Make a file descriptor non-blocking.
27
24
    """
28
 
    flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
 
25
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
29
26
    flags = flags | os.O_NONBLOCK
30
 
    fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
 
27
    fcntl.fcntl(fd, fcntl.F_SETFL, flags)
31
28
 
32
29
 
33
30
def setBlocking(fd):
34
31
    """
35
32
    Make a file descriptor blocking.
36
33
    """
37
 
    flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
 
34
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
38
35
    flags = flags & ~os.O_NONBLOCK
39
 
    fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
 
36
    fcntl.fcntl(fd, fcntl.F_SETFL, flags)
 
37
 
 
38
 
 
39
if fcntl is None:
 
40
    # fcntl isn't available on Windows.  By default, handles aren't
 
41
    # inherited on Windows, so we can do nothing here.
 
42
    _setCloseOnExec = _unsetCloseOnExec = lambda fd: None
 
43
else:
 
44
    def _setCloseOnExec(fd):
 
45
        """
 
46
        Make a file descriptor close-on-exec.
 
47
        """
 
48
        flags = fcntl.fcntl(fd, fcntl.F_GETFD)
 
49
        flags = flags | fcntl.FD_CLOEXEC
 
50
        fcntl.fcntl(fd, fcntl.F_SETFD, flags)
 
51
 
 
52
 
 
53
    def _unsetCloseOnExec(fd):
 
54
        """
 
55
        Make a file descriptor close-on-exec.
 
56
        """
 
57
        flags = fcntl.fcntl(fd, fcntl.F_GETFD)
 
58
        flags = flags & ~fcntl.FD_CLOEXEC
 
59
        fcntl.fcntl(fd, fcntl.F_SETFD, flags)
40
60
 
41
61
 
42
62
def readFromFD(fd, callback):
43
63
    """
44
64
    Read from file descriptor, calling callback with resulting data.
45
65
 
46
 
    Returns same thing FileDescriptor.doRead would.
 
66
    If successful, call 'callback' with a single argument: the
 
67
    resulting data.
 
68
 
 
69
    Returns same thing FileDescriptor.doRead would: CONNECTION_LOST,
 
70
    CONNECTION_DONE, or None.
47
71
 
48
72
    @type fd: C{int}
49
73
    @param fd: non-blocking file descriptor to be read from.
69
93
        return CONNECTION_DONE
70
94
    callback(output)
71
95
 
 
96
 
72
97
def writeToFD(fd, data):
73
98
    """
74
99
    Write data to file descriptor.