Message262440
Ok, I now understand the problem better. They are two kinds of io objects:
(1) object which directly or indirectly owns a limited resource like file descriptor => must emit a ResourceWarning
(2) object which don't own a limited resource => no ResourceWarning must be logged
Examples of (1): FileIO, BufferedReader(FileIO), TextIOWrapper(BufferedReader(FileIO))
Examples of (2): BytesIO, BuffereadReader(BytesIO), TextIOWrapper(BuffereadReader(BytesIO))
The tricky part is to decide if an object owns a limited resource or not. Currently, the io module uses the _dealloc_warn() trick. A close() method tries to call _dealloc_warn(), but it ignores any exception when calling _dealloc_warn(). BufferedReader calls raw._dealloc_warn(). TextIOWrapper calls buffer._dealloc_warn().
For case (1), BufferedReader(FileIO).close() calls FileIO._dealloc_warn() => ResourceWarning is logged
For case (2), BufferedReader(BytesIO).close() calls BytesIO._dealloc_warn() raises an AttributeError => no warning is logged
Well, we can call this a hack, but it works :-) pyio_res_warn-3.patch implements the same hack in _pyio. |
|
Date |
User |
Action |
Args |
2016-03-25 13:45:40 | vstinner | set | recipients:
+ vstinner, pitrou, Arfrever, akira, martin.panter, piotr.dobrogost, serhiy.storchaka |
2016-03-25 13:45:40 | vstinner | set | messageid: <1458913540.92.0.138284270703.issue19829@psf.upfronthosting.co.za> |
2016-03-25 13:45:40 | vstinner | link | issue19829 messages |
2016-03-25 13:45:40 | vstinner | create | |
|