/usr/lib/python2.7/dist-packages/pyudev/_os/pipe.py is in python-pyudev 0.21.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | # -*- coding: utf-8 -*-
# Copyright (C) 2013 Sebastian Wiesner <lunaryorn@gmail.com>
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
pyudev._os.pipe
===============
Fallback implementations for pipe.
1. pipe2 from python os module
2. pipe2 from libc
3. pipe from python os module
The Pipe class wraps the chosen implementation.
.. moduleauthor:: Sebastian Wiesner <lunaryorn@gmail.com>
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import fcntl
from functools import partial
from pyudev._ctypeslib.libc import fd_pair
from pyudev._ctypeslib.libc import ERROR_CHECKERS
from pyudev._ctypeslib.libc import SIGNATURES
from pyudev._ctypeslib.utils import load_ctypes_library
# Define O_CLOEXEC, if not present in os already
O_CLOEXEC = getattr(os, 'O_CLOEXEC', 0o2000000)
def _pipe2_ctypes(libc, flags):
"""A ``pipe2`` implementation using ``pipe2`` from ctypes.
``libc`` is a :class:`ctypes.CDLL` object for libc. ``flags`` is an
integer providing the flags to ``pipe2``.
Return a pair of file descriptors ``(r, w)``.
"""
fds = fd_pair()
libc.pipe2(fds, flags)
return fds[0], fds[1]
def _pipe2_by_pipe(flags):
"""A ``pipe2`` implementation using :func:`os.pipe`.
``flags`` is an integer providing the flags to ``pipe2``.
.. warning::
This implementation is not atomic!
Return a pair of file descriptors ``(r, w)``.
"""
fds = os.pipe()
if flags & os.O_NONBLOCK != 0:
for fd in fds:
set_fd_status_flag(fd, os.O_NONBLOCK)
if flags & O_CLOEXEC != 0:
for fd in fds:
set_fd_flag(fd, O_CLOEXEC)
return fds
def _get_pipe2_implementation():
"""Find the appropriate implementation for ``pipe2``.
Return a function implementing ``pipe2``."""
if hasattr(os, 'pipe2'):
return os.pipe2 # pylint: disable=no-member
else:
try:
libc = load_ctypes_library("libc", SIGNATURES, ERROR_CHECKERS)
return (partial(_pipe2_ctypes, libc)
if hasattr(libc, 'pipe2') else
_pipe2_by_pipe)
except ImportError:
return _pipe2_by_pipe
_PIPE2 = _get_pipe2_implementation()
def set_fd_flag(fd, flag):
"""Set a flag on a file descriptor.
``fd`` is the file descriptor or file object, ``flag`` the flag as integer.
"""
flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
fcntl.fcntl(fd, fcntl.F_SETFD, flags | flag)
def set_fd_status_flag(fd, flag):
"""Set a status flag on a file descriptor.
``fd`` is the file descriptor or file object, ``flag`` the flag as integer.
"""
flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | flag)
class Pipe(object):
"""A unix pipe.
A pipe object provides two file objects: :attr:`source` is a readable file
object, and :attr:`sink` a writeable. Bytes written to :attr:`sink` appear
at :attr:`source`.
Open a pipe with :meth:`open()`.
"""
@classmethod
def open(cls):
"""Open and return a new :class:`Pipe`.
The pipe uses non-blocking IO."""
source, sink = _PIPE2(os.O_NONBLOCK | O_CLOEXEC)
return cls(source, sink)
def __init__(self, source_fd, sink_fd):
"""Create a new pipe object from the given file descriptors.
``source_fd`` is a file descriptor for the readable side of the pipe,
``sink_fd`` is a file descriptor for the writeable side."""
self.source = os.fdopen(source_fd, 'rb', 0)
self.sink = os.fdopen(sink_fd, 'wb', 0)
def close(self):
"""Closes both sides of the pipe."""
try:
self.source.close()
finally:
self.sink.close()
|