Skip to content

Commit

Permalink
make SparqlStore close underlying urllib response body
Browse files Browse the repository at this point in the history
fixes #638
  • Loading branch information
gromgull committed Jan 12, 2017
1 parent 3e74b39 commit 5e2c71e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import re
import collections
import contextlib
import urllib2
import warnings

Expand Down Expand Up @@ -330,7 +331,9 @@ def query(self, query,
self.timeout = self._timeout
self.setQuery(query)

return Result.parse(SPARQLWrapper.query(self).response)
with contextlib.closing(SPARQLWrapper.query(self).response) as res:
return Result.parse(res)


def triples(self, (s, p, o), context=None):
"""
Expand Down Expand Up @@ -418,7 +421,9 @@ def triples(self, (s, p, o), context=None):
self.timeout = self._timeout
self.setQuery(query)

doc = ElementTree.parse(SPARQLWrapper.query(self).response)
with contextlib.closing(SPARQLWrapper.query(self).response) as res:
doc = ElementTree.parse(res)

# ElementTree.dump(doc)
for rt, vars in _traverse_sparql_result_dom(
doc,
Expand Down Expand Up @@ -449,7 +454,8 @@ def __len__(self, context=None):
if self._is_contextual(context):
self.addParameter("default-graph-uri", context.identifier)
self.setQuery(q)
doc = ElementTree.parse(SPARQLWrapper.query(self).response)
with contextlib.closing(SPARQLWrapper.query(self).response) as res:
doc = ElementTree.parse(res)
rt, vars = iter(
_traverse_sparql_result_dom(
doc,
Expand Down Expand Up @@ -485,7 +491,8 @@ def contexts(self, triple=None):
else:
self.setQuery('SELECT ?name WHERE { GRAPH ?name {} }')

doc = ElementTree.parse(SPARQLWrapper.query(self).response)
with contextlib.closing(SPARQLWrapper.query(self).response) as res:
doc = ElementTree.parse(res)

return (
rt.get(Variable("name"))
Expand Down Expand Up @@ -770,7 +777,8 @@ def _do_update(self, update):
# we must read (and discard) the whole response
# otherwise the network socket buffer will at some point be "full"
# and we will block
result.response.read()
with contextlib.closing(result.response) as res:
res.read()

def update(self, query,
initNs={},
Expand Down

0 comments on commit 5e2c71e

Please sign in to comment.