summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnatoly Scherbakov <altaisoft@gmail.com>2020-09-12 22:45:44 +0700
committerAnatoly Scherbakov <altaisoft@gmail.com>2020-09-12 22:45:44 +0700
commit7c7f05c944ca71af5c52b3b321096909d0a11f6b (patch)
tree8bd9e37770203c5a8732abe4409bcc8daced7201
parent65987a39996c045cb9b7156723369483040ab5df (diff)
downloadrdflib-7c7f05c944ca71af5c52b3b321096909d0a11f6b.tar.gz
#1160 removed .defrag() and added a test
-rw-r--r--rdflib/parser.py52
-rw-r--r--test/test_create_input_source.py4
-rw-r--r--test/test_issue1160.py32
3 files changed, 74 insertions, 14 deletions
diff --git a/rdflib/parser.py b/rdflib/parser.py
index cad000e1..6cdc0916 100644
--- a/rdflib/parser.py
+++ b/rdflib/parser.py
@@ -206,7 +206,10 @@ def create_input_source(
"""
# test that exactly one of source, location, file, and data is not None.
- non_empty_arguments = list(filter(bool, [source, location, file, data]))
+ non_empty_arguments = list(filter(
+ lambda v: v is not None,
+ [source, location, file, data],
+ ))
if len(non_empty_arguments) != 1:
raise ValueError(
@@ -248,20 +251,19 @@ def create_input_source(
absolute_location = None # Further to fix for issue 130
auto_close = False # make sure we close all file handles we open
+
if location is not None:
- # Fix for Windows problem https://github.com/RDFLib/rdflib/issues/145
- if os.path.exists(location):
- location = pathname2url(location)
- base = urljoin("file:", "%s/" % pathname2url(os.getcwd()))
- absolute_location = URIRef(location, base=base)
- if absolute_location.startswith("file:///"):
- filename = url2pathname(absolute_location.replace("file:///", "/"))
- file = open(filename, "rb")
- else:
- input_source = URLInputSource(absolute_location, format)
- auto_close = True
- # publicID = publicID or absolute_location # Further to fix
- # for issue 130
+ (
+ absolute_location,
+ auto_close,
+ file,
+ input_source,
+ ) = _create_input_source_from_location(
+ file=file,
+ format=format,
+ input_source=input_source,
+ location=location,
+ )
if file is not None:
input_source = FileInputSource(file)
@@ -282,3 +284,25 @@ def create_input_source(
elif input_source.getPublicId() is None:
input_source.setPublicId(absolute_location or "")
return input_source
+
+
+def _create_input_source_from_location(file, format, input_source, location):
+ # Fix for Windows problem https://github.com/RDFLib/rdflib/issues/145
+ if os.path.exists(location):
+ location = pathname2url(location)
+
+ base = urljoin("file:", "%s/" % pathname2url(os.getcwd()))
+
+ absolute_location = URIRef(location, base=base)
+
+ if absolute_location.startswith("file:///"):
+ filename = url2pathname(absolute_location.replace("file:///", "/"))
+ file = open(filename, "rb")
+ else:
+ input_source = URLInputSource(absolute_location, format)
+
+ auto_close = True
+ # publicID = publicID or absolute_location # Further to fix
+ # for issue 130
+
+ return absolute_location, auto_close, file, input_source
diff --git a/test/test_create_input_source.py b/test/test_create_input_source.py
index 0182171d..6fc05f9f 100644
--- a/test/test_create_input_source.py
+++ b/test/test_create_input_source.py
@@ -19,3 +19,7 @@ class ParserTestCase(unittest.TestCase):
source='a',
location='b',
)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/test_issue1160.py b/test/test_issue1160.py
new file mode 100644
index 00000000..19258be7
--- /dev/null
+++ b/test/test_issue1160.py
@@ -0,0 +1,32 @@
+import unittest
+from unittest import mock
+
+import rdflib
+
+from rdflib import ConjunctiveGraph
+from rdflib.parser import URLInputSource
+
+QUERY = '''
+SELECT DISTINCT ?g
+FROM NAMED <http://ns.example.com/named#>
+WHERE {
+ GRAPH ?g {
+ ?s ?p ?o .
+ }
+}
+'''
+
+
+class NamedGraphWithFragmentTest(unittest.TestCase):
+ def test_named_graph_with_fragment(self):
+ """Test that fragment part of the URL is not erased."""
+ graph = ConjunctiveGraph()
+
+ with mock.patch('rdflib.parser.URLInputSource') as load_mock:
+ # We have to expect an exception here.
+ self.assertRaises(Exception, graph.query, QUERY)
+
+ load_mock.assert_called_with(
+ rdflib.URIRef('http://ns.example.com/named#'),
+ 'nt',
+ )