Skip to content

Conversation

@quique0194
Copy link

Fix #27

@niklasb
Copy link
Owner

niklasb commented Dec 7, 2016

Can you please explain to me what this pull request changes specifically and why you feel the changes are necessary.

self._writeline(cmd)
self._writeline(str(len(args)))
for arg in args:
arg = str(arg)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function cannot handle unicode strings. Try the following:

str(u"canción")

if type(arg) == unicode:
arg = arg.encode("utf-8")
self._writeline(str(len(arg)))
self._sock.sendall(arg.encode("utf-8"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only unicode strings need to be encoded as utf-8 to be sent as bytes to webkit server


def _writeline(self, line):
""" Writes a line to the underlying socket. """
self._sock.sendall(line.encode("utf-8") + b"\n")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem if you encode twice. Try the following:

u"canción".encode('utf-8').encode('utf-8')

if type(line) == unicode:
line = line.encode("utf-8")
""" Writes a line to the underlying socket. """
self._sock.sendall(line.encode("utf-8") + b"\n")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding twice can create errors:

u"canción".encode('utf-8').encode('utf-8')

@quique0194
Copy link
Author

I was working with some forms. I had to add some unicode data like "Nuñez" but the following error showed up:

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/ubuntu/apurata/app/scripts/fetch_sbs_data.py", line 85, in fetch_sbs_data
    ret, html = get_data_from_sbs(data, proxy=proxy, tries=10)
  File "/home/ubuntu/apurata/app/lib/sbs.py", line 140, in get_data_from_sbs
    html_text = get_sbs_html(data, tries, proxy=proxy)
  File "/home/ubuntu/apurata/app/lib/sbs.py", line 56, in get_sbs_html
    ).set(data["last_name_m"].encode('utf-8'))
  File "/usr/local/lib/python2.7/dist-packages/webkit_server.py", line 93, in set
    self._invoke("set", value)
  File "/usr/local/lib/python2.7/dist-packages/webkit_server.py", line 207, in _invoke
    return self.client.issue_node_cmd(cmd, "false", self.node_id, *args)
  File "/usr/local/lib/python2.7/dist-packages/webkit_server.py", line 377, in issue_node_cmd
    return self.conn.issue_command("Node", *args)
  File "/usr/local/lib/python2.7/dist-packages/webkit_server.py", line 518, in issue_command
    self._sock.sendall(arg.encode("utf-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants