Skip to content

Commit

Permalink
Avoid raising unrelated AttributeError ... (#2013)
Browse files Browse the repository at this point in the history
from attempting to access language and datatype attributes of non-Literal objects

Added `isinstance` check to avoid AttributeErrors being raised by attempting to access `language` and `datatype`attribs of non-Literal objects being passed back up the call stack resulting from SPARQL BIND failures/errors.
  • Loading branch information
gjhiggins authored Jul 11, 2022
1 parent 9523d35 commit 7aba79a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rdflib/plugins/sparql/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ def Builtin_CONCAT(expr, ctx):

# dt/lang passed on only if they all match

dt = set(x.datatype for x in expr.arg)
dt = set(x.datatype for x in expr.arg if isinstance(x, Literal))
dt = dt.pop() if len(dt) == 1 else None

lang = set(x.language for x in expr.arg)
lang = set(x.language for x in expr.arg if isinstance(x, Literal))
lang = lang.pop() if len(lang) == 1 else None

return Literal("".join(string(x) for x in expr.arg), datatype=dt, lang=lang)
Expand Down
9 changes: 9 additions & 0 deletions test/test_issues/test_issue1873.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rdflib import Graph


def test_sparql_error_implicit_bind():
[r for r in Graph().query('SELECT (concat("", sha1(?x)) AS ?y) WHERE {}')]


def test_sparql_error_explicit_bind():
[r for r in Graph().query("SELECT ?v ?p ?m WHERE {?v ?p ?m BIND (sha1(?x) AS ?m)}")]

0 comments on commit 7aba79a

Please sign in to comment.