Python error on session.close()

19 views
Skip to first unread message

Dominique Guardiola Falco

unread,
Jun 5, 2011, 10:12:59 AM6/5/11
to sur...@googlegroups.com
I have this simple function :

def get_schema_label(uri):
    import surf
    store = surf.Store(reader='rdflib',writer='rdflib',rdflib_store='IOMemory')
    session = surf.Session(store)
    store.load_triples(source=uri)
    ontology = session.get_class(surf.ns.OWL.Ontology)
    o = ontology(uri)
    label = unicode(o.rdfs_label.first)
    store.clear()
    store.close()
    #session.close()
    return(label)
   
get_schema_label('http://www.w3.org/ns/org#')

it works, but I uncomment session.close() I get this error

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/dom/django/base/d2rq/models.py", line 27, in save
    self.label = get_schema_label(self.uri)
  File "/Users/dom/django/base/d2rq/models.py", line 16, in get_schema_label
    session.close()
  File "/Users/dom/django/.virtualenvs/base/lib/python2.7/site-packages/SuRF-1.1.4_r352-py2.7.egg/surf/session.py", line 287, in close
    for store in self.__stores:
RuntimeError: dictionary changed size during iteration

If I don't close my sessions I suppose this will have an impact on memory ?

Dominique Guardiola Falco

unread,
Jun 5, 2011, 10:37:51 AM6/5/11
to sur...@googlegroups.com
The function works better like this (but the python error on session.close() remains)


def get_schema_label(uri):
    import surf
    store = surf.Store(reader='rdflib',writer='rdflib',rdflib_store='IOMemory')
    session = surf.Session(store)
    store.load_triples(source=uri)
    ontology = session.get_class(surf.ns.OWL.Ontology)
    all = ontology.all()
    o = all.first()

Christoph Burgmer

unread,
Jun 5, 2011, 2:30:44 PM6/5/11
to sur...@googlegroups.com, Dominique Guardiola Falco
> it works, but I uncomment session.close() I get this error
>
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> File "/Users/dom/django/base/d2rq/models.py", line 27, in save
> self.label = get_schema_label(self.uri)
> File "/Users/dom/django/base/d2rq/models.py", line 16, in
> get_schema_label session.close()
> File
> "/Users/dom/django/.virtualenvs/base/lib/python2.7/site-packages/SuRF-1.1.4
> _r352-py2.7.egg/surf/session.py", line 287, in close
> for store in self.__stores:
> RuntimeError: dictionary changed size during iteration

Can you edit the source code and add a .copy() to this line 287? It should
then look like this:

def close(self):
for store in self.__stores.copy():
self.__stores[store].close()
del self.__stores[store]

Python does not allow for changing the dictionary while iterating over it.

-Christoph

Reply all
Reply to author
Forward
0 new messages