Merge pull request #1361 from ilvalle/py3_fixes_step2

few py3 fixes
This commit is contained in:
mdipierro
2016-06-18 07:48:39 -05:00
committed by GitHub
24 changed files with 194 additions and 168 deletions
+6 -12
View File
@@ -6,15 +6,10 @@
from __future__ import print_function
import re
import urllib
from cgi import escape
from gluon._compat import maketrans, urllib_quote, unicodeT, _local_html_escape, to_bytes
from gluon._compat import maketrans, urllib_quote, unicodeT, _local_html_escape, to_bytes, to_native, _local_html_escape as escape
from ast import parse as ast_parse
import ast
try:
from ast import parse as ast_parse
import ast
except ImportError: # python 2.5
from compiler import parse
import compiler.ast as ast
"""
TODO: next version should use MathJax
@@ -950,12 +945,11 @@ def render(text,
if protolinks == "default":
protolinks = protolinks_simple
pp = '\n' if pretty_print else ''
if isinstance(text, unicodeT):
text = text.encode('utf8')
text = str(text or '')
text = to_native(text)
if not (isinstance(text, str)):
text = str(text or '')
text = regex_backslash.sub(lambda m: m.group(1).translate(ttab_in), text)
text = text.replace('\x05', '').replace('\r\n', '\n') # concatenate strings separeted by \\n
if URL is not None:
text = replace_at_urls(text, URL)
+27 -7
View File
@@ -84,7 +84,6 @@ Tornado code inspired by http://thomas.pelletier.im/2010/08/websocket-tornado-re
"""
from __future__ import print_function
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
@@ -92,17 +91,38 @@ import tornado.web
import hmac
import sys
import optparse
import urllib
import time
import sys
if (sys.version_info[0] == 2):
from urllib import urlencode, urlopen
def to_bytes(obj, charset='utf-8', errors='strict'):
if obj is None:
return None
if isinstance(obj, (bytes, bytearray, buffer)):
return bytes(obj)
if isinstance(obj, unicode):
return obj.encode(charset, errors)
raise TypeError('Expected bytes')
else:
from urllib.request import urlopen
from urllib.parse import urlencode
def to_bytes(obj, charset='utf-8', errors='strict'):
if obj is None:
return None
if isinstance(obj, (bytes, bytearray, memoryview)):
return bytes(obj)
if isinstance(obj, str):
return obj.encode(charset, errors)
raise TypeError('Expected bytes')
listeners, names, tokens = {}, {}, {}
def websocket_send(url, message, hmac_key=None, group='default'):
sig = hmac_key and hmac.new(hmac_key, message).hexdigest() or ''
params = urllib.urlencode(
sig = hmac_key and hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest() or ''
params = urlencode(
{'message': message, 'signature': sig, 'group': group})
f = urllib.urlopen(url, params)
f = urlopen(url, to_bytes(params))
data = f.read()
f.close()
return data
@@ -121,7 +141,7 @@ class PostHandler(tornado.web.RequestHandler):
print('%s:MESSAGE to %s:%s' % (time.time(), group, message))
if hmac_key:
signature = self.request.arguments['signature'][0]
if not hmac.new(hmac_key, message).hexdigest() == signature:
if not to_bytes(hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest()) == signature:
self.send_error(401)
for client in listeners.get(group, []):
client.write_message(message)
@@ -140,7 +160,7 @@ class TokenHandler(tornado.web.RequestHandler):
message = self.request.arguments['message'][0]
if hmac_key:
signature = self.request.arguments['signature'][0]
if not hmac.new(hmac_key, message).hexdigest() == signature:
if not to_bytes(hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest()) == signature:
self.send_error(401)
tokens[message] = None