Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions webkit_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
SERVER_EXEC = os.path.abspath(os.path.join(os.path.dirname(__file__),
'webkit_server'))

if sys.version_info[0] == 2:
PY2 = True
PY3 = False
elif sys.version_info[0] == 3:
PY2 = False
PY3 = True


class SelectionMixin(object):
""" Implements a generic XPath selection for a class providing
Expand Down Expand Up @@ -513,12 +520,47 @@ def issue_command(self, cmd, *args):
self._writeline(cmd)
self._writeline(str(len(args)))
for arg in args:
arg = str(arg)
self._writeline(str(len(arg)))
self._sock.sendall(arg.encode("utf-8"))
self.send_arg(arg)

return self._read_response()

def send_arg(self, arg):
""" Send each arg for args in issue_command """
arg = self.prepare_for_socket_sendall(arg)
self._writeline(str(len(arg)))
self._sock.sendall(arg)

def prepare_for_socket_sendall(self, arg):
""" Deal with the unicode and bytes problem in Python 2 and Python 3 for socket.sendall """

if PY2:
if type(arg) != unicode:
Copy link
Author

Choose a reason for hiding this comment

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

I am not sure if this raise an NameError in Python 3 since there's no unicode type in Python 3.
But I've tested it in Python 3 and it worked.
Would be appreciate if anyone has some advices on this.

Choose a reason for hiding this comment

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

While this would be a problem in other languages such as C++, where identifiers need to be defined at compilation time even if they are never used at runtime, this should not be an issue for Python, since here definitions of identifiers are only checked when encountered, and not during the syntax check.

In Python, it is also common to import packages only when they are required, and therefore import statements can be nested into if statements, indicating that an identifier does not have to be defined until it is encountered by the interpreter.

Therefore, I do not expect to find any problems with this line of code. In addition, I have successfully tested the suggested changes while observing no error messages or warnings.

try:
arg = str(arg)
except:
pass

# socket.sendall in Python 2 accepts str (bytes)
if type(arg) not in (str, unicode):
raise TypeError("type({}) should be str (bytes) or unicode, not {}".format(arg, type(arg)))
elif type(arg) == unicode:
arg = arg.encode("utf-8")

if PY3:
if type(arg) != bytes:
try:
arg = str(arg)
except:
pass

# socket.sendall in Python 3 accepts bytes
if type(arg) not in (str, bytes):
raise TypeError("type({}) should be str or bytes, not {}".format(arg, type(arg)))
elif type(arg) == str:
arg = arg.encode("utf-8")

return arg

def _read_response(self):
""" Reads a complete response packet from the server """
result = self.buf.read_line().decode("utf-8")
Expand All @@ -538,4 +580,5 @@ def _read_message(self):

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