Skip to content

Commit

Permalink
T150521: Don't crash on queries with optional values
Browse files Browse the repository at this point in the history
The current implementation assumes variables are always set. When using sparql OPTIONAL sometimes values are not set. In that case, set it to None

Change-Id: I4af22d4faf42ca1e20581c9dd094237cf3467f71
  • Loading branch information
multichill authored and [[mw:User:Multichill]] committed Dec 1, 2016
1 parent 6d1e785 commit b8a2684
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pywikibot/data/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ def select(self, query, full_data=False, headers=DEFAULT_HEADERS):
for row in data['results']['bindings']:
values = {}
for var in qvars:
if full_data:
if row[var]['type'] not in VALUE_TYPES:
raise ValueError('Unknown type: %s' % row[var]['type'])
valtype = VALUE_TYPES[row[var]['type']]
values[var] = valtype(row[var], entity_url=self.entity_url)
if var in row:
if full_data:
if row[var]['type'] not in VALUE_TYPES:
raise ValueError('Unknown type: %s' % row[var]['type'])
valtype = VALUE_TYPES[row[var]['type']]
values[var] = valtype(row[var], entity_url=self.entity_url)
else:
values[var] = row[var]['value']
else:
values[var] = row[var]['value']
# var is not available (OPTIONAL is probably used)
values[var] = None
result.append(values)
return result
else:
Expand Down

0 comments on commit b8a2684

Please sign in to comment.