<commit_before>
static PyObject *
TTY_func_set_mode(TTY *self, PyObject *args)
{
    int r, mode;

    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "i:set_mode", &mode)) {
        return NULL;
    }

    r = uv_tty_set_mode((uv_tty_t *)UV_HANDLE(self), mode);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TTYError);
        return NULL;
    }

    Py_RETURN_NONE;
}


static PyObject *
TTY_func_reset_mode(PyObject *cls)
{
    UNUSED_ARG(cls);
    uv_tty_reset_mode();
    Py_RETURN_NONE;
}


static PyObject *
TTY_func_isatty(PyObject *cls, PyObject *args)
{
    int fd;

    UNUSED_ARG(cls);

    if (!PyArg_ParseTuple(args, "i:isatty", &fd)) {
        return NULL;
    }

    if (uv_guess_handle(fd) == UV_TTY) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}


static PyObject *
TTY_func_get_winsize(TTY *self)
{
    int r, width, height;

    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    r = uv_tty_get_winsize((uv_tty_t *)UV_HANDLE(self), &width, &height);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TTYError);
        return NULL;
    }

    return Py_BuildValue("(ii)", width, height);
}


static int
TTY_tp_init(TTY *self, PyObject *args, PyObject *kwargs)
{
    int fd, r;
    uv_tty_t *uv_tty;
    Loop *loop;
    PyObject *tmp = NULL;

    UNUSED_ARG(kwargs);

    if (UV_HANDLE(self)) {
        PyErr_SetString(PyExc_StreamError, "Object already initialized");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "O!i:__init__", &LoopType, &loop, &fd)) {
        return -1;
    }

    if (fd != 0 && fd != 1 && fd != 2) {
        PyErr_SetString(PyExc_TTYError, "Incorrect file descriptor specified");
        return -1;
    }

    tmp = (PyObject *)((Handle *)self)->loop;
    Py_INCREF(loop);
    ((Handle *)self)->loop = loop;
    Py_XDECREF(tmp);

    uv_tty = PyMem_Malloc(sizeof(uv_tty_t));
    if (!uv_tty) {
        PyErr_NoMemory();
        Py_DECREF(loop);
        return -1;
    }<commit_msg>Allow any file descriptor to be opened as a TTY<commit_after>
static PyObject *
TTY_func_set_mode(TTY *self, PyObject *args)
{
    int r, mode;

    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "i:set_mode", &mode)) {
        return NULL;
    }

    r = uv_tty_set_mode((uv_tty_t *)UV_HANDLE(self), mode);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TTYError);
        return NULL;
    }

    Py_RETURN_NONE;
}


static PyObject *
TTY_func_reset_mode(PyObject *cls)
{
    UNUSED_ARG(cls);
    uv_tty_reset_mode();
    Py_RETURN_NONE;
}


static PyObject *
TTY_func_isatty(PyObject *cls, PyObject *args)
{
    int fd;

    UNUSED_ARG(cls);

    if (!PyArg_ParseTuple(args, "i:isatty", &fd)) {
        return NULL;
    }

    if (uv_guess_handle(fd) == UV_TTY) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}


static PyObject *
TTY_func_get_winsize(TTY *self)
{
    int r, width, height;

    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    r = uv_tty_get_winsize((uv_tty_t *)UV_HANDLE(self), &width, &height);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TTYError);
        return NULL;
    }

    return Py_BuildValue("(ii)", width, height);
}


static int
TTY_tp_init(TTY *self, PyObject *args, PyObject *kwargs)
{
    int fd, r;
    uv_tty_t *uv_tty;
    Loop *loop;
    PyObject *tmp = NULL;

    UNUSED_ARG(kwargs);

    if (UV_HANDLE(self)) {
        PyErr_SetString(PyExc_StreamError, "Object already initialized");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "O!i:__init__", &LoopType, &loop, &fd)) {
        return -1;
    }

    tmp = (PyObject *)((Handle *)self)->loop;
    Py_INCREF(loop);
    ((Handle *)self)->loop = loop;
    Py_XDECREF(tmp);

    uv_tty = PyMem_Malloc(sizeof(uv_tty_t));
    if (!uv_tty) {
        PyErr_NoMemory();
        Py_DECREF(loop);
        return -1;
    }
{'fraction_non_alphanumeric': 0.13240763699479297, 'fraction_numerical': 0.0034713612695264073, 'mean_word_length': 2.990108803165183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1935544', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1574, 'n_words': 299}