Skip to content

Difference in _use_fd_functions in shutil.py and test_shutil.py #119048

Closed

Description

@frenzymadness

Bug report

Bug description:

When switching from os.listdir to os.scandir 7 years ago somebody forgot to update the definition of _use_fd_functions in test_shutil together with shutil module.

in shutil:

_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
                     os.supports_dir_fd and
                     os.scandir in os.supports_fd and
                     os.stat in os.supports_follow_symlinks)

in test_shutil:

        _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
                             os.supports_dir_fd and
                             os.listdir in os.supports_fd and
                             os.stat in os.supports_follow_symlinks)

I believe there is no need to calculate this variable twice so I'm gonna propose a PR where I use the value from shutil.py in its tests. Nobody will ever forget to update both places again.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. frenzymadness commented on May 14, 2024

    @frenzymadness
    ContributorAuthor

    As I'm thinking about it more there might be a reason why this is calculated again in the tests and then compared with the value from the shutil module.

    But given the current status, this check:

            _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
                                 os.supports_dir_fd and
                                 os.listdir in os.supports_fd and
                                 os.stat in os.supports_follow_symlinks)
            if _use_fd_functions:
                self.assertTrue(shutil._use_fd_functions)
    

    in test_rmtree_uses_safe_fd_version_if_available checks whether both os.listdir and os.scandir are in os.supports_fd because that's the only difference between _use_fd_functions and shutil._use_fd_functions in this context.

    Does it make sense to test this?

  2. frenzymadness commented on May 14, 2024

    @frenzymadness
    ContributorAuthor

    Ccing @encukou since we were discussing it briefly.

  3. encukou commented on May 30, 2024

    @encukou
    Member

    If the difference is intentional, it should have a comment. I don't think it is.

    IMO, the repeated code in the test is not useful and we can use shutil._use_fd_functions directly.
    @serhiy-storchaka do you agree?

  4. vasiliyk commented on Sep 21, 2026

    @vasiliyk
    Contributor

    I've opened the PR implementing what @encukou suggested: the test now uses shutil._use_fd_functions directly instead of recomputing it with the stale os.listdir check.

  5. added a commit that references this issue on Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions