Safely encode yaql expressions to support unicode expressions
Before unicode in yaql would cause a UnicodeEncodeError. Now YaqlExpression should behave better with unicode input. Change-Id: I3bbb018d7bd78cbf66c4190ef4c0b7d7e3baa03c Closes-Bug: #1361169
This commit is contained in:
committed by
Serg Melikyan
parent
159c6c29d7
commit
c8ca2ef72e
@@ -15,6 +15,8 @@
|
||||
import re
|
||||
import types
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
|
||||
import yaql
|
||||
import yaql.exceptions
|
||||
import yaql.expressions
|
||||
@@ -23,7 +25,7 @@ import yaql.expressions
|
||||
class YaqlExpression(object):
|
||||
def __init__(self, expression):
|
||||
if isinstance(expression, types.StringTypes):
|
||||
self._expression = str(expression)
|
||||
self._expression = encodeutils.safe_encode(expression)
|
||||
self._parsed_expression = yaql.parse(self._expression)
|
||||
self._file_position = None
|
||||
elif isinstance(expression, YaqlExpression):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# coding: utf-8
|
||||
# Copyright (c) 2014 Mirantis Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -164,6 +165,19 @@ class TestYaqlExpression(base.MuranoTestCase):
|
||||
|
||||
self.assertEqual('string', yaql_expr.expression)
|
||||
|
||||
def test_unicode_expression(self):
|
||||
yaql_expr = yaql_expression.YaqlExpression(u"'yaql ♥ unicode'")
|
||||
|
||||
self.assertEqual(u"'yaql ♥ unicode'".encode('utf-8'),
|
||||
yaql_expr.expression)
|
||||
|
||||
def test_unicode_expression_expression(self):
|
||||
yaql_expr = yaql_expression.YaqlExpression(u"'yaql ♥ unicode'")
|
||||
yaql_expr2 = yaql_expression.YaqlExpression(yaql_expr)
|
||||
|
||||
self.assertEqual(u"'yaql ♥ unicode'".encode('utf-8'),
|
||||
yaql_expr2.expression)
|
||||
|
||||
def test_evaluate_calls(self):
|
||||
string = 'string'
|
||||
expected_calls = [mock.call(string),
|
||||
|
||||
Reference in New Issue
Block a user