Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Superfluous call to isatty in open() when buffering >= 0 #78251

Closed
Dav1dde mannequin opened this issue Jul 8, 2018 · 3 comments
Closed

Superfluous call to isatty in open() when buffering >= 0 #78251

Dav1dde mannequin opened this issue Jul 8, 2018 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes performance Performance or resource usage topic-IO

Comments

@Dav1dde
Copy link
Mannequin

Dav1dde mannequin commented Jul 8, 2018

BPO 34070
Nosy @vstinner, @Dav1dde
PRs
  • bpo-34070: only check for isatty when buffering < 0 #8187
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-10-19.22:34:22.048>
    created_at = <Date 2018-07-08.18:12:53.330>
    labels = ['3.7', '3.8', 'expert-IO', 'performance']
    title = 'Superfluous call to isatty in open() when buffering >= 0'
    updated_at = <Date 2018-10-19.22:34:22.046>
    user = 'https://github.com/Dav1dde'

    bugs.python.org fields:

    activity = <Date 2018-10-19.22:34:22.046>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-19.22:34:22.048>
    closer = 'vstinner'
    components = ['IO']
    creation = <Date 2018-07-08.18:12:53.330>
    creator = 'Dav1d'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34070
    keywords = ['patch']
    message_count = 3.0
    messages = ['321281', '328066', '328067']
    nosy_count = 2.0
    nosy_names = ['vstinner', 'Dav1d']
    pr_nums = ['8187']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue34070'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    @Dav1dde
    Copy link
    Mannequin Author

    Dav1dde mannequin commented Jul 8, 2018

    _iomodule.c:_io_open_impl checks for isatty even if not necessary (when buffering >= 0).

    Code:

    PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);

        {
            PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
            if (res == NULL)
                goto error;
            isatty = PyLong_AsLong(res);
            Py_DECREF(res);
            if (isatty == -1 && PyErr_Occurred())
                goto error;
        }
    
        if (buffering == 1 || (buffering < 0 && isatty)) {
            buffering = -1;
            line_buffering = 1;
        }
        else
            line_buffering = 0;

    Python Code to reproduce:

    with open('foo', 'rb', buffering=0) as f:
        f.read()

    This generates an error (can be seen with strace):

    ioctl(5, TCGETS, 0x7ffef1435b60) = -1 ENOTTY (Inappropriate ioctl for device)

    I'll open a PR shortly.

    @Dav1dde Dav1dde mannequin added 3.7 (EOL) end of life 3.8 only security fixes topic-IO performance Performance or resource usage labels Jul 8, 2018
    @vstinner
    Copy link
    Member

    New changeset 8deab96 by Victor Stinner (David Herberth) in branch 'master':
    bpo-34070: open() only checks for isatty if buffering < 0 (GH-8187)
    8deab96

    @vstinner
    Copy link
    Member

    David Herberth: Thanks for report the issue, I didn't noticed it previously even if I read strace output frequently! And thanks for the fix.

    This generates an error (can be seen with strace):
    ioctl(5, TCGETS, 0x7ffef1435b60) = -1 ENOTTY (Inappropriate ioctl for device)

    Honestly, I don't think that it's a major bug, so I suggest to not backport the change to 3.7 and older. I prefer to not change stable changes to avoid any risk of regression.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes performance Performance or resource usage topic-IO
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant