Back to index

python3.2  3.2.2
Classes | Functions | Variables
termios.c File Reference
#include "Python.h"
#include <termios.h>
#include <sys/ioctl.h>

Go to the source code of this file.

Classes

struct  constant

Functions

 PyDoc_STRVAR (termios__doc__,"This module provides an interface to the Posix calls for tty I/O control.\n\ For a complete description of these calls, see the Posix or Unix manual\n\ pages. It is only available for those Unix versions that support Posix\n\ termios style tty I/O control.\n\ \n\ All functions in this module take a file descriptor fd as their first\n\ argument. This can be an integer file descriptor, such as returned by\n\ sys.stdin.fileno(), or a file object, such as sys.stdin itself.")
static int fdconv (PyObject *obj, void *p)
 PyDoc_STRVAR (termios_tcgetattr__doc__,"tcgetattr(fd) -> list_of_attrs\n\ \n\ Get the tty attributes for file descriptor fd, as follows:\n\ [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] where cc is a list\n\ of the tty special characters (each a string of length 1, except the items\n\ with indices VMIN and VTIME, which are integers when these fields are\n\ defined). The interpretation of the flags and the speeds as well as the\n\ indexing in the cc array must be done using the symbolic constants defined\n\ in this module.")
static PyObjecttermios_tcgetattr (PyObject *self, PyObject *args)
 PyDoc_STRVAR (termios_tcsetattr__doc__,"tcsetattr(fd, when, attributes) -> None\n\ \n\ Set the tty attributes for file descriptor fd.\n\ The attributes to be set are taken from the attributes argument, which\n\ is a list like the one returned by tcgetattr(). The when argument\n\ determines when the attributes are changed: termios.TCSANOW to\n\ change immediately, termios.TCSADRAIN to change after transmitting all\n\ queued output, or termios.TCSAFLUSH to change after transmitting all\n\ queued output and discarding all queued input. ")
static PyObjecttermios_tcsetattr (PyObject *self, PyObject *args)
 PyDoc_STRVAR (termios_tcsendbreak__doc__,"tcsendbreak(fd, duration) -> None\n\ \n\ Send a break on file descriptor fd.\n\ A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration\n\ has a system dependent meaning.")
static PyObjecttermios_tcsendbreak (PyObject *self, PyObject *args)
 PyDoc_STRVAR (termios_tcdrain__doc__,"tcdrain(fd) -> None\n\ \n\ Wait until all output written to file descriptor fd has been transmitted.")
static PyObjecttermios_tcdrain (PyObject *self, PyObject *args)
 PyDoc_STRVAR (termios_tcflush__doc__,"tcflush(fd, queue) -> None\n\ \n\ Discard queued data on file descriptor fd.\n\ The queue selector specifies which queue: termios.TCIFLUSH for the input\n\ queue, termios.TCOFLUSH for the output queue, or termios.TCIOFLUSH for\n\ both queues. ")
static PyObjecttermios_tcflush (PyObject *self, PyObject *args)
 PyDoc_STRVAR (termios_tcflow__doc__,"tcflow(fd, action) -> None\n\ \n\ Suspend or resume input or output on file descriptor fd.\n\ The action argument can be termios.TCOOFF to suspend output,\n\ termios.TCOON to restart output, termios.TCIOFF to suspend input,\n\ or termios.TCION to restart input.")
static PyObjecttermios_tcflow (PyObject *self, PyObject *args)
PyMODINIT_FUNC PyInit_termios (void)

Variables

static PyObjectTermiosError
static PyMethodDef termios_methods []
static struct constant termios_constants []
static struct PyModuleDef

Class Documentation

struct constant

Definition at line 320 of file termios.c.

Class Members
char * name
long value

Function Documentation

static int fdconv ( PyObject obj,
void p 
) [static]

Definition at line 43 of file termios.c.

