"ResourceWarning: unclosed file" when using Graph.parse() with python3 · Issue #651 · RDFLib/rdflib (original) (raw)

Hi,

With python 3, when invoking the parse() function on turtle files as part of our testing suite, we get the following warning:

ResourceWarning: unclosed file <_io.BufferedReader name='<PATH_TO_TTL_FILE>'>

The full test report is available here.

The warning can be reproduced with this smaller piece of code:

$ python test_rdflib_warning.py 
.test_rdflib_warning.py:23: ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/file.ttl'>
  g2.parse(local_ttl_file, format='turtle')
.
----------------------------------------------------------------------
Ran 2 tests in 3.709s

OK

Where test_rdflib_warning.py contains:

from rdflib.graph import Graph
import urllib.request
import unittest


class TestResourceWarning(unittest.TestCase):

    def test_url(self):
        # No warning
        ttl_url = \
            "https://provenance.ecs.soton.ac.uk/store/documents/114154.ttl"

        g1 = Graph()
        g1.parse(ttl_url, format='turtle')

    def test_url_local_copy(self):
        # Warning
        # test_rdflib_warning.py:23: ResourceWarning: unclosed file
        # <_io.BufferedReader name='/tmp/file.ttl'>
        ttl_url = \
            "https://provenance.ecs.soton.ac.uk/store/documents/114154.ttl"

        local_ttl_file = '/tmp/file.ttl'
        urllib.request.urlretrieve(ttl_url, '/tmp/file.ttl')

        g2 = Graph()
        g2.parse(local_ttl_file, format='turtle')

if __name__ == '__main__':
    unittest.main()

We are using rdflib 4.2.1 (latest version on pypi).

Is it possible that the turtle parser is not closing the turtle file after reading it?