Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee803a8

Browse files
committed
Issue #26567: enhance ResourceWarning example
1 parent 74879e4 commit ee803a8

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

Doc/whatsnew/3.6.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,24 +272,27 @@ used to try to retrieve the traceback where the detroyed object was allocated.
272272

273273
Example with the script ``example.py``::
274274

275+
import warnings
276+
275277
def func():
276-
f = open(__file__)
277-
f = None
278+
return open(__file__)
278279

279-
func()
280+
f = func()
281+
f = None
280282

281283
Output of the command ``python3.6 -Wd -X tracemalloc=5 example.py``::
282284

283-
example.py:3: ResourceWarning: unclosed file <...>
285+
example.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='example.py' mode='r' encoding='UTF-8'>
284286
f = None
285287
Object allocated at (most recent call first):
286-
File "example.py", lineno 2
287-
f = open(__file__)
288-
File "example.py", lineno 5
289-
func()
288+
File "example.py", lineno 4
289+
return open(__file__)
290+
File "example.py", lineno 6
291+
f = func()
290292

291293
The "Object allocated at" traceback is new and only displayed if
292-
:mod:`tracemalloc` is tracing Python memory allocations.
294+
:mod:`tracemalloc` is tracing Python memory allocations and if the
295+
:mod:`warnings` was already imported.
293296

294297

295298
zipfile

0 commit comments

Comments
 (0)