Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions spitfire/runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
from third_party import six


class __UnresolvedPlaceholder(object):
pass
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Copy link
Contributor

@nicksay nicksay Aug 28, 2019

Choose a reason for hiding this comment

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

We can use the python2/python3 compatible iter(<map_value>) approach here (combined with next suggestion).

Suggested change
for t_name, f_name in six.iteritems(template_function_map):
for t_name in iter(template_function_map):

f_func = import_module_symbol(f_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
f_func = import_module_symbol(f_name)
f_func = import_module_symbol(template_fucntion_map[tname])

setattr(module, t_name, f_func)

Expand Down
12 changes: 7 additions & 5 deletions spitfire/runtime/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import functools
import types
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import types
import sys
import types


from spitfire import runtime
from spitfire.runtime import udn
from third_party import six
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
from third_party import six



Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if sys.version_info[0] == 2
SAFE_VALUE_TYPES = (str, unicode, int, long, float,
runtime.UndefinedPlaceholder)
else:
SAFE_VALUE_TYPES = (str, int, float, runtime.UndefinedPlaceholder)

# decorate a function object so the default filter will not be applied to the
Expand Down Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

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

simple_str_filter always calls str()

Suggested change
if isinstance(value, six.string_types):
if isinstance(value, str):

value = value.replace("&", "&amp;") # Must be done first!
value = value.replace("<", "&lt;")
value = value.replace(">", "&gt;")
Expand All @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

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

See above

Suggested change
if isinstance(value, (str, six.text_type, float,
if isinstance(value, SAFE_VALUE_TYPES):

runtime.UndefinedPlaceholder) + six.integer_types):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
runtime.UndefinedPlaceholder) + six.integer_types):

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,
Copy link
Contributor

Choose a reason for hiding this comment

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

See above

Suggested change
if isinstance(value, (str, unicode, float,
if isinstance(value, SAFE_VALUE_TYPES):

runtime.UndefinedPlaceholder) + six.integer_types):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
runtime.UndefinedPlaceholder) + six.integer_types):

# fixme: why do force this conversion here?
# do we want to be unicode or str?
return str(value)
Expand Down
4 changes: 2 additions & 2 deletions spitfire/runtime/udn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

@nicksay nicksay Aug 28, 2019

Choose a reason for hiding this comment

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

Suggested change
from third_party.six.moves import builtins
# Python3 moved "__builtin__" to "builtins".
try:
import builtins
except ImportError:
import __builtin__ as builtins

from spitfire import runtime
# Import the accelerated C module if available.
try:
Expand Down Expand Up @@ -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)

Expand Down