{
    int fd;

    fd = PyObject_AsFileDescriptor(obj);
    if (fd >= 0) {
        *(int*)p = fd;
        return 1;
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyDoc_STRVAR ( termios__doc__  ,
"This module provides an interface to the Posix calls for tty I/O control.\n\For a complete description of these  calls,
see the Posix or Unix manual\n\pages.It is only available for those Unix versions that support Posix\n\termios style tty I/O control.\n\\n\All functions in this module take a file descriptor fd as their first\n\argument.This can be an integer file  descriptor,
such as returned by\n\sys.stdin.  fileno(),
or a file  object,
such as sys.stdin itself."   
)
PyDoc_STRVAR ( termios_tcgetattr__doc__  ,
"tcgetattr(fd) -> list_of_attrs\n\\n\Get the tty attributes for file descriptor  fd,
as follows:\n\where cc is a list\n\of the tty special characters(each a string of length 1, except the items\n\with indices VMIN and VTIME, which are integers when these fields are\n\defined).The interpretation of the flags and the speeds as well as the\n\indexing in the cc array must be done using the symbolic constants defined\n\in this module."  [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] 
)
PyDoc_STRVAR ( termios_tcsetattr__doc__  ,
"tcsetattr(fd, when, attributes) -> None\n\\n\Set the tty attributes for file descriptor fd.\n\The attributes to be set are taken from the attributes  argument,
which\n\is a list like the one returned by tcgetattr().The when argument\n\determines when the attributes are changed:termios.TCSANOW to\n\change  immediately,
termios.TCSADRAIN to change after transmitting all\n\queued  output,
or termios.TCSAFLUSH to change after transmitting all\n\queued output and discarding all queued input."   
)
PyDoc_STRVAR ( termios_tcsendbreak__doc__  ,
"tcsendbreak(fd, duration) -> None\n\\n\Send a break on file descriptor fd.\n\A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration\n\has a system dependent meaning."   
)
PyDoc_STRVAR ( termios_tcdrain__doc__  ,
"tcdrain(fd) -> None\n\\n\Wait until all output written to file descriptor fd has been transmitted."   
)
PyDoc_STRVAR ( termios_tcflush__doc__  ,
"tcflush(fd, queue) -> None\n\\n\Discard queued data on file descriptor fd.\n\The queue selector specifies which queue: termios.TCIFLUSH for the input\n\  queue,
termios.TCOFLUSH for the output  queue,
or termios.TCIOFLUSH for\n\both queues."   
)
PyDoc_STRVAR ( termios_tcflow__doc__  ,
"tcflow(fd, action) -> None\n\\n\Suspend or resume input or output on file descriptor fd.\n\The action argument can be termios.TCOOFF to suspend  output,
\n\termios.TCOON to restart  output,
termios.TCIOFF to suspend  input,
\n\or termios.TCION to restart input."   
)

Definition at line 956 of file termios.c.

{
    PyObject *m;
    struct constant *constant = termios_constants;

    m = PyModule_Create(&termiosmodule);
    if (m == NULL)
        return NULL;

    if (TermiosError == NULL) {
        TermiosError = PyErr_NewException("termios.error", NULL, NULL);
    }
    Py_INCREF(TermiosError);
    PyModule_AddObject(m, "error", TermiosError);

    while (constant->name != NULL) {
        PyModule_AddIntConstant(m, constant->name, constant->value);
        ++constant;
    }
    return m;
}

Here is the call graph for this function:

static PyObject* termios_tcdrain ( PyObject self,
PyObject args 
) [static]

Definition at line 234 of file termios.c.

{
    int fd;

    if (!PyArg_ParseTuple(args, "O&:tcdrain",
                          fdconv, &fd))
        return NULL;
    if (tcdrain(fd) == -1)
        return PyErr_SetFromErrno(TermiosError);

    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:

static PyObject* termios_tcflow ( PyObject self,
PyObject args 
) [static]

Definition at line 280 of file termios.c.

{
    int fd, action;

    if (!PyArg_ParseTuple(args, "O&i:tcflow",
                          fdconv, &fd, &action))
        return NULL;
    if (tcflow(fd, action) == -1)
        return PyErr_SetFromErrno(TermiosError);

    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:

static PyObject* termios_tcflush ( PyObject self,
PyObject args 
) [static]

Definition at line 257 of file termios.c.

{
    int fd, queue;

    if (!PyArg_ParseTuple(args, "O&i:tcflush",
                          fdconv, &fd, &queue))
        return NULL;
    if (tcflush(fd, queue) == -1)
        return PyErr_SetFromErrno(TermiosError);

    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:

static PyObject* termios_tcgetattr ( PyObject self,
PyObject args 
) [static]

Definition at line 67 of file termios.c.

{
    int fd;
    struct termios mode;
    PyObject *cc;
    speed_t ispeed, ospeed;
    PyObject *v;
    int i;
    char ch;

    if (!PyArg_ParseTuple(args, "O&:tcgetattr",
                          fdconv, (void*)&fd))
        return NULL;

    if (tcgetattr(fd, &mode) == -1)
        return PyErr_SetFromErrno(TermiosError);

    ispeed = cfgetispeed(&mode);
    ospeed = cfgetospeed(&mode);

    cc = PyList_New(NCCS);
    if (cc == NULL)
        return NULL;
    for (i = 0; i < NCCS; i++) {
        ch = (char)mode.c_cc[i];
        v = PyBytes_FromStringAndSize(&ch, 1);
        if (v == NULL)
            goto err;
        PyList_SetItem(cc, i, v);
    }

    /* Convert the MIN and TIME slots to integer.  On some systems, the
       MIN and TIME slots are the same as the EOF and EOL slots.  So we
       only do this in noncanonical input mode.  */
    if ((mode.c_lflag & ICANON) == 0) {
        v = PyLong_FromLong((long)mode.c_cc[VMIN]);
        if (v == NULL)
            goto err;
        PyList_SetItem(cc, VMIN, v);
        v = PyLong_FromLong((long)mode.c_cc[VTIME]);
        if (v == NULL)
            goto err;
        PyList_SetItem(cc, VTIME, v);
    }

    if (!(v = PyList_New(7)))
        goto err;

    PyList_SetItem(v, 0, PyLong_FromLong((long)mode.c_iflag));
    PyList_SetItem(v, 1, PyLong_FromLong((long)mode.c_oflag));
    PyList_SetItem(v, 2, PyLong_FromLong((long)mode.c_cflag));
    PyList_SetItem(v, 3, PyLong_FromLong((long)mode.c_lflag));
    PyList_SetItem(v, 4, PyLong_FromLong((long)ispeed));
    PyList_SetItem(v, 5, PyLong_FromLong((long)ospeed));
    PyList_SetItem(v, 6, cc);
    if (PyErr_Occurred()){
        Py_DECREF(v);
        goto err;
    }
    return v;
  err:
    Py_DECREF(cc);
    return NULL;
}

Here is the call graph for this function:

static PyObject* termios_tcsendbreak ( PyObject self,
PyObject args 
) [static]

Definition at line 214 of file termios.c.

{
    int fd, duration;

    if (!PyArg_ParseTuple(args, "O&i:tcsendbreak",
                          fdconv, &fd, &duration))
        return NULL;
    if (tcsendbreak(fd, duration) == -1)
        return PyErr_SetFromErrno(TermiosError);

    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:

static PyObject* termios_tcsetattr ( PyObject self,
PyObject args 
) [static]

Definition at line 144 of file termios.c.

{
    int fd, when;
    struct termios mode;
    speed_t ispeed, ospeed;
    PyObject *term, *cc, *v;
    int i;

    if (!PyArg_ParseTuple(args, "O&iO:tcsetattr",
                          fdconv, &fd, &when, &term))
        return NULL;
    if (!PyList_Check(term) || PyList_Size(term) != 7) {
        PyErr_SetString(PyExc_TypeError,
                     "tcsetattr, arg 3: must be 7 element list");
        return NULL;
    }

    /* Get the old mode, in case there are any hidden fields... */
    if (tcgetattr(fd, &mode) == -1)
        return PyErr_SetFromErrno(TermiosError);
    mode.c_iflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 0));
    mode.c_oflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 1));
    mode.c_cflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 2));
    mode.c_lflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 3));
    ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4));
    ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5));
    cc = PyList_GetItem(term, 6);
    if (PyErr_Occurred())
        return NULL;

    if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) {
        PyErr_Format(PyExc_TypeError,
            "tcsetattr: attributes[6] must be %d element list",
                 NCCS);
        return NULL;
    }

    for (i = 0; i < NCCS; i++) {
        v = PyList_GetItem(cc, i);

        if (PyBytes_Check(v) && PyBytes_Size(v) == 1)
            mode.c_cc[i] = (cc_t) * PyBytes_AsString(v);
        else if (PyLong_Check(v))
            mode.c_cc[i] = (cc_t) PyLong_AsLong(v);
        else {
            PyErr_SetString(PyExc_TypeError,
     "tcsetattr: elements of attributes must be characters or integers");
                        return NULL;
                }
    }

    if (cfsetispeed(&mode, (speed_t) ispeed) == -1)
        return PyErr_SetFromErrno(TermiosError);
    if (cfsetospeed(&mode, (speed_t) ospeed) == -1)
        return PyErr_SetFromErrno(TermiosError);
    if (tcsetattr(fd, when, &mode) == -1)
        return PyErr_SetFromErrno(TermiosError);

    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:


Variable Documentation

struct PyModuleDef [static]
Initial value:
 {
    PyModuleDef_HEAD_INIT,
    "termios",
    termios__doc__,
    -1,
    termios_methods,
    NULL,
    NULL,
    NULL,
    NULL
}

Definition at line 943 of file termios.c.

struct constant termios_constants[] [static]
Initial value:
{
    {"tcgetattr", termios_tcgetattr,
     METH_VARARGS, termios_tcgetattr__doc__},
    {"tcsetattr", termios_tcsetattr,
     METH_VARARGS, termios_tcsetattr__doc__},
    {"tcsendbreak", termios_tcsendbreak,
     METH_VARARGS, termios_tcsendbreak__doc__},
    {"tcdrain", termios_tcdrain,
     METH_VARARGS, termios_tcdrain__doc__},
    {"tcflush", termios_tcflush,
     METH_VARARGS, termios_tcflush__doc__},
    {"tcflow", termios_tcflow,
     METH_VARARGS, termios_tcflow__doc__},
    {NULL, NULL}
}

Definition at line 294 of file termios.c.

Definition at line 41 of file termios.c.