From e641ca4007b6e08a224084d622d5f796b002f7c9 Mon Sep 17 00:00:00 2001 From: Gunnar Aastrand Grimnes Date: Tue, 24 Jan 2017 13:59:26 +0100 Subject: [PATCH] fixes for turtle/trig namespace handling raise exception when trying to rebind a prefix to another ns. fix broken rebinding when generating prefixes This fixes #679 - but actually it's more like a work-around. The underlying problem is confusion about context and graph objects (#167) --- rdflib/plugins/serializers/turtle.py | 5 ++++- test/test_trig.py | 2 +- test/test_trig_export.py | 23 ----------------------- 3 files changed, 5 insertions(+), 25 deletions(-) delete mode 100644 test/test_trig_export.py diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py index dbef7d497d..9027ed3def 100644 --- a/rdflib/plugins/serializers/turtle.py +++ b/rdflib/plugins/serializers/turtle.py @@ -47,6 +47,8 @@ def __init__(self, store): self.reset() def addNamespace(self, prefix, uri): + if prefix in self.namespaces and self.namespaces[prefix]!=uri: + raise Exception("Trying to override namespace prefix %s => %s, but it's already bound to %s"%(prefix, uri, self.namespaces[prefix])) self.namespaces[prefix] = uri def checkSubject(self, subject): @@ -195,7 +197,8 @@ def addNamespace(self, prefix, namespace): p = "p" + p self._ns_rewrite[prefix] = p - prefix = self._ns_rewrite.get(prefix, prefix) + prefix = self._ns_rewrite.get(prefix, prefix) + super(TurtleSerializer, self).addNamespace(prefix, namespace) return prefix diff --git a/test/test_trig.py b/test/test_trig.py index 90dbeb078f..705bc0d971 100644 --- a/test/test_trig.py +++ b/test/test_trig.py @@ -118,7 +118,7 @@ def testGraphParsing(self): def testRoundTrips(self): - raise unittest.SkipTest('skipped until 5.0') + self.skipTest('skipped until 5.0') data = """ . diff --git a/test/test_trig_export.py b/test/test_trig_export.py deleted file mode 100644 index 6cf82fde38..0000000000 --- a/test/test_trig_export.py +++ /dev/null @@ -1,23 +0,0 @@ -from rdflib import URIRef, Literal, Graph, Dataset - -def trig_export_test(): - graphs = [(URIRef("urn:tg1"),"A"), (URIRef("urn:tg2"), "B")] - ds = Dataset() - for i, n in graphs: - g = ds.graph(i) - a = URIRef("urn:{}#S".format(n)) - b = URIRef("urn:{}#p".format(n)) - c = Literal('c') - g.add((a,b,c)) - - - # this generated two graphs, with a differnet namespace for the URIs inside. - # this tests that the prefix for each internal ns is different - - data = ds.serialize(format='trig') - print data - assert False - - -if __name__ == '__main__': - trig_export_test()