From 16f1d7d3f6f43465c0d65a3c3c3983dcaca3f66f Mon Sep 17 00:00:00 2001 From: Gerhard Weis Date: Mon, 3 Mar 2014 12:56:42 +1000 Subject: [PATCH] if query result source has no encoding set, fall back to utf-8 encoding. --- rdflib/plugins/sparql/results/csvresults.py | 5 ++++- rdflib/plugins/sparql/results/tsvresults.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rdflib/plugins/sparql/results/csvresults.py b/rdflib/plugins/sparql/results/csvresults.py index 88c95c30e..f5c811cf6 100644 --- a/rdflib/plugins/sparql/results/csvresults.py +++ b/rdflib/plugins/sparql/results/csvresults.py @@ -23,7 +23,10 @@ def parse(self, source): r = Result('SELECT') - if hasattr(source, 'mode') and 'b' in source.mode: + if not hasattr(source, 'mode') or 'b' in source.mode and not getattr(source, 'encoding', None): + # if there is no mode, or source is in binary mode + # assume we need to decode from utf-8. + # if there is a mode set and it's not binary, check if source has encoding set. source = codecs.getreader('utf-8')(source) reader = csv.reader(source, delimiter=self.delim) diff --git a/rdflib/plugins/sparql/results/tsvresults.py b/rdflib/plugins/sparql/results/tsvresults.py index b85f61b85..b49959710 100644 --- a/rdflib/plugins/sparql/results/tsvresults.py +++ b/rdflib/plugins/sparql/results/tsvresults.py @@ -40,7 +40,10 @@ class TSVResultParser(ResultParser): def parse(self, source): - if hasattr(source, 'mode') and 'b' in source.mode: + if not hasattr(source, 'mode') or 'b' in source.mode and not getattr(source, 'encoding', None): + # if there is no mode, or source is in binary mode + # assume we need to decode from utf-8. + # if there is a mode set and it's not binary, check if source has encoding set. source = codecs.getreader('utf-8')(source) try: