Git Product home page Git Product logo

Comments (12)

joernhees avatar joernhees commented on May 27, 2024

notice that i don't have any intention to merge RDFLib/rdflib#550 as that would cut SPARQLWrapper to 1.7.1 instead of current 1.7.5, RDFLib/rdflib#550 and this issue is just to figure out where to fix this best (and quick)

from sparqlwrapper.

uholzer avatar uholzer commented on May 27, 2024

I think I found the problem.

Let's look at rdflib/plugins/stores/sparqlstore.py from rdflib, line 414:

doc = ElementTree.parse(SPARQLWrapper.query(self).response)

This line exptects SPARQLWrapper.query(self).response to be a file-like object in binary mode, i.e. yielding str, not unicode when calling read() on it in Python 2. (As it is the body of an HTTP response, this is what I would expect too.) Adding the line

print repr(SPARQLWrapper.query(self).response.read())

right in front of it and running the unit tests again gives a unicode string as output:

u'<?xml version="1.0"?>\n<sparql xmlns="http://www.w3.org/2005/sparql-results#">\n  <head>\n    <variable name="o"/>\n  </head>\n  <results>\n    <result>\n      <binding name="o">\n        <literal>1: adfk { \' \\" " { </literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>2: adfk } &lt;foo&gt; #\xe9\xef \\</literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>3: adfk { " \\\' \' { </literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>4: adfk } &lt;foo&gt; #\xe9\xef \\</literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>5: adfk { \' \\" " { </literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>6: adfk } &lt;foo&gt; #\xe9\xef \\</literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>7: ad adsfj &#x0A; { &#x0A; sadfj</literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>8: adfk { " \\\' \' { </literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>9: adfk } &lt;foo&gt; #\xe9\xef \\</literal>\n      </binding>\n    </result>\n    <result>\n      <binding name="o">\n        <literal>10: ad adsfj &#x0A; { &#x0A; sadfj</literal>\n      </binding>\n    </result>\n  </results>\n</sparql>\n'

Clearly, ElementTree then tries to decode this as UTF-8, which causes Python to implicitely encode it as ASCII beforehand. Replacing line 414 with

doc = ElementTree.fromstring(SPARQLWrapper.query(self).response.read().encode('UTF-8'))

fixes the unit test. Of course, I wouldn't recomend this as workaround. I think that SPARQLWrapper should return a file-like object in binary mode. Could it be that the new keepalive package introduced in commit 609961a is at fault?

from sparqlwrapper.

uholzer avatar uholzer commented on May 27, 2024

It turns out it is. keepalive.py provides a HTTPResponse which derives from httplib.HTTPResponse. It overrides the read function like this:

    def read(self, amt=None):
        ...

        s = self._rbuf + self._raw_read(amt).decode('UTF-8', 'ignore')
        self._rbuf = ''
        return s

from sparqlwrapper.

uholzer avatar uholzer commented on May 27, 2024

Reported this as an Issue against keepalive: wikier/keepalive#2

from sparqlwrapper.

joernhees avatar joernhees commented on May 27, 2024

@uholzer thanks for looking into this... can we just remove the .decode(...) stuff? @wikier ?

from sparqlwrapper.

wikier avatar wikier commented on May 27, 2024

@joernhees I think what's causing troubles to @uholzer.... we'll discuss it in wikier/keepalive#2 and try to get a fix out asap. I'll keep you posted, guys.

from sparqlwrapper.

wikier avatar wikier commented on May 27, 2024

@joernhees wikier/keepalive#2 provides a candidate patch. Besides manual testing, I'd welcome a unit test on our side testing the reported issue; is that possible?

from sparqlwrapper.

joernhees avatar joernhees commented on May 27, 2024

see https://travis-ci.org/RDFLib/rdflib/builds/92929930 (was fixed on py2 but some stuff still fails on py3)

from sparqlwrapper.

joernhees avatar joernhees commented on May 27, 2024

a unit test inside SPARQLWrapper for queries/data with unicode chars would propbably be a good idea...

from sparqlwrapper.

wikier avatar wikier commented on May 27, 2024

@joernhees see wikier/keepalive#2, fixed in wikier/keepalive@b1d2c32.

Definitely a unit test it's necessary. If no one contributed it, by the end of the week I'll to find time to take care of it before pushing out a new 1.7.6 release depending on keepalive>=0.5.

from sparqlwrapper.

wikier avatar wikier commented on May 27, 2024

any idea how this could be tested?

from sparqlwrapper.

wikier avatar wikier commented on May 27, 2024

OK, let's push this out for now.

from sparqlwrapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.