Skip to content

Commit 0cd10a8

Browse files
author
Mohammad Nweider
committed
Using the raw fd directly rather than opening the fd pseudo file
1 parent c92c8ad commit 0cd10a8

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

awslambdaric/bootstrap.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,12 @@ class FramedTelemetryLogSink(object):
322322
big-endian.
323323
"""
324324

325-
def __init__(self, filename):
326-
self.filename = filename
325+
def __init__(self, fd):
326+
self.fd = int(fd)
327327
self.frame_type = 0xA55A0001 .to_bytes(4, "big")
328328

329329
def __enter__(self):
330-
self.file = open(self.filename, "wb", 0)
330+
self.file = os.fdopen(self.fd, 'wb', 0)
331331
return self
332332

333333
def __exit__(self, exc_type, exc_value, exc_tb):
@@ -355,7 +355,7 @@ def create_log_sink():
355355
if "_LAMBDA_TELEMETRY_LOG_FD" in os.environ:
356356
fd = os.environ["_LAMBDA_TELEMETRY_LOG_FD"]
357357
del os.environ["_LAMBDA_TELEMETRY_LOG_FD"]
358-
return FramedTelemetryLogSink("/proc/self/fd/" + fd)
358+
return FramedTelemetryLogSink(fd)
359359

360360
else:
361361
return StandardLogSink()

tests/test_bootstrap.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ def test_log_error_standard_log_sink(self, mock_stdout):
914914

915915
def test_log_error_framed_log_sink(self):
916916
with NamedTemporaryFile() as temp_file:
917-
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
917+
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink:
918918
err_to_log = bootstrap.make_error("Error message", "ErrorType", None)
919919
bootstrap.log_error(err_to_log, log_sink)
920920

@@ -949,7 +949,7 @@ def test_log_error_indentation_standard_log_sink(self, mock_stdout):
949949

950950
def test_log_error_indentation_framed_log_sink(self):
951951
with NamedTemporaryFile() as temp_file:
952-
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
952+
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink:
953953
err_to_log = bootstrap.make_error(
954954
"Error message", "ErrorType", [" line1 ", " line2 ", " "]
955955
)
@@ -984,7 +984,7 @@ def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout):
984984

985985
def test_log_error_empty_stacktrace_line_framed_log_sink(self):
986986
with NamedTemporaryFile() as temp_file:
987-
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
987+
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink:
988988
err_to_log = bootstrap.make_error(
989989
"Error message", "ErrorType", ["line1", "", "line2"]
990990
)
@@ -1070,19 +1070,19 @@ def test_create_unbuffered_log_sinks(self, mock_stdout):
10701070
self.assertEqual(mock_stdout.getvalue(), "log")
10711071

10721072
def test_create_framed_telemetry_log_sinks(self):
1073-
fd = "test_fd"
1074-
os.environ["_LAMBDA_TELEMETRY_LOG_FD"] = fd
1073+
fd = 3
1074+
os.environ["_LAMBDA_TELEMETRY_LOG_FD"] = "3"
10751075

10761076
actual = bootstrap.create_log_sink()
10771077

10781078
self.assertIsInstance(actual, bootstrap.FramedTelemetryLogSink)
1079-
self.assertEqual(actual.filename, "/proc/self/fd/" + fd)
1079+
self.assertEqual(actual.fd, fd)
10801080
self.assertFalse("_LAMBDA_TELEMETRY_LOG_FD" in os.environ)
10811081

10821082
def test_single_frame(self):
10831083
with NamedTemporaryFile() as temp_file:
10841084
message = "hello world\nsomething on a new line!\n"
1085-
with bootstrap.FramedTelemetryLogSink(temp_file.name) as ls:
1085+
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls:
10861086
ls.log(message)
10871087
with open(temp_file.name, "rb") as f:
10881088
content = f.read()
@@ -1101,7 +1101,7 @@ def test_multiple_frame(self):
11011101
first_message = "hello world\nsomething on a new line!"
11021102
second_message = "hello again\nhere's another message\n"
11031103

1104-
with bootstrap.FramedTelemetryLogSink(temp_file.name) as ls:
1104+
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls:
11051105
ls.log(first_message)
11061106
ls.log(second_message)
11071107

0 commit comments

Comments
 (0)