Skip to content

Commit 67ff94e

Browse files
Merge branch 'main' into main
2 parents 1b440a6 + 56eb1ca commit 67ff94e

38 files changed

+265
-157
lines changed

Diff for: .dockerignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
**/node_modules/
55
**/dist/
66

7-
test/integration/*
8-
!test/integration/resources/
9-
!test/integration/test-handlers/
7+
tests/integration/*
8+
!tests/integration/resources/
9+
!tests/integration/test-handlers/

Diff for: .gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
generated.docker-compose.*.yml
22

3-
test/integration/resources/init
3+
tests/integration/resources/init
44

55
node_modules/
66
*.tsbuildinfo
@@ -143,3 +143,6 @@ dmypy.json
143143

144144
# Cython debug symbols
145145
cython_debug/
146+
147+
# Test files generated
148+
tmp*.py

Diff for: Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ init:
99

1010
.PHONY: test
1111
test: check-format
12-
pytest --cov awslambdaric --cov-report term-missing --cov-fail-under 90 test
12+
pytest --cov awslambdaric --cov-report term-missing --cov-fail-under 90 tests
1313

1414
.PHONY: setup-codebuild-agent
1515
setup-codebuild-agent:
16-
docker build -t codebuild-agent - < test/integration/codebuild-local/Dockerfile.agent
16+
docker build -t codebuild-agent - < tests/integration/codebuild-local/Dockerfile.agent
1717

1818
.PHONY: test-smoke
1919
test-smoke: setup-codebuild-agent
20-
CODEBUILD_IMAGE_TAG=codebuild-agent test/integration/codebuild-local/test_one.sh test/integration/codebuild/buildspec.os.alpine.1.yml alpine 3.12 3.8
20+
CODEBUILD_IMAGE_TAG=codebuild-agent tests/integration/codebuild-local/test_one.sh tests/integration/codebuild/buildspec.os.alpine.1.yml alpine 3.12 3.8
2121

2222
.PHONY: test-integ
2323
test-integ: setup-codebuild-agent
24-
CODEBUILD_IMAGE_TAG=codebuild-agent test/integration/codebuild-local/test_all.sh test/integration/codebuild/.
24+
CODEBUILD_IMAGE_TAG=codebuild-agent tests/integration/codebuild-local/test_all.sh tests/integration/codebuild/.
2525

2626
.PHONY: check-security
2727
check-security:
2828
bandit -r awslambdaric
2929

3030
.PHONY: format
3131
format:
32-
black setup.py awslambdaric/ test/
32+
black setup.py awslambdaric/ tests/
3333

3434
.PHONY: check-format
3535
check-format:
36-
black --check setup.py awslambdaric/ test/
36+
black --check setup.py awslambdaric/ tests/
3737

3838
# Command to run everytime you make changes to verify everything works
3939
.PHONY: dev

Diff for: awslambdaric/__main__.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def main(args):
1313
handler = args[1]
1414
lambda_runtime_api_addr = os.environ["AWS_LAMBDA_RUNTIME_API"]
1515

16-
print(f"Executing '{handler}' in function directory '{app_root}'")
1716
bootstrap.run(app_root, handler, lambda_runtime_api_addr)
1817

1918

Diff for: awslambdaric/bootstrap.py

+18-35
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
"""
44

5+
import importlib
56
import json
67
import logging
78
import os
89
import sys
910
import time
1011
import traceback
11-
import warnings
1212

1313
from .lambda_context import LambdaContext
1414
from .lambda_runtime_client import LambdaRuntimeClient
1515
from .lambda_runtime_exception import FaultException
1616
from .lambda_runtime_marshaller import to_json
1717

18-
with warnings.catch_warnings():
19-
warnings.filterwarnings("ignore", category=DeprecationWarning)
20-
import imp
21-
2218
ERROR_LOG_LINE_TERMINATE = "\r"
2319
ERROR_LOG_IDENT = "\u00a0" # NO-BREAK SPACE U+00A0
2420

@@ -33,23 +29,14 @@ def _get_handler(handler):
3329
)
3430
return make_fault_handler(fault)
3531

36-
file_handle, pathname, desc = None, None, None
3732
try:
38-
# Recursively loading handler in nested directories
39-
for segment in modname.split("."):
40-
if pathname is not None:
41-
pathname = [pathname]
42-
file_handle, pathname, desc = imp.find_module(segment, pathname)
43-
if file_handle is None:
44-
module_type = desc[2]
45-
if module_type == imp.C_BUILTIN:
46-
fault = FaultException(
47-
FaultException.BUILT_IN_MODULE_CONFLICT,
48-
"Cannot use built-in module {} as a handler module".format(modname),
49-
)
50-
request_handler = make_fault_handler(fault)
51-
return request_handler
52-
m = imp.load_module(modname, file_handle, pathname, desc)
33+
if modname.split(".")[0] in sys.builtin_module_names:
34+
fault = FaultException(
35+
FaultException.BUILT_IN_MODULE_CONFLICT,
36+
"Cannot use built-in module {} as a handler module".format(modname),
37+
)
38+
return make_fault_handler(fault)
39+
m = importlib.import_module(modname.replace("/", "."))
5340
except ImportError as e:
5441
fault = FaultException(
5542
FaultException.IMPORT_MODULE_ERROR,
@@ -66,9 +53,6 @@ def _get_handler(handler):
6653
)
6754
request_handler = make_fault_handler(fault)
6855
return request_handler
69-
finally:
70-
if file_handle is not None:
71-
file_handle.close()
7256

7357
try:
7458
request_handler = getattr(m, fname)
@@ -89,14 +73,13 @@ def result(*args):
8973
return result
9074

9175

92-
def make_error(error_message, error_type, stack_trace):
93-
result = {}
94-
if error_message:
95-
result["errorMessage"] = error_message
96-
if error_type:
97-
result["errorType"] = error_type
98-
if stack_trace:
99-
result["stackTrace"] = stack_trace
76+
def make_error(error_message, error_type, stack_trace, invoke_id=None):
77+
result = {
78+
"errorMessage": error_message if error_message else "",
79+
"errorType": error_type if error_type else "",
80+
"requestId": invoke_id if invoke_id is not None else "",
81+
"stackTrace": stack_trace if stack_trace else [],
82+
}
10083
return result
10184

10285

@@ -169,7 +152,7 @@ def handle_event_request(
169152
)
170153
except FaultException as e:
171154
xray_fault = make_xray_fault("LambdaValidationError", e.msg, os.getcwd(), [])
172-
error_result = make_error(e.msg, e.exception_type, e.trace)
155+
error_result = make_error(e.msg, e.exception_type, e.trace, invoke_id)
173156

174157
except Exception:
175158
etype, value, tb = sys.exc_info()
@@ -181,7 +164,7 @@ def handle_event_request(
181164

182165
xray_fault = make_xray_fault(etype.__name__, str(value), os.getcwd(), tb_tuples)
183166
error_result = make_error(
184-
str(value), etype.__name__, traceback.format_list(tb_tuples)
167+
str(value), etype.__name__, traceback.format_list(tb_tuples), invoke_id
185168
)
186169

187170
if error_result is not None:
@@ -404,7 +387,7 @@ def run(app_root, handler, lambda_runtime_api_addr):
404387
global _GLOBAL_AWS_REQUEST_ID
405388

406389
request_handler = _get_handler(handler)
407-
except Exception as e:
390+
except Exception:
408391
error_result = build_fault_result(sys.exc_info(), None)
409392

410393
log_error(error_result, log_sink)

Diff for: awslambdaric/runtime_client.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@ static PyObject *method_initialize_client(PyObject *self, PyObject *args) {
2525
}
2626

2727
static PyObject *method_next(PyObject *self) {
28-
if (CLIENT == nullptr) {
29-
PyErr_SetString(PyExc_RuntimeError, "Client not yet initalized");
30-
return NULL;
31-
}
28+
aws::lambda_runtime::invocation_request response;
29+
30+
// Release GIL and save thread state
31+
// ref: https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock
32+
PyThreadState *_save;
33+
_save = PyEval_SaveThread();
3234

3335
auto outcome = CLIENT->get_next();
3436
if (!outcome.is_success()) {
37+
// Reacquire GIL before exiting
38+
PyEval_RestoreThread(_save);
3539
PyErr_SetString(PyExc_RuntimeError, "Failed to get next");
3640
return NULL;
3741
}
3842

39-
auto response = outcome.get_result();
43+
response = outcome.get_result();
44+
// Reacquire GIL before constructing return object
45+
PyEval_RestoreThread(_save);
46+
4047
auto payload = response.payload;
4148
auto request_id = response.request_id.c_str();
4249
auto trace_id = response.xray_trace_id.c_str();

Diff for: deps/curl-7.77.0.tar.gz

3.95 MB
Binary file not shown.

Diff for: requirements/dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ coverage>=4.4.0
22
flake8>=3.3.0
33
tox>=2.2.1
44
pytest-cov>=2.4.0
5-
pylint>=1.7.2,<2.0
5+
pylint>=1.7.2
66
black>=20.8b0
77
bandit>=1.6.2
88

Diff for: setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_curl_extra_linker_flags():
3232
def get_runtime_client_extension():
3333
if platform.system() != "Linux" and os.getenv("BUILD") != "true":
3434
print(
35-
"The native runtime_client only builds in Linux. Skiping its compilation."
35+
"The native runtime_client only builds on Linux. Skipping its compilation."
3636
)
3737
return []
3838

@@ -68,7 +68,7 @@ def read_requirements(req="base.txt"):
6868

6969
setup(
7070
name="awslambdaric",
71-
version="1.0.0",
71+
version="1.2.1",
7272
author="Amazon Web Services",
7373
description="AWS Lambda Runtime Interface Client for Python",
7474
long_description=read("README.md"),

Diff for: test/__init__.py renamed to tests/__init__.py

File renamed without changes.

Diff for: test/integration/codebuild/buildspec.os.alpine.1.yml renamed to tests/integration/codebuild/buildspec.os.alpine.1.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ phases:
2727
- echo "Extracting and including the Runtime Interface Emulator"
2828
- SCRATCH_DIR=".scratch"
2929
- mkdir "${SCRATCH_DIR}"
30-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
30+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3131
- >
32-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
32+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3333
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3434
- >
3535
echo "RUN apk add curl" >> \

Diff for: test/integration/codebuild/buildspec.os.alpine.2.yml renamed to tests/integration/codebuild/buildspec.os.alpine.2.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ phases:
2929
- echo "Extracting and including the Runtime Interface Emulator"
3030
- SCRATCH_DIR=".scratch"
3131
- mkdir "${SCRATCH_DIR}"
32-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
32+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3333
- >
34-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
34+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3535
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3636
- >
3737
echo "RUN apk add curl" >> \

Diff for: test/integration/codebuild/buildspec.os.alpine.3.yml renamed to tests/integration/codebuild/buildspec.os.alpine.3.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ phases:
2929
- echo "Extracting and including the Runtime Interface Emulator"
3030
- SCRATCH_DIR=".scratch"
3131
- mkdir "${SCRATCH_DIR}"
32-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
32+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3333
- >
34-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
34+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3535
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3636
- >
3737
echo "RUN apk add curl" >> \

Diff for: test/integration/codebuild/buildspec.os.amazonlinux.yml renamed to tests/integration/codebuild/buildspec.os.amazonlinux.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ phases:
3030
- echo "Extracting and including the Runtime Interface Emulator"
3131
- SCRATCH_DIR=".scratch"
3232
- mkdir "${SCRATCH_DIR}"
33-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
33+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3434
- >
35-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
35+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3636
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3737
- >
3838
echo "COPY ${SCRATCH_DIR}/aws-lambda-rie /usr/bin/aws-lambda-rie" >> \

Diff for: test/integration/codebuild/buildspec.os.centos.1.yml renamed to tests/integration/codebuild/buildspec.os.centos.1.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ phases:
2929
- echo "Extracting and including the Runtime Interface Emulator"
3030
- SCRATCH_DIR=".scratch"
3131
- mkdir "${SCRATCH_DIR}"
32-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
32+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3333
- >
34-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
34+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3535
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3636
- >
3737
echo "COPY ${SCRATCH_DIR}/aws-lambda-rie /usr/bin/aws-lambda-rie" >> \

Diff for: test/integration/codebuild/buildspec.os.centos.2.yml renamed to tests/integration/codebuild/buildspec.os.centos.2.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ phases:
2929
- echo "Extracting and including the Runtime Interface Emulator"
3030
- SCRATCH_DIR=".scratch"
3131
- mkdir "${SCRATCH_DIR}"
32-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
32+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3333
- >
34-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
34+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3535
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3636
- >
3737
echo "COPY ${SCRATCH_DIR}/aws-lambda-rie /usr/bin/aws-lambda-rie" >> \

Diff for: test/integration/codebuild/buildspec.os.debian.yml renamed to tests/integration/codebuild/buildspec.os.debian.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ phases:
2929
- echo "Extracting and including the Runtime Interface Emulator"
3030
- SCRATCH_DIR=".scratch"
3131
- mkdir "${SCRATCH_DIR}"
32-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
32+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3333
- >
34-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
34+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3535
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3636
- >
3737
echo "COPY ${SCRATCH_DIR}/aws-lambda-rie /usr/bin/aws-lambda-rie" >> \

Diff for: test/integration/codebuild/buildspec.os.ubuntu.1.yml renamed to tests/integration/codebuild/buildspec.os.ubuntu.1.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ phases:
2828
- echo "Extracting and including the Runtime Interface Emulator"
2929
- SCRATCH_DIR=".scratch"
3030
- mkdir "${SCRATCH_DIR}"
31-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
31+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3232
- >
33-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
33+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3434
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3535
- >
3636
echo "COPY ${SCRATCH_DIR}/aws-lambda-rie /usr/bin/aws-lambda-rie" >> \

Diff for: test/integration/codebuild/buildspec.os.ubuntu.2.yml renamed to tests/integration/codebuild/buildspec.os.ubuntu.2.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ phases:
2727
- echo "Extracting and including the Runtime Interface Emulator"
2828
- SCRATCH_DIR=".scratch"
2929
- mkdir "${SCRATCH_DIR}"
30-
- tar -xvf test/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
30+
- tar -xvf tests/integration/resources/aws-lambda-rie.tar.gz --directory "${SCRATCH_DIR}"
3131
- >
32-
cp "test/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
32+
cp "tests/integration/docker/Dockerfile.echo.${OS_DISTRIBUTION}" \
3333
"${SCRATCH_DIR}/Dockerfile.echo.${OS_DISTRIBUTION}.tmp"
3434
- >
3535
echo "COPY ${SCRATCH_DIR}/aws-lambda-rie /usr/bin/aws-lambda-rie" >> \

Diff for: test/integration/docker/Dockerfile.echo.alpine renamed to tests/integration/docker/Dockerfile.echo.alpine

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ARG FUNCTION_DIR="/home/app/"
3838
# Create function directory
3939
RUN mkdir -p ${FUNCTION_DIR}
4040
# Copy function code
41-
COPY test/integration/test-handlers/echo/* ${FUNCTION_DIR}
41+
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
4242
# Copy Runtime Interface Client .tgz
4343
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz
4444

Diff for: test/integration/docker/Dockerfile.echo.amazonlinux renamed to tests/integration/docker/Dockerfile.echo.amazonlinux

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ ARG FUNCTION_DIR="/home/app/"
8686
# Create function directory
8787
RUN mkdir -p ${FUNCTION_DIR}
8888
# Copy function code
89-
COPY test/integration/test-handlers/echo/* ${FUNCTION_DIR}
89+
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
9090
# Copy Runtime Interface Client .tgz
9191
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz
9292

Diff for: test/integration/docker/Dockerfile.echo.centos renamed to tests/integration/docker/Dockerfile.echo.centos

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ ARG FUNCTION_DIR="/home/app/"
8686
# Create function directory
8787
RUN mkdir -p ${FUNCTION_DIR}
8888
# Copy function code
89-
COPY test/integration/test-handlers/echo/* ${FUNCTION_DIR}
89+
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
9090
# Copy Runtime Interface Client .tgz
9191
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz
9292

Diff for: test/integration/docker/Dockerfile.echo.debian renamed to tests/integration/docker/Dockerfile.echo.debian

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ARG FUNCTION_DIR="/home/app/"
2727
# Create function directory
2828
RUN mkdir -p ${FUNCTION_DIR}
2929
# Copy function code
30-
COPY test/integration/test-handlers/echo/* ${FUNCTION_DIR}
30+
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
3131
# Copy Runtime Interface Client .tgz
3232
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz
3333

Diff for: test/integration/docker/Dockerfile.echo.ubuntu renamed to tests/integration/docker/Dockerfile.echo.ubuntu

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ARG FUNCTION_DIR="/home/app/"
5555
# Create function directory
5656
RUN mkdir -p ${FUNCTION_DIR}
5757
# Copy function code
58-
COPY test/integration/test-handlers/echo/* ${FUNCTION_DIR}
58+
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
5959
# Copy Runtime Interface Client .tgz
6060
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz
6161

0 commit comments

Comments
 (0)