From 70d9d041236f36b9589c25e6f4e83af03263dd99 Mon Sep 17 00:00:00 2001 From: blob8108 Date: Fri, 28 Mar 2014 22:12:52 +0000 Subject: Fix Unicode handling & investigate Scratch polling --- blockext/__init__.py | 16 ++++++++-------- blockext/scratch.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/blockext/__init__.py b/blockext/__init__.py index aebbb03..79338bd 100644 --- a/blockext/__init__.py +++ b/blockext/__init__.py @@ -33,6 +33,11 @@ except NameError: unicode = str unichr = chr +def make_unicode(text): + """Fix strings passed in from extension code.""" + if isinstance(text, bytes): + text = text.decode("utf-8") + return unicode(text) def unquote_unicode(text): def unicode_unquoter(match): @@ -103,11 +108,6 @@ class Blockext(BaseHTTPRequestHandler): response = "ERROR: Not found" status = 404 - if isinstance(response, bytes): - response = response.decode("utf-8") - else: - response = unicode(response) - if is_browser and mime_type == "text/plain": # Some browsers seem to hate plain text. response = u""" @@ -147,7 +147,7 @@ def handler(name, **kwargs): def menu(name, values): - Blockext.menus[name] = list(map(unicode, values)) + Blockext.menus[name] = list(map(make_unicode, values)) @handler("", hidden=True) @@ -246,7 +246,7 @@ class Block(object): if blocking and shape != "command": raise ValueError("only commands can be blocking") - self._text = text + self._text = make_unicode(text) self.shape = shape self.func = func self.is_blocking = blocking @@ -327,7 +327,7 @@ class Block(object): if self.shape == "command": result = None result = ("true" if result is True else "false" if result is False else - "" if result == None else unicode(result)) + "" if result == None else make_unicode(result)) if request_id and request_id in Blockext.requests: del Blockext.requests[request_id] return result diff --git a/blockext/scratch.py b/blockext/scratch.py index b4440ec..862bfc9 100644 --- a/blockext/scratch.py +++ b/blockext/scratch.py @@ -5,6 +5,17 @@ import json from blockext import * +def quote_unicode(text): + """Scratch expects non-standard unicode URL encoding.""" + def unicode_quoter(c): + v = ord(c) + if v < 128: + return quote(c) + else: + if v >= 65536: raise ValueError + return "%u" + hex(v)[:2] + return b"".join(map(unicode_quoter, text)) + CROSSDOMAIN_XML = """ @@ -66,6 +77,10 @@ def poll(is_browser=False): lines += "{path} {result}\n".format( path="/".join([name] + args), result=block(*args).replace("\n", " "), + # TODO Scratch's url-decoding is buggy. + #path="/".join(quote_unicode(p).replace("/", "%2F") + # for p in [name] + args) + #result=quote_unicode(block(*args).replace("\n", " ")), ) return ("text/plain", lines) -- cgit v1.3.1