From 1f013d76f3c1f1a81e56892ac66a49f639ed0d95 Mon Sep 17 00:00:00 2001 From: ilvalle Date: Wed, 8 Jun 2016 19:30:13 +0200 Subject: [PATCH] minor few fix --- gluon/_compat.py | 14 ++++++++++---- gluon/contrib/login_methods/ldap_auth.py | 2 +- gluon/contrib/shell.py | 14 +++++--------- gluon/globals.py | 1 + gluon/highlight.py | 13 ++++++------- gluon/html.py | 3 ++- gluon/http.py | 4 ++-- gluon/languages.py | 4 ++-- gluon/restricted.py | 4 ++-- gluon/shell.py | 14 +++++++------- gluon/utf8.py | 6 +++--- gluon/validators.py | 2 +- gluon/widget.py | 8 ++++---- 13 files changed, 46 insertions(+), 43 deletions(-) diff --git a/gluon/_compat.py b/gluon/_compat.py index 0d1fef16..d95b0e82 100644 --- a/gluon/_compat.py +++ b/gluon/_compat.py @@ -22,9 +22,10 @@ if PY2: from email.MIMEBase import MIMEBase from email.Header import Header from email import MIMEMultipart, MIMEText, Encoders, Charset - from urllib import FancyURLopener, urlencode + from urllib import FancyURLopener, urlencode, urlopen from urllib import quote as urllib_quote, unquote as urllib_unquote from string import maketrans + from types import ClassType import cgi reduce = reduce hashlib_md5 = hashlib.md5 @@ -64,8 +65,9 @@ if PY2: return obj return obj.encode(charset, errors) - def _local_html_escape(data, quote): - return cgi.escape(data, quote).replace("'", "'") + def _local_html_escape(data, quote=False): + s = cgi.escape(data, quote) + return s.replace("'", "'") if quote else s else: import pickle @@ -87,7 +89,7 @@ else: from email import encoders as Encoders from email.header import Header from email.charset import Charset - from urllib.request import FancyURLopener + from urllib.request import FancyURLopener, urlopen from urllib.parse import quote as urllib_quote, unquote as urllib_unquote, urlencode import html hashlib_md5 = lambda s: hashlib.md5(bytes(s, 'utf8')) @@ -103,6 +105,7 @@ else: unichr = chr unicodeT = str maketrans = str.maketrans + ClassType = type implements_iterator = _identity implements_bool = _identity @@ -129,6 +132,9 @@ else: characters, both double quote (") and single quote (') characters are also translated. """ + if isinstance(s, str): + return html.escape(s, quote=quote) + s = s.replace(b"&", b"&") # Must be done first! s = s.replace(b"<", b"<") s = s.replace(b">", b">") diff --git a/gluon/contrib/login_methods/ldap_auth.py b/gluon/contrib/login_methods/ldap_auth.py index fa151115..e19010f3 100644 --- a/gluon/contrib/login_methods/ldap_auth.py +++ b/gluon/contrib/login_methods/ldap_auth.py @@ -477,7 +477,7 @@ def ldap_auth(server='ldap', ldap_groups_of_the_user = get_user_groups_from_ldap(username, password) # search for allowed group names - if not isinstance(allowed_groups, type(list())): + if not isinstance(allowed_groups, list): allowed_groups = [allowed_groups] for group in allowed_groups: if ldap_groups_of_the_user.count(group) > 0: diff --git a/gluon/contrib/shell.py b/gluon/contrib/shell.py index b1c83942..341b32a2 100755 --- a/gluon/contrib/shell.py +++ b/gluon/contrib/shell.py @@ -29,17 +29,12 @@ An interactive, stateful AJAX shell that runs Python code on the server. """ from __future__ import print_function - +from gluon._compat import ClassType, pickle, StringIO import logging import new -try: - import cPickle as pickle -except: - import pickle import sys import traceback import types -import StringIO import threading locker = threading.RLock() @@ -53,6 +48,7 @@ _HISTORY_KIND = '_Shell_History' UNPICKLABLE_TYPES = [ types.ModuleType, type, + ClassType types.FunctionType, ] @@ -204,7 +200,7 @@ def run(history, statement, env={}): # globals, run the statement, and re-pickle the history globals, all # inside it. old_main = sys.modules.get('__main__') - output = StringIO.StringIO() + output = StringIO() try: sys.modules['__main__'] = statement_module statement_module.__name__ = '__main__' @@ -212,7 +208,7 @@ def run(history, statement, env={}): # re-evaluate the unpicklables for code in history.unpicklables: - exec code in statement_module.__dict__ + exec(code, statement_module.__dict__) # re-initialize the globals for name, val in history.globals_dict().items(): @@ -232,7 +228,7 @@ def run(history, statement, env={}): try: sys.stderr = sys.stdout = output locker.acquire() - exec compiled in statement_module.__dict__ + exec(compiled, statement_module.__dict__) finally: locker.release() sys.stdout, sys.stderr = old_stdout, old_stderr diff --git a/gluon/globals.py b/gluon/globals.py index c3ae7b9e..ebf0fdd9 100644 --- a/gluon/globals.py +++ b/gluon/globals.py @@ -48,6 +48,7 @@ PAST = 'Sat, 1-Jan-1971 00:00:00' FUTURE = 'Tue, 1-Dec-2999 23:59:59' try: + #FIXME PY3 from gluon.contrib.minify import minify have_minify = True except ImportError: diff --git a/gluon/highlight.py b/gluon/highlight.py index 092cf022..56afc2ea 100644 --- a/gluon/highlight.py +++ b/gluon/highlight.py @@ -7,9 +7,8 @@ | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) """ from __future__ import print_function -from gluon._compat import xrange +from gluon._compat import xrange, _local_html_escape import re -import cgi __all__ = ['highlight'] @@ -63,7 +62,7 @@ class Highlighter(object): Callback for C specific highlighting. """ - value = cgi.escape(match.group()) + value = _local_html_escape(match.group(), quote=False) self.change_style(token, style) self.output.append(value) @@ -77,7 +76,7 @@ class Highlighter(object): Callback for python specific highlighting. """ - value = cgi.escape(match.group()) + value = _local_html_escape(match.group(), quote=False) if token == 'MULTILINESTRING': self.change_style(token, style) self.output.append(value) @@ -114,7 +113,7 @@ class Highlighter(object): Callback for HTML specific highlighting. """ - value = cgi.escape(match.group()) + value = _local_html_escape(match.group(), quote=False) self.change_style(token, style) self.output.append(value) if token == 'GOTOPYTHON': @@ -292,13 +291,13 @@ def highlight( 'WEB2PY']: code = Highlighter(language, link, styles).highlight(code) else: - code = cgi.escape(code) + code = _local_html_escape(code, quote=False) lines = code.split('\n') if counter is None: linenumbers = [''] * len(lines) elif isinstance(counter, str): - linenumbers = [cgi.escape(counter)] * len(lines) + linenumbers = [_local_html_escape(counter, quote=False)] * len(lines) else: linenumbers = [str(i + counter) + '.' for i in xrange(len(lines))] diff --git a/gluon/html.py b/gluon/html.py index e4a4c544..642f097c 100644 --- a/gluon/html.py +++ b/gluon/html.py @@ -2210,7 +2210,8 @@ class FORM(DIV): next = where to redirect in case of success any other kwargs will be passed for form.accepts(...) """ - from gluon import current, redirect + from gluon.globals import current + from gluon.http import redirect kwargs['request_vars'] = kwargs.get( 'request_vars', current.request.post_vars) kwargs['session'] = kwargs.get('session', current.session) diff --git a/gluon/http.py b/gluon/http.py index c251cbdd..a0273866 100644 --- a/gluon/http.py +++ b/gluon/http.py @@ -163,7 +163,7 @@ def redirect(location='', how=303, client_side=False, headers=None): """ headers = headers or {} if location: - from gluon import current + from gluon.globals import current loc = location.replace('\r', '%0D').replace('\n', '%0A') if client_side and current.request.ajax: headers['web2py-redirect-location'] = loc @@ -174,7 +174,7 @@ def redirect(location='', how=303, client_side=False, headers=None): 'You are being redirected here' % loc, **headers) else: - from gluon import current + from gluon.globals import current if client_side and current.request.ajax: headers['web2py-component-command'] = 'window.location.reload(true)' raise HTTP(200, **headers) diff --git a/gluon/languages.py b/gluon/languages.py index 7c536399..763479ef 100644 --- a/gluon/languages.py +++ b/gluon/languages.py @@ -541,7 +541,7 @@ class translator(object): def get_possible_languages(self): """ Gets list of all possible languages for current application """ return list(set(self.current_languages + - [lang for lang in iterkeys(read_possible_languages(self.langpath)) + [lang for lang in read_possible_languages(self.langpath) if lang != 'default'])) def set_current_languages(self, *languages): @@ -658,7 +658,7 @@ class translator(object): languages = [] self.requested_languages = languages = tuple(languages) if languages: - all_languages = set(lang for lang in iterkeys(pl_info) + all_languages = set(lang for lang in pl_info if lang != 'default') \ | set(self.current_languages) for lang in languages: diff --git a/gluon/restricted.py b/gluon/restricted.py index 75bf8c7a..278b3a47 100644 --- a/gluon/restricted.py +++ b/gluon/restricted.py @@ -10,7 +10,7 @@ Restricted environment to execute application's code """ import sys -from gluon._compat import pickle +from gluon._compat import pickle, ClassType import traceback import types import os @@ -249,7 +249,7 @@ def snapshot(info=None, context=5, code=None, environment=None): # if no exception info given, get current: etype, evalue, etb = info or sys.exc_info() - if isinstance(etype, type): + if isinstance(etype, ClassType): etype = etype.__name__ # create a snapshot dict with some basic information diff --git a/gluon/shell.py b/gluon/shell.py index f39660ff..95359c3a 100644 --- a/gluon/shell.py +++ b/gluon/shell.py @@ -30,7 +30,7 @@ from gluon.globals import Request, Response, Session from gluon.storage import Storage, List from gluon.admin import w2p_unpack from pydal.base import BaseAdapter -from gluon._compat import iteritems +from gluon._compat import iteritems, ClassType logger = logging.getLogger("web2py") @@ -87,7 +87,7 @@ def exec_environment( if pyfile: pycfile = pyfile + 'c' if os.path.isfile(pycfile): - exec (read_pyc(pycfile)) in env + exec (read_pyc(pycfile), env) else: execfile(pyfile, env) return Storage(env) @@ -244,14 +244,14 @@ def run( "controllers_%s_%s.pyc" % (c, f)) if ((cronjob and os.path.isfile(pycfile)) or not os.path.isfile(pyfile)): - exec (read_pyc(pycfile)) in _env + exec(read_pyc(pycfile), _env) elif os.path.isfile(pyfile): execfile(pyfile, _env) else: die(errmsg) if f: - exec ('print %s()' % f, _env) + exec('print %s()' % f, _env) return _env.update(exec_pythonrc()) @@ -260,7 +260,7 @@ def run( ccode = None if startfile.endswith('.pyc'): ccode = read_pyc(startfile) - exec (ccode) in _env + exec(ccode, _env) else: execfile(startfile, _env) @@ -398,7 +398,7 @@ def test(testpath, import_models=True, verbose=False): def doctest_object(name, obj): """doctest obj and enclosed methods and classes.""" - if type(obj) in (types.FunctionType, type, types.MethodType, + if type(obj) in (types.FunctionType, type, ClassType, types.MethodType, types.UnboundMethodType): # Reload environment before each test. @@ -409,7 +409,7 @@ def test(testpath, import_models=True, verbose=False): obj, globs=globs, name='%s: %s' % (os.path.basename(testfile), name), verbose=verbose) - if type(obj) in (type): + if type(obj) in (type, ClassType): for attr_name in dir(obj): # Execute . operator so decorators are executed. diff --git a/gluon/utf8.py b/gluon/utf8.py index 315885b1..6fbab2dd 100644 --- a/gluon/utf8.py +++ b/gluon/utf8.py @@ -11,14 +11,14 @@ Utilities and class for UTF8 strings managing ---------------------------------------------- """ from __future__ import print_function -from gluon._compat import builtin as __builtin__, unicodeT, iteritems +from gluon._compat import builtin as __builtin__, unicodeT, iteritems, to_unicode __all__ = ['Utf8'] repr_escape_tab = {} #FIXME PY3 -#for i in range(1, 32): -# repr_escape_tab[i] = ur'\x%02x' % i +for i in range(1, 32): + repr_escape_tab[i] = to_unicode("\\"+"x%02x" % i) repr_escape_tab[7] = u'\\a' repr_escape_tab[8] = u'\\b' repr_escape_tab[9] = u'\\t' diff --git a/gluon/validators.py b/gluon/validators.py index a2e8218f..4181c226 100644 --- a/gluon/validators.py +++ b/gluon/validators.py @@ -71,7 +71,7 @@ __all__ = [ ] try: - from globals import current + from gluon.globals import current have_current = True except ImportError: have_current = False diff --git a/gluon/widget.py b/gluon/widget.py index ceccf068..b641bb50 100644 --- a/gluon/widget.py +++ b/gluon/widget.py @@ -355,7 +355,7 @@ class web2pyDialog(object): except: sys.stderr.write('Sorry, -K only supported for python 2.6-2.7\n') return - code = "from gluon import current;current._scheduler.loop()" + code = "from gluon.globals import current;current._scheduler.loop()" print('starting scheduler from widget for "%s"...' % app) args = (app, True, True, None, False, code) logging.getLogger().setLevel(self.options.debuglevel) @@ -1034,9 +1034,9 @@ def check_existent_app(options, appname): def get_code_for_scheduler(app, options): if len(app) == 1 or app[1] is None: - code = "from gluon import current;current._scheduler.loop()" + code = "from gluon.globals import current;current._scheduler.loop()" else: - code = "from gluon import current;current._scheduler.group_names = ['%s'];" + code = "from gluon.globals import current;current._scheduler.group_names = ['%s'];" code += "current._scheduler.loop()" code = code % ("','".join(app[1:])) app_ = app[0] @@ -1056,7 +1056,7 @@ def start_schedulers(options): apps = [(app.strip(), None) for app in options.scheduler.split(',')] if options.scheduler_groups: apps = options.scheduler_groups - code = "from gluon import current;current._scheduler.loop()" + code = "from gluon.globals import current;current._scheduler.loop()" logging.getLogger().setLevel(options.debuglevel) if options.folder: os.chdir(options.folder)