Skip to content

Control unicode encoding behaviour based on execution environment. #122

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

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions awslambdaric/lambda_runtime_feature_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
"""
import os
from .lambda_runtime_features import feature_list


class FeatureGate:
def __init__(self, execution_env=os.environ.get("AWS_EXECUTION_ENV")):
self.__execution_env = execution_env

def is_feature_enabled(self, feature_name) -> bool:
if self.__execution_env is None or self.__execution_env in feature_list.get(
feature_name
):
return True
else:
return False


feature_handler = FeatureGate()
6 changes: 6 additions & 0 deletions awslambdaric/lambda_runtime_features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
"""
feature_list = {
"lambda_marshaller_ensure_ascii_false": {"AWS_Lambda_python3.12"},
}
6 changes: 5 additions & 1 deletion awslambdaric/lambda_runtime_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import simplejson as json

from .lambda_runtime_exception import FaultException
from .lambda_runtime_feature_handler import feature_handler


# simplejson's Decimal encoding allows '-NaN' as an output, which is a parse error for json.loads
# to get the good parts of Decimal support, we'll special-case NaN decimals and otherwise duplicate the encoding for decimals the same way simplejson does
# We also set 'ensure_ascii=False' so that the encoded json contains unicode characters instead of unicode escape sequences
class Encoder(json.JSONEncoder):
def __init__(self):
super().__init__(use_decimal=False, ensure_ascii=False)
if feature_handler.is_feature_enabled("lambda_marshaller_ensure_ascii_false"):
super().__init__(use_decimal=False, ensure_ascii=False)
else:
super().__init__(use_decimal=False)

def default(self, obj):
if isinstance(obj, decimal.Decimal):
Expand Down
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bandit>=1.6.2
# Test requirements
pytest>=3.0.7
mock>=2.0.0
parameterized>=0.9.0
51 changes: 51 additions & 0 deletions tests/test_lambda_runtime_features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import unittest
from parameterized import parameterized
from awslambdaric.lambda_runtime_features import feature_list
from awslambdaric.lambda_runtime_feature_handler import FeatureGate


feature_names = [(feature_name,) for feature_name in feature_list]
execution_envs = (
"AWS_Lambda_python3.12",
"AWS_Lambda_python3.11",
"AWS_Lambda_python3.10",
"AWS_Lambda_python3.9",
)
execution_envs_not_enabled_lambda_marshaller_ensure_ascii_false = tuple(
set(execution_envs).difference(
feature_list.get("lambda_marshaller_ensure_ascii_false")
)
)
execution_envs_is_enabled_lambda_marshaller_ensure_ascii_false = tuple(
feature_list.get("lambda_marshaller_ensure_ascii_false")
)


class TestLambdaRuntimeFeatures(unittest.TestCase):
@parameterized.expand(feature_names)
def test_no_execution_env_all_features_enabled(self, feature_name):
feature_handler = FeatureGate()
is_feature_enabled = feature_handler.is_feature_enabled(feature_name)
self.assertEqual(is_feature_enabled, True)

@parameterized.expand(
execution_envs_not_enabled_lambda_marshaller_ensure_ascii_false
)
def test_not_enabled_lambda_marshaller_ensure_ascii_false(self, execution_env):
feature_handler = FeatureGate(execution_env=execution_env)
is_feature_enabled = feature_handler.is_feature_enabled(
"lambda_marshaller_ensure_ascii_false"
)

self.assertEqual(is_feature_enabled, False)

@parameterized.expand(
execution_envs_is_enabled_lambda_marshaller_ensure_ascii_false
)
def test_is_enabled_lambda_marshaller_ensure_ascii_false(self, execution_env):
feature_handler = FeatureGate(execution_env=execution_env)
is_feature_enabled = feature_handler.is_feature_enabled(
"lambda_marshaller_ensure_ascii_false"
)
self.assertEqual(is_feature_enabled, True)
4 changes: 3 additions & 1 deletion tests/test_lambda_runtime_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ def test_to_json_unicode_encoding(self):
response = to_json({"price": "£1.00"})
self.assertEqual('{"price": "£1.00"}', response)
self.assertNotEqual('{"price": "\\u00a31.00"}', response)
self.assertEqual(19, len(response.encode('utf-8'))) # would be 23 bytes if a unicode escape was returned
self.assertEqual(
19, len(response.encode("utf-8"))
) # would be 23 bytes if a unicode escape was returned