[New-bugs-announce] [issue28867] NamedTemporaryFile does not generate a ResourceWarning for unclosed files (unlike TemporaryFile)

Jon Dufresne report at bugs.python.org
Sun Dec 4 12:47:27 EST 2016


New submission from Jon Dufresne:

When using unittest, I'll frequently enable -Wall to help catch code smells and potential bugs.

One feature of this, I'm told when files aren't explicitly closed with an error like: "ResourceWarning: unclosed file <...>". I've noticed this warning is generated for an unclosed tempfile.TemporaryFile (good), but not for an unclosed tempfile.NamedTemporaryFile (possible bug).

I've verified this on Python 3.5 and 3.6.

Example test:

```
import tempfile
import unittest


class MyTest(unittest.TestCase):
    def test_temporary_file(self):
        tempfile.TemporaryFile()

    def test_named_temporary_file(self):
        tempfile.NamedTemporaryFile()
```

Actual output:

```
$ Python-3.6.0b4/python --version
Python 3.6.0b4
$ Python-3.6.0b4/python -Wall -m unittest -v test
test_named_temporary_file (test.MyTest) ... ok
test_temporary_file (test.MyTest) ... /home/jon/test.py:7: ResourceWarning: unclosed file <_io.BufferedRandom name=3>
  tempfile.TemporaryFile()
ok

----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK

```

Expected:

I expect both tests to generate a ResourceWarning, as neither test explicitly closes the file.

----------
components: Library (Lib)
messages: 282352
nosy: jdufresne
priority: normal
severity: normal
status: open
title: NamedTemporaryFile does not generate a ResourceWarning for unclosed files (unlike TemporaryFile)
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28867>
_______________________________________


More information about the New-bugs-announce mailing list