This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.scandir: implement finalizer (for ResourceWarning)
Type: Stage:
Components: Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benhoyt, pitrou, python-dev, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-03-21 16:07 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
scandir_finalizer.patch vstinner, 2016-03-21 16:07 review
Messages (2)
msg262136 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-21 16:07
Attached patch implements a finalizer for os.scandir() iterator type. The patch also calls the new PyErr_ResourceWarning() function to be able to display in the warning where the iterator was created (allocated) when tracemalloc is enabled.

While it makes sense to pass an "open" socket to the logger of the ResourceWarning warning for sockets (issue #26590), maybe we can close the ScandirIterator *before* calling the logger?

Example:
---
import os
f = os.scandir('.')
f = None
---

Output:
---
$ ./python -X tracemalloc -Wd example.py 
example.py:3: ResourceWarning: unclosed scandir iterator <posix.ScandirIterator object at 0x7f21cdb15280>
  f = None
Object allocated at (most recent call first):
  File "example.py", lineno 2
    f = os.scandir('.')
---

See also the issue #26590 "socket destructor: implement finalizer".
msg262218 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-22 23:44
New changeset b65d783cfe3b by Victor Stinner in branch 'default':
Implement finalizer for os.scandir() iterator
https://hg.python.org/cpython/rev/b65d783cfe3b
History
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70790
2016-03-22 23:56:32vstinnersetstatus: open -> closed
resolution: fixed
2016-03-22 23:44:18python-devsetnosy: + python-dev
messages: + msg262218
2016-03-21 16:07:48vstinnersetfiles: + scandir_finalizer.patch
keywords: + patch
2016-03-21 16:07:42vstinnersetnosy: + benhoyt
2016-03-21 16:07:26vstinnercreate