sijax-python icon indicating copy to clipboard operation
sijax-python copied to clipboard

Fixed bug #11 using "bytes" method

Open RoboMod opened this issue 11 years ago • 4 comments

Should fix: spantaleev/flask-sijax#11

RoboMod avatar Feb 04 '15 14:02 RoboMod

Hmm, have you tried removing the explicit conversion to str? The proper way, I believe would be to encode all unicode strings from the Python side, send them to the Javascript where its decoded as a utf-8 encoded JSON. I'm not sure I've grasped all the places in Sijax and Flask-Sijax where this occurs. I suppose we should generate another set of test cases that covers these issues.

Midnighter avatar Feb 04 '15 15:02 Midnighter

Yes, I tried it without any conversion but that doesn't work. Your right about the JSON encoding, but in combination with iframes the strings have to be executable scripts. So there is no JS-process to decode data from server.

RoboMod avatar Feb 04 '15 16:02 RoboMod

So I played with this some more and it's a little tricky. bytes in Python 3 takes an encoding and then basically just encodes the string as I have suggested above. In Python 2 bytes is just a synonym for str and takes no encoding argument. I tried bytes from the future module which unifies this for Python2&3 (from builtins import bytes) but then werkzeug complains because they check assert type(data) is bytes rather than assert isinstance(data, bytes) which would be better.

So two solutions:

  1. Simply yield string.encode("utf-8")
  2. I will contact the werkzeug maintainer because one should always use an isinstance check to allow for inheritance. Then we could use bytes from the future module. Although we don't gain much from that.

Midnighter avatar Feb 10 '15 15:02 Midnighter

Since option (2) would only work with newer versions of werkzeug anyway, I'd suggest to go with (1). So just change the yield line and at the top change line 21 to: from builtins import next.

Midnighter avatar Feb 11 '15 09:02 Midnighter