~ubuntu-branches/ubuntu/trusty/pexpect/trusty-proposed

« back to all changes in this revision

Viewing changes to fdpexpect.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio, Thomas Kluyver, Jakub Wilk, Jackson Doak, Andrew Starr-Bochicchio
  • Date: 2013-12-06 20:20:26 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131206202026-9k9oixbv7e8ke30q
Tags: 3.0-1
* Team upload.

[ Thomas Kluyver ]
* New upstream release. Closes: #729518
* Add packaging for Python 3.
* Use pybuild for packaging.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Jackson Doak ]
* Create debian/python3-pexpect.docs

[ Andrew Starr-Bochicchio ]
* Remove empty debian/patches dir.
* Move documentation and examples into a new python-pexpect-doc
  package. They are shared between the Python 2 and Python 3 packages,
  so there is no need to install them with both.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""This is like pexpect, but will work on any file descriptor that you pass it.
2
 
So you are reponsible for opening and close the file descriptor.
3
 
 
4
 
$Id: fdpexpect.py 505 2007-12-26 21:33:50Z noah $
5
 
"""
6
 
 
7
 
from pexpect import *
8
 
import os
9
 
 
10
 
__all__ = ['fdspawn']
11
 
 
12
 
class fdspawn (spawn):
13
 
 
14
 
    """This is like pexpect.spawn but allows you to supply your own open file
15
 
    descriptor. For example, you could use it to read through a file looking
16
 
    for patterns, or to control a modem or serial device. """
17
 
 
18
 
    def __init__ (self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
19
 
 
20
 
        """This takes a file descriptor (an int) or an object that support the
21
 
        fileno() method (returning an int). All Python file-like objects
22
 
        support fileno(). """
23
 
 
24
 
        ### TODO: Add better handling of trying to use fdspawn in place of spawn
25
 
        ### TODO: (overload to allow fdspawn to also handle commands as spawn does.
26
 
 
27
 
        if type(fd) != type(0) and hasattr(fd, 'fileno'):
28
 
            fd = fd.fileno()
29
 
 
30
 
        if type(fd) != type(0):
31
 
            raise ExceptionPexpect ('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
32
 
 
33
 
        try: # make sure fd is a valid file descriptor
34
 
            os.fstat(fd)
35
 
        except OSError:
36
 
            raise ExceptionPexpect, 'The fd argument is not a valid file descriptor.'
37
 
 
38
 
        self.args = None
39
 
        self.command = None
40
 
        spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)
41
 
        self.child_fd = fd
42
 
        self.own_fd = False
43
 
        self.closed = False
44
 
        self.name = '<file descriptor %d>' % fd
45
 
 
46
 
    def __del__ (self):
47
 
 
48
 
        return
49
 
 
50
 
    def close (self):
51
 
 
52
 
        if self.child_fd == -1:
53
 
            return
54
 
        if self.own_fd:
55
 
            self.close (self)
56
 
        else:
57
 
            self.flush()
58
 
            os.close(self.child_fd)
59
 
            self.child_fd = -1
60
 
            self.closed = True
61
 
 
62
 
    def isalive (self):
63
 
 
64
 
        """This checks if the file descriptor is still valid. If os.fstat()
65
 
        does not raise an exception then we assume it is alive. """
66
 
 
67
 
        if self.child_fd == -1:
68
 
            return False
69
 
        try:
70
 
            os.fstat(self.child_fd)
71
 
            return True
72
 
        except:
73
 
            return False
74
 
 
75
 
    def terminate (self, force=False):
76
 
 
77
 
        raise ExceptionPexpect ('This method is not valid for file descriptors.')
78
 
 
79
 
    def kill (self, sig):
80
 
 
81
 
        return
82
 
 
 
1
import warnings
 
2
 
 
3
warnings.warn("This module has been moved to pexpect.fdpexpect, please update imports.",
 
4
                ImportWarning)
 
5
del warnings
 
6
 
 
7
from pexpect.fdpexpect import *  # analysis:ignore
 
 
b'\\ No newline at end of file'