From 0cd10a86f8d42a8e32da271ac13a399e47c312e3 Mon Sep 17 00:00:00 2001 From: Mohammad Nweider Date: Tue, 7 Sep 2021 13:36:41 +0000 Subject: [PATCH] Using the raw fd directly rather than opening the fd pseudo file --- awslambdaric/bootstrap.py | 8 ++++---- tests/test_bootstrap.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index be9271f..b2950e1 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -322,12 +322,12 @@ class FramedTelemetryLogSink(object): big-endian. """ - def __init__(self, filename): - self.filename = filename + def __init__(self, fd): + self.fd = int(fd) self.frame_type = 0xA55A0001 .to_bytes(4, "big") def __enter__(self): - self.file = open(self.filename, "wb", 0) + self.file = os.fdopen(self.fd, 'wb', 0) return self def __exit__(self, exc_type, exc_value, exc_tb): @@ -355,7 +355,7 @@ def create_log_sink(): if "_LAMBDA_TELEMETRY_LOG_FD" in os.environ: fd = os.environ["_LAMBDA_TELEMETRY_LOG_FD"] del os.environ["_LAMBDA_TELEMETRY_LOG_FD"] - return FramedTelemetryLogSink("/proc/self/fd/" + fd) + return FramedTelemetryLogSink(fd) else: return StandardLogSink() diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index b6f4b23..83b86ba 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -914,7 +914,7 @@ def test_log_error_standard_log_sink(self, mock_stdout): def test_log_error_framed_log_sink(self): with NamedTemporaryFile() as temp_file: - with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink: + with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink: err_to_log = bootstrap.make_error("Error message", "ErrorType", None) bootstrap.log_error(err_to_log, log_sink) @@ -949,7 +949,7 @@ def test_log_error_indentation_standard_log_sink(self, mock_stdout): def test_log_error_indentation_framed_log_sink(self): with NamedTemporaryFile() as temp_file: - with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink: + with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink: err_to_log = bootstrap.make_error( "Error message", "ErrorType", [" line1 ", " line2 ", " "] ) @@ -984,7 +984,7 @@ def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout): def test_log_error_empty_stacktrace_line_framed_log_sink(self): with NamedTemporaryFile() as temp_file: - with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink: + with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink: err_to_log = bootstrap.make_error( "Error message", "ErrorType", ["line1", "", "line2"] ) @@ -1070,19 +1070,19 @@ def test_create_unbuffered_log_sinks(self, mock_stdout): self.assertEqual(mock_stdout.getvalue(), "log") def test_create_framed_telemetry_log_sinks(self): - fd = "test_fd" - os.environ["_LAMBDA_TELEMETRY_LOG_FD"] = fd + fd = 3 + os.environ["_LAMBDA_TELEMETRY_LOG_FD"] = "3" actual = bootstrap.create_log_sink() self.assertIsInstance(actual, bootstrap.FramedTelemetryLogSink) - self.assertEqual(actual.filename, "/proc/self/fd/" + fd) + self.assertEqual(actual.fd, fd) self.assertFalse("_LAMBDA_TELEMETRY_LOG_FD" in os.environ) def test_single_frame(self): with NamedTemporaryFile() as temp_file: message = "hello world\nsomething on a new line!\n" - with bootstrap.FramedTelemetryLogSink(temp_file.name) as ls: + with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls: ls.log(message) with open(temp_file.name, "rb") as f: content = f.read() @@ -1101,7 +1101,7 @@ def test_multiple_frame(self): first_message = "hello world\nsomething on a new line!" second_message = "hello again\nhere's another message\n" - with bootstrap.FramedTelemetryLogSink(temp_file.name) as ls: + with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls: ls.log(first_message) ls.log(second_message)