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

More reliable use of scandir in Path.glob() #84097

Closed
serhiy-storchaka opened this issue Mar 9, 2020 · 4 comments
Closed

More reliable use of scandir in Path.glob() #84097

serhiy-storchaka opened this issue Mar 9, 2020 · 4 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@serhiy-storchaka
Copy link
Member

BPO 39916
Nosy @serhiy-storchaka, @miss-islington
PRs
  • bpo-39916: Use os.scandir() as context manager in Path.glob(). #18880
  • [3.8] bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880) #18934
  • [3.7] bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880) #18935
  • 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 2020-03-11.17:07:50.377>
    created_at = <Date 2020-03-09.16:54:33.494>
    labels = ['3.8', '3.7', 'library', '3.9', 'performance']
    title = 'More reliable use of scandir in Path.glob()'
    updated_at = <Date 2020-03-11.17:07:50.376>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2020-03-11.17:07:50.376>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-03-11.17:07:50.377>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2020-03-09.16:54:33.494>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39916
    keywords = ['patch']
    message_count = 4.0
    messages = ['363750', '363934', '363942', '363944']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'miss-islington']
    pr_nums = ['18880', '18934', '18935']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue39916'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @serhiy-storchaka
    Copy link
    Member Author

    Path.glob() uses os.scandir() in the following code.

        entries = list(scandir(parent_path))

    It properly closes the internal file descriptor opened by scandir() if success because it is automatically closed closed when the iterator is exhausted. But if it was interrupted (by KeyboardInterrupt, MemoryError or OSError), the file descriptor will be closed only when the iterator be collected by the garbage collector. It is unreliable on implementations like PyPy and emits a ResourceWarning.

    The proposed code uses more reliable code

        with scandir(parent_path) as scandir_it:
            entries = list(scandir_it)

    which is used in other sites (in the shutil module). I have no idea why I did not write it in this form at first place.

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir performance Performance or resource usage labels Mar 9, 2020
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 704e206 by Serhiy Storchaka in branch 'master':
    bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880)
    704e206

    @miss-islington
    Copy link
    Contributor

    New changeset b1b1d5f by Miss Islington (bot) in branch '3.7':
    bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880)
    b1b1d5f

    @miss-islington
    Copy link
    Contributor

    New changeset c228799 by Miss Islington (bot) in branch '3.8':
    bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880)
    c228799

    @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 3.9 only security fixes performance Performance or resource usage stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants