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

Conversation

trimbo
Copy link
Contributor

@trimbo trimbo commented Aug 26, 2019

We need to use portions of the runtime as part of the migration. Splitting out into a separate changelist should help expedite the work.

We need to use portions of the runtime as part of the migration.
Splitting out into a separate changelist shouold expedite the work.
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

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):

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):
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])

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

@@ -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

@@ -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):

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
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)

@@ -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):

if isinstance(value, (str, unicode, int, long, float,
runtime.UndefinedPlaceholder)):
if isinstance(value, (str, six.text_type, float,
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):

if isinstance(value, (str, unicode, int, long, float,
runtime.UndefinedPlaceholder)):
if isinstance(value, (str, unicode, float,
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):

Copy link
Contributor

@nicksay nicksay left a comment

Choose a reason for hiding this comment

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

Suggestions to avoid needing six.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants