Skip to content

Commit 80eefef

Browse files
authored
Control unicode encoding behaviour based on execution environment. (#122)
1 parent 49bb4cf commit 80eefef

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

Diff for: awslambdaric/lambda_runtime_marshaller.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import decimal
66
import math
7-
7+
import os
88
import simplejson as json
99

1010
from .lambda_runtime_exception import FaultException
@@ -15,7 +15,10 @@
1515
# We also set 'ensure_ascii=False' so that the encoded json contains unicode characters instead of unicode escape sequences
1616
class Encoder(json.JSONEncoder):
1717
def __init__(self):
18-
super().__init__(use_decimal=False, ensure_ascii=False)
18+
if os.environ.get("AWS_EXECUTION_ENV") == "AWS_Lambda_python3.12":
19+
super().__init__(use_decimal=False, ensure_ascii=False)
20+
else:
21+
super().__init__(use_decimal=False)
1922

2023
def default(self, obj):
2124
if isinstance(obj, decimal.Decimal):

Diff for: requirements/dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ bandit>=1.6.2
99
# Test requirements
1010
pytest>=3.0.7
1111
mock>=2.0.0
12+
parameterized>=0.9.0

Diff for: tests/test_lambda_runtime_marshaller.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,35 @@
33
"""
44

55
import decimal
6+
import os
67
import unittest
7-
8+
from parameterized import parameterized
89
from awslambdaric.lambda_runtime_marshaller import to_json
910

1011

1112
class TestLambdaRuntimeMarshaller(unittest.TestCase):
13+
execution_envs = (
14+
"AWS_Lambda_python3.12",
15+
"AWS_Lambda_python3.11",
16+
"AWS_Lambda_python3.10",
17+
"AWS_Lambda_python3.9",
18+
)
19+
20+
envs_lambda_marshaller_ensure_ascii_false = {"AWS_Lambda_python3.12"}
21+
22+
execution_envs_lambda_marshaller_ensure_ascii_true = tuple(
23+
set(execution_envs).difference(envs_lambda_marshaller_ensure_ascii_false)
24+
)
25+
execution_envs_lambda_marshaller_ensure_ascii_false = tuple(
26+
envs_lambda_marshaller_ensure_ascii_false
27+
)
28+
29+
def setUp(self):
30+
self.org_os_environ = os.environ
31+
32+
def tearDown(self):
33+
os.environ = self.org_os_environ
34+
1235
def test_to_json_decimal_encoding(self):
1336
response = to_json({"pi": decimal.Decimal("3.14159")})
1437
self.assertEqual('{"pi": 3.14159}', response)
@@ -38,10 +61,22 @@ def test_json_serializer_is_not_default_json(self):
3861
self.assertFalse(hasattr(stock_json, "YOLO"))
3962
self.assertTrue(hasattr(simplejson, "YOLO"))
4063

41-
def test_to_json_unicode_encoding(self):
64+
@parameterized.expand(execution_envs_lambda_marshaller_ensure_ascii_false)
65+
def test_to_json_unicode_not_escaped_encoding(self, execution_env):
66+
os.environ = {"AWS_EXECUTION_ENV": execution_env}
4267
response = to_json({"price": "£1.00"})
4368
self.assertEqual('{"price": "£1.00"}', response)
4469
self.assertNotEqual('{"price": "\\u00a31.00"}', response)
4570
self.assertEqual(
4671
19, len(response.encode("utf-8"))
4772
) # would be 23 bytes if a unicode escape was returned
73+
74+
@parameterized.expand(execution_envs_lambda_marshaller_ensure_ascii_true)
75+
def test_to_json_unicode_is_escaped_encoding(self, execution_env):
76+
os.environ = {"AWS_EXECUTION_ENV": execution_env}
77+
response = to_json({"price": "£1.00"})
78+
self.assertEqual('{"price": "\\u00a31.00"}', response)
79+
self.assertNotEqual('{"price": "£1.00"}', response)
80+
self.assertEqual(
81+
23, len(response.encode("utf-8"))
82+
) # would be 19 bytes if a escaped was returned

0 commit comments

Comments
 (0)