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

GH-120754: Remove isatty call during regular open #121593

Closed
wants to merge 5 commits into from

Commits on Jul 10, 2024

  1. pythonGH-120754: Remove isatty call during regular read

    For POSIX, TTYs are never regular files, so if the interpreter knows the
    file is regular it doesn't need to do an additional system call to check
    if the file is a TTY.
    
    The `open()` Python builtin requires a `stat` call at present in order
    to ensure the file being opened isn't a directory. That result includes
    the file mode which tells us if it is a regular file. There are a number
    of attributes from the stat which are stashed one off currently, move to
    stashing the whole object rather than just individual members.
    
    The stat object is reasonably large and currently the
    `stat_result.st_size` member cannot be modified from Python, which is
    needed by the `_pyio` implementation, so make the whole stat object
    optional. In the `_io` implementation this makes handling a stat
    failure simpler. At present there is no explicit user call to clear it,
    but if one is needed (ex. a program which has a lot of open FileIO
    objects and the memory becomes a problem) it would be straightforward to
    add. Ideally would be able to automatically clear (the values are
    generally used during I/O object initialization and not after. After a
    `write` they are no longer useful in current cases).
    
    It is fairly common pattern to scan a directory, look at the `stat`
    results (ex. is this file changed), and then open/read the file. In this
    PR I didn't update open's API to allow passing in a stat result to use,
    but that could be beneficial for some cases (ex. `importlib`).
    
    With this change on my Linux machine reading a small plain text file is
    down to 6 system calls.
    
    ```python
    openat(AT_FDCWD, "read_one.py", O_RDONLY|O_CLOEXEC) = 3
    fstat(3, {st_mode=S_IFREG|0644, st_size=87, ...}) = 0
    lseek(3, 0, SEEK_CUR)                   = 0
    read(3, "from pathlib import Path\n\npath ="..., 88) = 87
    read(3, "", 1)                          = 0
    close(3)                                = 0
    ```
    cmaloney committed Jul 10, 2024
    Configuration menu
    Copy the full SHA
    36e9061 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    a51e8ee View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    bc55828 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    991fb01 View commit details
    Browse the repository at this point in the history

Commits on Jul 11, 2024

  1. Configuration menu
    Copy the full SHA
    a487c6b View commit details
    Browse the repository at this point in the history