Title: os.scandir: implement finalizer (for ResourceWarning)
Type: Stage:
Components: Versions: Python 3.6
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 2016-03-22 23:56 by vstinner. This issue is now closed.

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?

import os
f = os.scandir('.')
f = None

$ ./python -X tracemalloc -Wd ResourceWarning: unclosed scandir iterator <posix.ScandirIterator object at 0x7f21cdb15280>
  f = None
Object allocated at (most recent call first):
  File "", 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
Date User Action Args
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