-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate some essential runtime files to Python 3 #80
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,10 @@ | |||||
# Use of this source code is governed by a BSD-style | ||||||
# license that can be found in the LICENSE file. | ||||||
|
||||||
from __future__ import absolute_import | ||||||
from __future__ import division | ||||||
from __future__ import print_function | ||||||
from third_party import six | ||||||
|
||||||
class __UnresolvedPlaceholder(object): | ||||||
pass | ||||||
|
@@ -34,7 +38,7 @@ def __init__(self, name, search_list): | |||||
self.__name = name | ||||||
self.__search_list = search_list | ||||||
|
||||||
def __nonzero__(self): | ||||||
def __bool__(self): | ||||||
return False | ||||||
|
||||||
def __str__(self): | ||||||
|
@@ -54,7 +58,7 @@ class UndefinedAttribute(UndefinedPlaceholder): | |||||
|
||||||
def _get_available_placeholders(scope): | ||||||
if isinstance(scope, dict): | ||||||
return scope.keys() | ||||||
return list(scope.keys()) | ||||||
else: | ||||||
return [a | ||||||
for a in dir(scope) | ||||||
|
@@ -68,15 +72,15 @@ def import_module_symbol(name): | |||||
module = __import__(module_name, globals(), locals(), [symbol_name]) | ||||||
try: | ||||||
symbol = getattr(module, symbol_name) | ||||||
except AttributeError, e: | ||||||
except AttributeError as e: | ||||||
raise ImportError("can't import %s" % name) | ||||||
return symbol | ||||||
|
||||||
|
||||||
# map template function names to python function names | ||||||
# inject them into a module so they run as globals | ||||||
def register_functions(module, template_function_map): | ||||||
for t_name, f_name in template_function_map.iteritems(): | ||||||
for t_name, f_name in six.iteritems(template_function_map): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the python2/python3 compatible
Suggested change
|
||||||
f_func = import_module_symbol(f_name) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
setattr(module, t_name, f_func) | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,8 +7,10 @@ | |||||||||||||||
|
||||||||||||||||
import functools | ||||||||||||||||
import types | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
from spitfire import runtime | ||||||||||||||||
from spitfire.runtime import udn | ||||||||||||||||
from third_party import six | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
# decorate a function object so the default filter will not be applied to the | ||||||||||||||||
|
@@ -37,7 +39,7 @@ def passthrough_filter(value): | |||||||||||||||
def escape_html(value, quote=True): | ||||||||||||||||
"""Replace special characters '&', '<' and '>' by SGML entities.""" | ||||||||||||||||
value = simple_str_filter(value) | ||||||||||||||||
if isinstance(value, basestring): | ||||||||||||||||
if isinstance(value, six.string_types): | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
value = value.replace("&", "&") # Must be done first! | ||||||||||||||||
value = value.replace("<", "<") | ||||||||||||||||
value = value.replace(">", ">") | ||||||||||||||||
|
@@ -49,17 +51,17 @@ def escape_html(value, quote=True): | |||||||||||||||
# deprecated | ||||||||||||||||
def safe_values(value): | ||||||||||||||||
"""Deprecated - use simple_str_filter instead.""" | ||||||||||||||||
if isinstance(value, (str, unicode, int, long, float, | ||||||||||||||||
runtime.UndefinedPlaceholder)): | ||||||||||||||||
if isinstance(value, (str, six.text_type, float, | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above
Suggested change
|
||||||||||||||||
runtime.UndefinedPlaceholder) + six.integer_types): | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
return value | ||||||||||||||||
else: | ||||||||||||||||
return '' | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
def simple_str_filter(value): | ||||||||||||||||
"""Return a string if the input type is something primitive.""" | ||||||||||||||||
if isinstance(value, (str, unicode, int, long, float, | ||||||||||||||||
runtime.UndefinedPlaceholder)): | ||||||||||||||||
if isinstance(value, (str, unicode, float, | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above
Suggested change
|
||||||||||||||||
runtime.UndefinedPlaceholder) + six.integer_types): | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
# fixme: why do force this conversion here? | ||||||||||||||||
# do we want to be unicode or str? | ||||||||||||||||
return str(value) | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,11 +9,11 @@ | |||||||||||||||
# syntactically, 'name' will always be a valid identifier - so you won't get | ||||||||||||||||
# name='my attribute' - it must be a legal python identifier | ||||||||||||||||
|
||||||||||||||||
import __builtin__ | ||||||||||||||||
import inspect | ||||||||||||||||
import logging | ||||||||||||||||
import weakref | ||||||||||||||||
|
||||||||||||||||
from third_party.six.moves import builtins | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
from spitfire import runtime | ||||||||||||||||
# Import the accelerated C module if available. | ||||||||||||||||
try: | ||||||||||||||||
|
@@ -180,7 +180,7 @@ def _resolve_placeholder(name, template, global_vars): | |||||||||||||||
# fixme: finally try to resolve builtins - this should be configurable | ||||||||||||||||
# if you compile optimized modes, this isn't necessary | ||||||||||||||||
try: | ||||||||||||||||
return getattr(__builtin__, name) | ||||||||||||||||
return getattr(builtins, name) | ||||||||||||||||
except AttributeError: | ||||||||||||||||
return UndefinedPlaceholder(name, search_list) | ||||||||||||||||
|
||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.