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: io.BufferedReader hides ResourceWarnings when garbage collected
Type: behavior Stage: resolved
Components: IO Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-07-26 11:20 by Claudiu.Popa, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
buffered_io_resource_warning.patch Claudiu.Popa, 2014-07-26 11:20 review
Messages (6)
msg224040 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-07-26 11:20
Given the following example, Python 3.5 doesn't emit any resource warning:

import io, gc
f = open("a")
bufio = io.BufferedReader(f)
gc.collect()


Here's a small patch that enables this.
msg224041 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-26 11:40
All works to me.

$ ./python -Wall -c "open('/dev/null', 'rb')"
-c:1: ResourceWarning: unclosed file <_io.BufferedReader name='/dev/null'>
msg224043 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-07-26 11:42
That's not the same, try with my example. open("a") will be a TextIOWrapper.
msg224045 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-07-26 11:47
I mean this one:

$ python_d -Wall -c "f=open('a', 'r'); import io; io.BufferedReader(f)"
msg224046 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-26 11:49
This example is not correct.

1) Argument of BufferedReader should be binary stream.

>>> import io, gc
>>> f = open('/dev/null')
>>> bufio = io.BufferedReader(f)
>>> bufio.read(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'readinto'

2) gc.collect() doesn't collect file streams because references to them are saved in local variables.
msg224047 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-07-26 11:54
You're right, thanks for the new information. You can close the issue then.
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66276
2014-07-26 12:01:24serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2014-07-26 11:54:04Claudiu.Popasetmessages: + msg224047
2014-07-26 11:49:27serhiy.storchakasetmessages: + msg224046
2014-07-26 11:47:09Claudiu.Popasetmessages: + msg224045
2014-07-26 11:42:09Claudiu.Popasetmessages: + msg224043
2014-07-26 11:40:09serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg224041
2014-07-26 11:20:54Claudiu.Popacreate